Skip to content

Commit

Permalink
fix find/findOne userId tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mizzao committed Dec 18, 2013
1 parent c9ab900 commit 5de03e1
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 48 deletions.
6 changes: 2 additions & 4 deletions package.js
Expand Up @@ -60,9 +60,7 @@ Package.on_test(function (api) {

api.add_files(["tests/find.js"], both);
api.add_files(["tests/findone.js"], both);

// TODO: someone get this test working -- spent probably 10x the time trying to write these tests vs the code I'm testing! Not worth it anymore
//api.add_files(["tests/find_findone_userid.js"], both);
api.add_files(["tests/find_findone_userid.js"], both);

api.add_files(["tests/multiple_hooks.js"], both);
api.add_files(["tests/transform.js"], both);
Expand All @@ -72,4 +70,4 @@ Package.on_test(function (api) {
// NOTE: fetch can only work server-side because find's "fields" option is
// limited to only working on the server
//api.add_files(["tests/fetch.js"], "server");
});
});
125 changes: 81 additions & 44 deletions tests/find_findone_userid.js
@@ -1,62 +1,99 @@
var collection = new Meteor.Collection("test_collection_for_find_findone_userid");

var beforeFindUserId, afterFindUserId, beforeFindOneUserId, afterFindOneUserId;

// Don't declare hooks in publish method, as it is problematic
collection.before.find(function (userId, selector, options) {
if (options && options.test) { // ignore other calls to find (caused by insert/update)
beforeFindUserId = userId;
}
});

collection.after.find(function (userId, selector, options, result) {
if (options && options.test) { // ignore other calls to find (caused by insert/update)
afterFindUserId = userId;
}
});

collection.before.findOne(function (userId, selector, options) {
if (options && options.test) { // ignore other calls to find (caused by insert/update)
beforeFindOneUserId = userId;
}
});

collection.after.findOne(function (userId, selector, options, result) {
if (options && options.test) { // ignore other calls to find (caused by insert/update)
afterFindOneUserId = userId;
}
});

if (Meteor.isServer) {
Tinytest.addAsync("find - userId available to before find hook when within publish context", function (test, next) {
console.log("DECLARE HOOK FOR BEFORE FIND")
collection.before.find(function (userId, selector, options) {
console.log("got to before find")
if (options && options.test) { // ignore other calls to find (caused by insert/update)
test.notEqual(userId, undefined);
next();
}
});
});
Meteor.publish("test_publish_for_find_findone_userid", function () {
console.log("PUBLISHING");

Tinytest.addAsync("find - userId available to after find hook when within publish context", function (test, next) {
console.log("DECLARE HOOK FOR AFTER FIND")
collection.after.find(function (userId, selector, options, result) {
console.log("got to after find")
if (options && options.test) { // ignore other calls to find (caused by insert/update)
test.notEqual(userId, undefined);
next();
}
beforeFindUserId = null;
afterFindUserId = null;
beforeFindOneUserId = null;
afterFindOneUserId = null;

// Trigger hooks
collection.find({}, {test: 1});
collection.findOne({}, {test: 1});

// TODO: reloading the browser will break these tests, because duplicate names are generated.

Tinytest.add("find - userId available to before find hook when within publish context", function (test) {
test.notEqual(beforeFindUserId, null);
});
});

Tinytest.addAsync("findone - userId available to before findOne hook when within publish context", function (test, next) {
console.log("DECLARE HOOK FOR BEFORE FINDONE")
collection.before.findOne(function (userId, selector, options) {
console.log("got to before findOne")
if (options && options.test) { // ignore other calls to find (caused by insert/update)
test.notEqual(userId, undefined);
next();
}
Tinytest.add("find - userId available to after find hook when within publish context", function (test) {
test.notEqual(afterFindUserId, null);
});
});

Tinytest.addAsync("findone - userId available to after findOne hook when within publish context", function (test, next) {
console.log("DECLARE HOOK FOR AFTER FINDONE")
collection.after.findOne(function (userId, selector, options, result) {
console.log("got to after findOne")
if (options && options.test) { // ignore other calls to find (caused by insert/update)
test.notEqual(userId, undefined);
next();
}
Tinytest.add("findone - userId available to before findOne hook when within publish context", function (test) {
test.notEqual(beforeFindOneUserId, null);
});
});

collection.remove({});
collection.insert({test: 1}, function (err, id) {});
Tinytest.add("findone - userId available to after findOne hook when within publish context", function (test) {
test.notEqual(afterFindOneUserId, null);
});

Meteor.publish("test_publish_for_find_findone_userid", function () {
console.log("PUBLISHING")
collection.findOne({}, {test: 1});
return collection.find({}, {test: 1});
return;
});
}

if (Meteor.isClient) {
beforeFindUserId = null;
afterFindUserId = null;
beforeFindOneUserId = null;
afterFindOneUserId = null;

// Trigger hooks
collection.find({}, {test: 1});
collection.findOne({}, {test: 1});

// Run client tests.
// TODO: Somehow, Tinytest.add / addAsync doesn't work inside InsecureLogin.ready().

Tinytest.add("find - userId available to before find hook", function(test) {
test.notEqual(beforeFindUserId, null);
});

Tinytest.add("find - userId available to after find hook", function(test) {
test.notEqual(afterFindUserId, null);
});

Tinytest.add("findone - userId available to before findOne hook", function(test) {
test.notEqual(beforeFindOneUserId, null);
});

Tinytest.add("findone - userId available to after findOne hook", function(test) {
test.notEqual(afterFindOneUserId, null);
});

InsecureLogin.ready(function () {
console.log("TRIGGERING SERVER FIND TESTS");
// Run server tests
Meteor.subscribe("test_publish_for_find_findone_userid");
});
}
}

0 comments on commit 5de03e1

Please sign in to comment.