-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote-collections-provider-tests.js
383 lines (292 loc) · 14.7 KB
/
remote-collections-provider-tests.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Import Tinytest from the tinytest Meteor package.
import {Tinytest} from "meteor/tinytest";
// Import and rename a variable exported by remote-collections-provider.js.
import {RemoteCollectionsProvider} from "meteor/jkuester:remote-collections-provider";
import {} from 'meteor/audit-argument-checks';
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// STARTUP / INITIAL VALUES
//
////////////////////////////////////////////////////////////////////////////////////////////////////
Tinytest.add('remote-collections-provider - startup - import working as expected', function (test) {
testExists(test, RemoteCollectionsProvider, "RemoteCollectionsProvider");
});
Tinytest.add('remote-collections-provider - startup - there are default methods on startup', function (test) {
const defaultMethods = RemoteCollectionsProvider.getAllMethodNames();
testExists(test, defaultMethods);
test.equal(defaultMethods.length, 3); //there are three default methods
});
Tinytest.add('remote-collections-provider - startup - there are default callable via Meteor.call on startup', function (test) {
testMethodCall(test, RemoteCollectionsProvider.HAS_REMOTE_COLLECTIONS_PROVIDER, true);
testMethodCall(test, RemoteCollectionsProvider.DEFAULT_GET_COLLECTIONS, [RemoteCollectionsProvider.DEFAULT_COLLECTION]);
testMethodCall(test, RemoteCollectionsProvider.DEFAULT_GET_PUBLICATIONS, [RemoteCollectionsProvider.DEFAULT_PUBLICATION]);
});
Tinytest.add('remote-collections-provider - startup - there are default collections on startup', function (test) {
const allCollections = RemoteCollectionsProvider.getAllCollectionNames();
testAllAddedEntries(test, allCollections, 1);
test.equal(allCollections[0], "tests");
});
Tinytest.add('remote-collections-provider - init - init throws no errors', function (test) {
RemoteCollectionsProvider.init(); //removes the defaults
test.equal(true, true);
});
Tinytest.add('remote-collections-provider - init - init removes all default collections', function (test) {
testAllAddedEntries(test, RemoteCollectionsProvider.getAllCollectionNames(), 0);
});
Tinytest.add('remote-collections-provider - init - init removes all default methods', function (test) {
testAllAddedEntries(test, RemoteCollectionsProvider.getAllMethodNames(), 0);
});
Tinytest.add('remote-collections-provider - init - init removes all default publications', function (test) {
testAllAddedEntries(test, RemoteCollectionsProvider.getAllPublicationNames(), 0);
});
Tinytest.add('remote-collections-provider - init - defaults not callable after init', function (test) {
test.throws(function () {
Meteor.call(RemoteCollectionsProvider.HAS_REMOTE_COLLECTIONS_PROVIDER);
});
test.throws(function () {
Meteor.call(RemoteCollectionsProvider.DEFAULT_GET_COLLECTIONS);
});
test.throws(function () {
Meteor.call(RemoteCollectionsProvider.DEFAULT_GET_PUBLICATIONS);
});
});
Tinytest.add('remote-collections-provider - clear - clears all reference', function (test) {
addToCollection(test, "someCollection", null);
addMethod(test, "newMethod", () => {return "newMethod"}, true);
addPublication(test, "newPub", () => {return "newPub"}, true);
//hard remove
RemoteCollectionsProvider.clear(true);
test.equal(RemoteCollectionsProvider.getAllCollectionNames(), {});
test.equal(RemoteCollectionsProvider.getAllPublicationNames(), {});
test.equal(RemoteCollectionsProvider.getAllMethodNames(), {});
test.throws(function () {
Meteor.call("newMethod");
});
//soft remove
addToCollection(test, "someCollection", null);
addMethod(test, "newMethod", () => {return "newMethod"}, true);
addPublication(test, "newPub", () => {return "newPub"}, true);
RemoteCollectionsProvider.clear();
test.equal(RemoteCollectionsProvider.getAllCollectionNames(), {});
test.equal(RemoteCollectionsProvider.getAllPublicationNames(), {});
test.equal(RemoteCollectionsProvider.getAllMethodNames(), {});
testMethodCall(test, "newMethod", "newMethod");
//finally remove also from server
RemoteCollectionsProvider.removeMethod("newMethod", true);
});
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// METHODS
//
////////////////////////////////////////////////////////////////////////////////////////////////////
const methodName = "someMethod";
const methodFct = function () {
return "theMethod";
}
Tinytest.add('remote-collections-provider - methods - methods added working', function (test) {
RemoteCollectionsProvider.clear(true);
//if we add a method by name it should store the method ref
addMethod(test, methodName, methodFct, true);
test.equal(RemoteCollectionsProvider.getMethod(methodName), methodFct, "unexpected instance match");
testMethodCall(test, methodName, "theMethod");
//if we add just a name it should store the name
removeMethod(test, methodName, true);
addMethod(test, methodName, null, null);
test.equal(RemoteCollectionsProvider.getMethod(methodName), methodName);
test.throws(function () {
Meteor.call(methodName);
});
});
Tinytest.add('remote-collections-provider - methods - methods remove working', function (test) {
RemoteCollectionsProvider.clear(true);
addMethod(test, methodName, methodFct, true);
removeMethod(test, methodName, true);
test.throws(function () {
Meteor.call(methodName);
});
//throw and error if we want to add null to the server
test.throws(function () {
addMethod(test, methodName, null, true);
}, RemoteCollectionsProvider.CANNOT_ADD_NULL_TO_METHODS);
});
Tinytest.add('remote-collections-provider - methods - methods retrieve working', function (test) {
RemoteCollectionsProvider.clear(true);
addMethod(test, methodName, methodFct, true)
const retrievedMethod = RemoteCollectionsProvider.getMethod(methodName);
testExists(test, retrievedMethod, methodName);
test.equal(typeof retrievedMethod, "function");
test.equal(retrievedMethod, methodFct);
});
Tinytest.add('remote-collections-provider - methods - get all methods working', function (test) {
RemoteCollectionsProvider.clear(true);
addMethod(test, methodName, methodFct, true)
testAllAddedEntries(test, RemoteCollectionsProvider.getAllMethodNames(), 1);
});
Tinytest.add('remote-collections-provider - methods - methods apply working', function (test) {
RemoteCollectionsProvider.clear(true);
addMethod(test, methodName, methodFct, false); //dont apply immediately
test.throws(function () { //to throw a call error
Meteor.call(methodName);
});
RemoteCollectionsProvider.applyAllMethods(); //but when we apply...
testMethodCall(test, methodName, "theMethod"); //it should work
});
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// COLLECTIONS
//
////////////////////////////////////////////////////////////////////////////////////////////////////
const collectionName = "someCollection";
Tinytest.add('remote-collections-provider - collections - collection names are added', function (test) {
RemoteCollectionsProvider.clear(true);
//add collecition name
addToCollection(test, collectionName);
const collectionRef = RemoteCollectionsProvider.getCollection(collectionName);
testExists(test, collectionRef);
test.equal(collectionRef, collectionName);
removeCollection(test, collectionName);
//add collection schema
const schemaObj = {title: {type: String}};
addToCollection(test, collectionName, schemaObj); //keep it easy, avoid coupling to SimpleSchema here
const schemaRef = RemoteCollectionsProvider.getCollection(collectionName);
testExists(test, schemaRef);
test.equal(schemaRef, schemaObj);
});
Tinytest.add('remote-collections-provider - collections - collection names can be removed', function (test) {
RemoteCollectionsProvider.clear(true);
addToCollection(test, collectionName);
removeCollection(test, collectionName);
testExistsNot(test, RemoteCollectionsProvider.getCollection(collectionName));
});
Tinytest.add('remote-collections-provider - collections - collection names can be checke dif exist', function (test) {
RemoteCollectionsProvider.clear(true);
addToCollection(test, collectionName);
test.equal(RemoteCollectionsProvider.hasCollection(collectionName), true);
});
Tinytest.add('remote-collections-provider - collections - all collections can be retrieved', function (test) {
RemoteCollectionsProvider.clear(true);
addToCollection(test, collectionName);
testAllAddedEntries(test, RemoteCollectionsProvider.getAllCollectionNames(), 1);
});
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// PUBLICATIONS
//
////////////////////////////////////////////////////////////////////////////////////////////////////
const publicationName = "somePublication";
const publicationFunction = function () {
return testCollection.find({});
}
Tinytest.add('remote-collections-provider - publications - publications are added', function (test) {
RemoteCollectionsProvider.clear(true);
//add as function
addPublication(test, publicationName, publicationFunction, true);
test.equal(RemoteCollectionsProvider.getPublication(publicationName), publicationFunction);
removePublication(test, publicationName, true);
//add as name
addPublication(test, publicationName, null, null);
test.equal(RemoteCollectionsProvider.getPublication(publicationName), publicationName);
removePublication(test, publicationName, true);
//throw and error if we want to add null to the server
test.throws(function () {
addPublication(test, publicationName, null, true);
}, RemoteCollectionsProvider.CANNOT_ADD_NULL_TO_PUBLICATIONS);
});
Tinytest.add('remote-collections-provider - publications - publications names can be removed', function (test) {
RemoteCollectionsProvider.clear(true);
addPublication(test, publicationName, publicationFunction, true);
removePublication(test, publicationName, true);
testExistsNot(test, RemoteCollectionsProvider.getPublication(publicationName));
});
Tinytest.add('remote-collections-provider - publications - single publication can be retrieved', function (test) {
RemoteCollectionsProvider.clear(true);
addPublication(test, publicationName, publicationFunction, true);
const retrievedPub = RemoteCollectionsProvider.getPublication(publicationName);
testExists(test, retrievedPub, publicationName);
test.equal(retrievedPub, publicationFunction);
});
Tinytest.add('remote-collections-provider - publications - all publications can be retrieved', function (test) {
RemoteCollectionsProvider.clear(true);
addPublication(test, publicationName, publicationFunction, true);
testAllAddedEntries(test, RemoteCollectionsProvider.getAllPublicationNames(), 1);
});
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// TEST HELPER
//
////////////////////////////////////////////////////////////////////////////////////////////////////
function testExists(test, obj, optionalName) {
if (!optionalName)
optionalName = "";
test.isNotNull(obj, "unexpected object is null (" + optionalName + ")");
test.isNotUndefined(obj, "unexpected: object is undefined (" + optionalName + ")");
}
function testExistsNot(test, obj, optionalName) {
if (!optionalName)
optionalName = "";
const doesExist = obj !== null && typeof obj !== 'undefined';
test.equal(doesExist, false, "unexpected object existence, expected " + optionalName + " not to exist");
}
function testObjectHasChildren(test, obj, expectedChildCount) {
testExists(test, obj);
const keys = Object.keys(obj);
testExists(test, keys);
test.equal(keys.length, expectedChildCount, "unexpected: childcount is " + keys.length + ", expected is " + expectedChildCount);
}
function testMethodCall(test, methodName, expectedResult) {
testExists(test, Meteor.server.method_handlers[methodName], methodName);
const methodResult = Meteor.call(methodName);
testExists(test, methodResult, "result of " + methodName);
test.equal(methodResult, expectedResult);
}
function addMethod(test, methodName, method, applyImmediately = true) {
testExistsNot(test, RemoteCollectionsProvider.getMethod(methodName), methodName);
RemoteCollectionsProvider.addMethod(methodName, method, applyImmediately);
testExists(test, RemoteCollectionsProvider.getMethod(methodName), methodName);
}
function addToCollection(test, collectionName, optionalSchema = null) {
testExistsNot(test, RemoteCollectionsProvider.getCollection(collectionName), collectionName);
RemoteCollectionsProvider.addCollectionNames(collectionName, optionalSchema);
testExists(test, RemoteCollectionsProvider.getCollection(collectionName), collectionName);
}
function removeCollection(test, collectionName) {
testExists(test, RemoteCollectionsProvider.getCollection(collectionName), collectionName);
RemoteCollectionsProvider.removeCollection(collectionName);
testExistsNot(test, RemoteCollectionsProvider.getCollection(collectionName), collectionName);
}
function addPublication(test, publication, pubFunc, applyImmediately) {
//TEST IF NOT EXISTS
testExistsNot(test, RemoteCollectionsProvider.getPublication(publication), publication);
testExistsNot(test, Meteor.server.publish_handlers[publication], "publish handler");
//ADD
RemoteCollectionsProvider.addPublication(publication, pubFunc, applyImmediately);
if (pubFunc) {
testExists(test, RemoteCollectionsProvider.getPublication(publication), publication);
testExists(test, Meteor.server.publish_handlers[publication], "publish handler");
test.equal(RemoteCollectionsProvider.getPublication(publication), pubFunc);
test.equal(Meteor.server.publish_handlers[publication], pubFunc);
} else {
testExists(test, RemoteCollectionsProvider.getPublication(publication), publication);
testExistsNot(test, Meteor.server.publish_handlers[publication]);
test.equal(RemoteCollectionsProvider.getPublication(publication), publication);
}
}
function removePublication(test, publication, alsoRemoveFromServer) {
//FIRST TEST IF EXISTS (FROM LAST TEST
testExists(test, RemoteCollectionsProvider.getPublication(publication), publication);
//THEN REMOVE
RemoteCollectionsProvider.removePublication(publication, alsoRemoveFromServer);
//TEST IF NOT EXISTS
testExistsNot(test, RemoteCollectionsProvider.getPublication(publication), publication);
testExistsNot(test, Meteor.server.publish_handlers[publication], "publish handler (server) should not exist");
}
function removeMethod(test, methodName, alsoRemoveFromServer) {
testMethodCall(test, methodName, "theMethod");
testExists(test, RemoteCollectionsProvider.getMethod(methodName), methodName);
RemoteCollectionsProvider.removeMethod(methodName, alsoRemoveFromServer);
testExistsNot(test, RemoteCollectionsProvider.getMethod(methodName), methodName);
}
function testAllAddedEntries(test, list, expectedCount) {
testExists(test, list, "all entries list");
test.equal(list.length, expectedCount);
}