Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
83 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
}); | ||
} | ||
} |