From 5de03e1b49734a23c613c5e97690114423009a59 Mon Sep 17 00:00:00 2001 From: Andrew Mao Date: Wed, 18 Dec 2013 16:15:47 -0500 Subject: [PATCH] fix find/findOne userId tests --- package.js | 6 +- tests/find_findone_userid.js | 125 +++++++++++++++++++++++------------ 2 files changed, 83 insertions(+), 48 deletions(-) diff --git a/package.js b/package.js index a8efe1b..36189e4 100644 --- a/package.js +++ b/package.js @@ -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); @@ -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"); -}); \ No newline at end of file +}); diff --git a/tests/find_findone_userid.js b/tests/find_findone_userid.js index ebfce89..390f97d 100644 --- a/tests/find_findone_userid.js +++ b/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"); }); -} \ No newline at end of file +}