/
mongo-instances.tests.js
79 lines (63 loc) 路 2.69 KB
/
mongo-instances.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
/* eslint-env mocha */
import { expect } from 'chai'
import { Random } from 'meteor/random'
import { Mongo } from 'meteor/mongo'
import { Meteor } from 'meteor/meteor'
const randomName = () => `test${Random.id(6)}`
const equal = (a, b) => expect(a).to.equal(b)
describe('unit tests', () => {
let collectionName
beforeEach(() => {
collectionName = randomName() // random ID, so a new collection every time
})
it('basic - works Mongo.Collection', () => {
const Test = new Mongo.Collection(collectionName)
Test.insert({ test: true })
const find = Mongo.Collection.get(collectionName).find({ test: true })
equal(find.count(), 1)
// get an existing collection again
const ReGet = Mongo.Collection.get(collectionName)
equal(ReGet.find().count(), 1)
})
it('basic - works Meteor.Collection', function () {
const Test = new Meteor.Collection(collectionName)
Test.insert({ test: true })
const find = Meteor.Collection.get(collectionName).find({ test: true })
equal(find.count(), 1)
})
it('basic - collection already exists', () => {
const createInstance = () => new Mongo.Collection(collectionName)
createInstance()
expect(createInstance).to.throw('is already')
})
it('nonexistent - returns undefined', () => {
const collection = Mongo.Collection.get('truly-non-existent')
equal(collection, undefined)
})
it('instanceof - matches Mongo.Collection', () => {
const Test = new Mongo.Collection(collectionName)
expect(Test).to.be.instanceOf(Mongo.Collection)
})
it('instanceof - Meteor.Collection matches Mongo.Collection', () => {
const Test = new Meteor.Collection(collectionName)
expect(Test).to.be.instanceOf(Mongo.Collection)
})
it('instanceof - Meteor.users matches (Mongo/Meteor).Collection', () => {
expect(Meteor.users).to.be.instanceOf(Mongo.Collection)
expect(Meteor.users).to.be.instanceOf(Meteor.Collection)
})
it('constructor equality - Mongo/Meteor.Collection === Mongo/Meteor.Collection.prototype.constructor', () => {
equal(Mongo.Collection, Mongo.Collection.prototype.constructor)
equal(Meteor.Collection, Mongo.Collection.prototype.constructor)
equal(Meteor.Collection, Meteor.Collection.prototype.constructor)
})
it('use New - keep behavior of Mongo.Collection', () => {
const createWithoutNew = () => Mongo.Collection(collectionName)
expect(createWithoutNew).to.throw('this._maybeSetUpReplication is not a function')
})
it('users - can Mongo.Collection.get Meteor.users instance', () => {
const name = 'users'
expect(Mongo.Collection.get(name)).to.be.instanceOf(Mongo.Collection)
expect(Mongo.Collection.get(name)).to.be.instanceOf(Meteor.Collection)
})
})