-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
find_findone_userid.js
99 lines (77 loc) · 3.12 KB
/
find_findone_userid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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) {
Meteor.publish("test_publish_for_find_findone_userid", function () {
console.log("PUBLISHING");
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.add("find - userId available to after find hook when within publish context", function (test) {
test.notEqual(afterFindUserId, null);
});
Tinytest.add("findone - userId available to before findOne hook when within publish context", function (test) {
test.notEqual(beforeFindOneUserId, null);
});
Tinytest.add("findone - userId available to after findOne hook when within publish context", function (test) {
test.notEqual(afterFindOneUserId, null);
});
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");
});
}