Skip to content

Commit

Permalink
馃悰 Properly fix #14 and fix #24
Browse files Browse the repository at this point in the history
  • Loading branch information
coagmano committed Mar 26, 2018
1 parent b8e6fdc commit b5e8589
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
20 changes: 12 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,33 @@ const StubCollections = (() => {
publicApi.stub = (collections) => {
const pendingCollections = collections || privateApi.collections;
[].concat(pendingCollections).forEach((collection) => {
if (!privateApi.pairs[collection._name]) {
if (!privateApi.pairs.has(collection)) {
const options = {
connection: null,
transform: collection._transform,
};
const stubName = collection._name + 'Stub' + Random.id();
const localCollection = new collection.constructor(stubName, options);
const pair = { localCollection, collection };
privateApi.stubPair(pair);
privateApi.pairs[collection._name] = pair;
const localCollection = new collection.constructor(collection._name, options);
privateApi.stubPair({ localCollection, collection });
privateApi.pairs.set(collection, localCollection);
}
});
};

publicApi.restore = () => {
// Pre-emptively remove the documents from the local collection because if
// a collection with the same name is stubbed later it will still have the
// documents from LocalConnectionDriver's internal cache.
for (const localCollection of privateApi.pairs.values()) {
localCollection.remove({});
}
privateApi.sandbox.restore();
privateApi.pairs = {};
privateApi.pairs.clear();
};

/* Private API */

privateApi.sandbox = sinon.sandbox.create();
privateApi.pairs = {};
privateApi.pairs = new Map();
privateApi.collections = [];
privateApi.symbols = Object.keys(Mongo.Collection.prototype);

Expand Down
37 changes: 37 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ 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) {
Expand Down Expand Up @@ -58,4 +61,38 @@ describe('StubCollections', function () {
StubCollections.restore();
expect(widgets.simpleSchema()._firstLevelSchemaKeys).to.include('schemaKey');
});

it("should stub mutliple null collections", function() {
localWidgets1.insert({});
localWidgets2.insert({});

expect(localWidgets1.find().count()).to.equal(1);
expect(localWidgets2.find().count()).to.equal(1);

StubCollections.stub([localWidgets1, localWidgets2]);

expect(localWidgets1.find().count()).to.equal(0);
expect(localWidgets2.find().count()).to.equal(0);

localWidgets1.insert({});
localWidgets1.insert({});
localWidgets2.insert({});
localWidgets2.insert({});

expect(localWidgets1.find().count()).to.equal(2);
expect(localWidgets2.find().count()).to.equal(2);

StubCollections.restore();

expect(localWidgets1.find().count()).to.equal(1);
expect(localWidgets2.find().count()).to.equal(1);

StubCollections.stub([localWidgets1, localWidgets2]);

localWidgets1.insert({});
localWidgets2.insert({});

expect(localWidgets1.find().count()).to.equal(1);
expect(localWidgets2.find().count()).to.equal(1);
});
});

0 comments on commit b5e8589

Please sign in to comment.