|
| 1 | +/* eslint-env mocha */ |
| 2 | +import { expect } from 'chai' |
| 3 | +import { Random } from 'meteor/random' |
| 4 | +import { Mongo } from 'meteor/mongo' |
| 5 | +import { Meteor } from 'meteor/meteor' |
| 6 | + |
| 7 | +const randomName = () => `test${Random.id(6)}` |
| 8 | +const equal = (a, b) => expect(a).to.equal(b) |
| 9 | + |
| 10 | +describe('unit tests', () => { |
| 11 | + let collectionName |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + collectionName = randomName() // random ID, so a new collection every time |
| 15 | + }) |
| 16 | + |
| 17 | + it('basic - works Mongo.Collection', () => { |
| 18 | + const Test = new Mongo.Collection(collectionName) |
| 19 | + |
| 20 | + Test.insert({ test: true }) |
| 21 | + const find = Mongo.Collection.get(collectionName).find({ test: true }) |
| 22 | + equal(find.count(), 1) |
| 23 | + |
| 24 | + // get an existing collection again |
| 25 | + const ReGet = Mongo.Collection.get(collectionName) |
| 26 | + equal(ReGet.find().count(), 1) |
| 27 | + }) |
| 28 | + |
| 29 | + it('basic - works Meteor.Collection', function () { |
| 30 | + const Test = new Meteor.Collection(collectionName) |
| 31 | + Test.insert({ test: true }) |
| 32 | + |
| 33 | + const find = Meteor.Collection.get(collectionName).find({ test: true }) |
| 34 | + equal(find.count(), 1) |
| 35 | + }) |
| 36 | + |
| 37 | + it('basic - collection already exists', () => { |
| 38 | + const createInstance = () => new Mongo.Collection(collectionName) |
| 39 | + createInstance() |
| 40 | + expect(createInstance).to.throw('is already') |
| 41 | + }) |
| 42 | + |
| 43 | + it('nonexistent - returns undefined', () => { |
| 44 | + const collection = Mongo.Collection.get('truly-non-existent') |
| 45 | + equal(collection, undefined) |
| 46 | + }) |
| 47 | + |
| 48 | + it('instanceof - matches Mongo.Collection', () => { |
| 49 | + const Test = new Mongo.Collection(collectionName) |
| 50 | + expect(Test).to.be.instanceOf(Mongo.Collection) |
| 51 | + }) |
| 52 | + |
| 53 | + it('instanceof - Meteor.Collection matches Mongo.Collection', () => { |
| 54 | + const Test = new Meteor.Collection(collectionName) |
| 55 | + expect(Test).to.be.instanceOf(Mongo.Collection) |
| 56 | + }) |
| 57 | + |
| 58 | + it('instanceof - Meteor.users matches (Mongo/Meteor).Collection', () => { |
| 59 | + expect(Meteor.users).to.be.instanceOf(Mongo.Collection) |
| 60 | + expect(Meteor.users).to.be.instanceOf(Meteor.Collection) |
| 61 | + }) |
| 62 | + |
| 63 | + it('constructor equality - Mongo/Meteor.Collection === Mongo/Meteor.Collection.prototype.constructor', () => { |
| 64 | + equal(Mongo.Collection, Mongo.Collection.prototype.constructor) |
| 65 | + equal(Meteor.Collection, Mongo.Collection.prototype.constructor) |
| 66 | + equal(Meteor.Collection, Meteor.Collection.prototype.constructor) |
| 67 | + }) |
| 68 | + |
| 69 | + it('use New - keep behavior of Mongo.Collection', () => { |
| 70 | + const createWithoutNew = () => Mongo.Collection(collectionName) |
| 71 | + expect(createWithoutNew).to.throw('this._maybeSetUpReplication is not a function') |
| 72 | + }) |
| 73 | + |
| 74 | + it('users - can Mongo.Collection.get Meteor.users instance', () => { |
| 75 | + const name = 'users' |
| 76 | + expect(Mongo.Collection.get(name)).to.be.instanceOf(Mongo.Collection) |
| 77 | + expect(Mongo.Collection.get(name)).to.be.instanceOf(Meteor.Collection) |
| 78 | + }) |
| 79 | +}) |
0 commit comments