quave:testing

v1.0.1Published 3 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

quave:testing

quave:testing is a Meteor package that allows you to test your Meteor app easily.

Why

We want to hide the complexity of Meteor internals and other details that sometime is necessary to implement to be able to test properly.

We believe we are not reinventing the wheel in this package but what we are doing is like putting together the wheels in the vehicle :).

Installation

meteor add quave:testing

Usage

mockMethodCall

  • Mocked Meteor Method call so you can include context as this. The context should be your last argument in the call. See a few examples below.
1import { Meteor } from 'meteor/meteor';
2import { Random } from 'meteor/random';
3import { assert } from 'chai';
4import { TasksCollection } from '/imports/db/TasksCollection';
5import '/imports/api/tasksMethods';
6
7import { mockMethodCall } from "meteor/quave:testing";
8
9if (Meteor.isServer) {
10  describe('Tasks', () => {
11    describe('methods', () => {
12      const userId = Random.id();
13      let taskId;
14
15      beforeEach(() => {
16        TasksCollection.remove({});
17        taskId = TasksCollection.insert({
18          text: 'Test Task',
19          createdAt: new Date(),
20          userId,
21        });
22      });
23
24      it(`can't delete task without an user authenticated`, () => {
25        const fn = () => mockMethodCall('tasks.remove', taskId);
26        assert.throw(fn, /Not authorized/);
27        assert.equal(TasksCollection.find().count(), 1);
28      });
29
30      it('can delete owned task', () => {
31        mockMethodCall('tasks.remove', taskId, { context: { userId } });
32
33        assert.equal(TasksCollection.find().count(), 0);
34      });
35
36      it(`can't delete task from another owner`, () => {
37        const fn = () =>
38          mockMethodCall('tasks.remove', taskId, {
39            context: { userId: 'somebody-else-id' },
40          });
41        assert.throw(fn, /Access denied/);
42        assert.equal(TasksCollection.find().count(), 1);
43      });
44
45      it('can change the status of a task', () => {
46        const originalTask = TasksCollection.findOne(taskId);
47        mockMethodCall('tasks.setIsChecked', taskId, !originalTask.isChecked, {
48          context: { userId },
49        });
50
51        const updatedTask = TasksCollection.findOne(taskId);
52        assert.notEqual(updatedTask.isChecked, originalTask.isChecked);
53      });
54
55      it('can insert new tasks', () => {
56        const text = 'New Task';
57        mockMethodCall('tasks.insert', text, {
58          context: { userId },
59        });
60
61        const tasks = TasksCollection.find({}).fetch();
62        assert.equal(tasks.length, 2);
63        assert.isTrue(tasks.some(task => task.text === text));
64      });
65    });
66  });
67}

License

MIT