-
Notifications
You must be signed in to change notification settings - Fork 17
/
tests.js
104 lines (78 loc) · 3.1 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* eslint-env mocha */
/* eslint-disable func-names, prefer-arrow-callback, no-unused-expressions */
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { expect } from 'chai';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import StubCollections from './index';
const widgets = new Mongo.Collection('widgets');
const localWidgets1 = new Mongo.Collection(null);
const localWidgets2 = new Mongo.Collection(null);
const schema = { schemaKey: { type: String, optional: true } };
widgets.attachSchema(new SimpleSchema(schema));
if (Meteor.isServer) {
widgets.remove({});
widgets.insert({});
Meteor.publish('widgets', function allWidgets() {
return widgets.find();
});
} else {
Meteor.subscribe('widgets');
}
describe('StubCollections', function () {
it('should stub added/registered collections', function () {
expect(widgets.find().count()).to.equal(1);
StubCollections.add([widgets]);
StubCollections.stub();
expect(widgets.find().count()).to.equal(0);
widgets.insert({});
widgets.insert({});
expect(widgets.find().count()).to.equal(2);
StubCollections.restore();
expect(widgets.find().count()).to.equal(1);
});
it('should stub non-registered one-off collections', function () {
expect(widgets.find().count()).to.equal(1);
StubCollections.stub([widgets]);
expect(widgets.find().count()).to.equal(0);
widgets.insert({});
widgets.insert({});
expect(widgets.find().count()).to.equal(2);
StubCollections.restore();
expect(widgets.find().count()).to.equal(1);
});
it('should stub the schema of a collection', function () {
expect(widgets.simpleSchema()._firstLevelSchemaKeys).to.include('schemaKey');
StubCollections.stub([widgets]);
expect(widgets.simpleSchema()._firstLevelSchemaKeys).to.include('schemaKey');
StubCollections.restore();
expect(widgets.simpleSchema()._firstLevelSchemaKeys).to.include('schemaKey');
});
it("should stub mutliple null collections", function() {
localWidgets1.insert({});
localWidgets2.insert({});
localWidgets2.insert({});
expect(localWidgets1.find().count()).to.equal(1);
expect(localWidgets2.find().count()).to.equal(2);
StubCollections.stub([localWidgets1, localWidgets2]);
expect(localWidgets1.find().count()).to.equal(0);
expect(localWidgets2.find().count()).to.equal(0);
localWidgets1.insert({});
localWidgets1.insert({});
localWidgets2.insert({});
expect(localWidgets1.find().count()).to.equal(2);
expect(localWidgets2.find().count()).to.equal(1);
StubCollections.restore();
expect(localWidgets1.find().count()).to.equal(1);
expect(localWidgets2.find().count()).to.equal(2);
StubCollections.stub([localWidgets1, localWidgets2]);
localWidgets1.insert({});
localWidgets2.insert({});
localWidgets2.insert({});
expect(localWidgets1.find().count()).to.equal(1);
expect(localWidgets2.find().count()).to.equal(2);
StubCollections.restore();
expect(localWidgets1.find().count()).to.equal(1);
expect(localWidgets2.find().count()).to.equal(2);
});
});