-
Notifications
You must be signed in to change notification settings - Fork 3
/
simple-schema-mixin-tests.js
335 lines (294 loc) · 9.15 KB
/
simple-schema-mixin-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
/* eslint-env mocha */
/* eslint-disable func-names, prefer-arrow-callback */
/* global Meteor */
/* global ValidatedMethod */
/* global assert */
/* global simpleSchemaMixin */
// import { SimpleSchema } from 'meteor/aldeed:simple-schema';
const plainMethod = new ValidatedMethod({
name: 'plainMethod',
mixins: [simpleSchemaMixin],
schema: {},
run() {
return 'result';
},
});
const noArgsMethod = new ValidatedMethod({
name: 'noArgsMethod',
mixins: [simpleSchemaMixin],
schema: null,
run() {
return 'result';
},
});
const methodWithArgs = new ValidatedMethod({
name: 'methodWithArgs',
mixins: [simpleSchemaMixin],
schema: {
int: { type: Number },
string: { type: String },
},
run() {
return 'result';
},
});
const methodWithSchemaMixin = new ValidatedMethod({
name: 'methodWithSchemaMixin',
mixins: [simpleSchemaMixin],
schema: {
int: { type: Number },
string: { type: String },
},
run() {
return 'result';
},
});
const methodThrowsImmediately = new ValidatedMethod({
name: 'methodThrowsImmediately',
mixins: [simpleSchemaMixin],
schema: null,
run() {
throw new Meteor.Error('error');
},
});
const methodReturnsName = new ValidatedMethod({
name: 'methodReturnsName',
validate: null,
run() {
return this.name;
},
});
let resultReceived = false;
const methodWithApplyOptions = new ValidatedMethod({
name: 'methodWithApplyOptions',
mixins: [simpleSchemaMixin],
schema: {},
applyOptions: {
onResultReceived: () => {
resultReceived = true;
},
},
run() {
return 'result';
},
});
describe('basic mdg:validated-method tests still work', function () {
it('defines a method that can be called', function (done) {
plainMethod.call({}, (error, result) => {
assert.equal(result, 'result');
Meteor.call(plainMethod.name, {}, (error2, result2) => {
assert.equal(result2, 'result');
done();
});
});
});
it.skip('allows methods that take no arguments', function (done) {
noArgsMethod.call((error, result) => {
assert.equal(result, 'result');
Meteor.call(noArgsMethod.name, (error2, result2) => {
assert.equal(result2, 'result');
done();
});
});
});
[methodWithArgs, methodWithSchemaMixin].forEach((method) => {
it(`checks schema ${method.name}`, function (done) {
method.call({}, (error) => {
// 2 invalid fields
assert.equal(error.errors.length, 2);
method.call({
int: 5,
string: 'what',
}, (error2, result) => {
// All good!
assert.equal(result, 'result');
done();
});
});
});
});
it('throws error if no callback passed', function (done) {
methodThrowsImmediately.call({}, (err) => {
// If you pass a callback, you get the error in the callback
assert.ok(err);
// If no callback, the error is thrown
assert.throws(() => {
methodThrowsImmediately.call({});
}, /error/);
done();
});
});
it('throws error if a mixin does not return the options object', function () {
assert.throws(() => {
new ValidatedMethod({ // eslint-disable-line no-new
name: 'methodWithFaultySchemaMixin',
mixins: [function nonReturningFunction() {}],
schema: null,
run() {
return 'result';
},
});
// eslint-disable-next-line max-len
}, /Error in methodWithFaultySchemaMixin method: The function 'nonReturningFunction' didn't return the options object/);
assert.throws(() => {
new ValidatedMethod({ // eslint-disable-line no-new
name: 'methodWithFaultySchemaMixin',
mixins: [function (args) { return args; }, function () {}],
schema: null,
run() {
return 'result';
},
});
// eslint-disable-next-line max-len
}, /Error in methodWithFaultySchemaMixin method: One of the mixins didn't return the options object/);
});
it('has access to the name on this.name', function (done) {
const ret = methodReturnsName._execute();
assert.equal(ret, 'methodReturnsName');
methodReturnsName.call({}, (err, res) => {
// The Method knows its own name
assert.equal(res, 'methodReturnsName');
done();
});
});
// the only apply option that I can think of to test is client side only
if (!Meteor.isServer) {
it('can accept Meteor.apply options', function (done) {
methodWithApplyOptions.call({}, () => {
// The Method knows its own name
assert.equal(resultReceived, true);
done();
});
});
}
});
const methodUsingDefaultValidate = new ValidatedMethod({
name: 'methodUsingDefaultValidate',
mixins: [simpleSchemaMixin],
validate: () => {},
run() {
return 'result';
},
});
const methodUsingNullValidate = new ValidatedMethod({
name: 'methodUsingNullValidate',
mixins: [simpleSchemaMixin],
validate: null,
run() {
return 'result';
},
});
const methodUsingDefaultValidateAndNullSchema = new ValidatedMethod({
name: 'methodUsingDefaultValidateAndNullSchema',
mixins: [simpleSchemaMixin],
schema: null,
validate: () => {},
run() {
return 'result';
},
});
const methodUsingNullValidateAndNullSchema = new ValidatedMethod({
name: 'methodUsingNullValidateAndNullSchema',
mixins: [simpleSchemaMixin],
schema: null,
validate: null,
run() {
return 'result';
},
});
const methodUsingNullValidateAndDefaultSchema = new ValidatedMethod({
name: 'methodUsingNullValidateAndDefaultSchema',
mixins: [simpleSchemaMixin],
schema: {},
validate: null,
run() {
return 'result';
},
});
const methodWithNoSchemaOrValidate = new ValidatedMethod({
name: 'methodWithNoSchemaOrValidate',
mixins: [simpleSchemaMixin],
run() {
return 'result';
},
});
const methodWithSchemaAndFilterTrue = new ValidatedMethod({
name: 'methodWithSchemaAndFilterTrue',
mixins: [simpleSchemaMixin],
schemaValidatorOptions: { clean: true, filter: true },
run() {
return 'result';
},
});
describe('rlivingston:simple-schema-mixin', function () {
it('allows validate in lieu of schema - only validate present', function (done) {
// validate ignores unexpected arguments by default. schema does not. So, if ignored, we pass.
methodUsingDefaultValidate.call({ unexpectedArg: 'test' }, (error, result) => {
assert.equal(result, 'result');
done();
});
});
it('allows validate in lieu of schema - only null validate present', function (done) {
// validate ignores unexpected arguments by default. schema does not. So, if ignored, we pass.
methodUsingNullValidate.call({ unexpectedArg: 'test' }, (error, result) => {
assert.equal(result, 'result');
done();
});
});
it('allows validate in lieu of schema - validate present with null schema'
, function (done) {
// validate ignores unexpected arguments by default. schema does not. So, if ignored, we pass.
methodUsingDefaultValidateAndNullSchema.call({ unexpectedArg: 'test' }, (error, result) => {
assert.equal(result, 'result');
done();
});
});
it('defaults to {} if no schema or validate supplied', function (done) {
methodWithNoSchemaOrValidate.call({ unexpectedArg: 'test' }, (error) => {
// If ValidateMethod handled it, its default stub does no validation. Thus
// there will be no validation-error due to the unexpectedArg. Therefore if we get a
// validation-error, all is good.
assert.equal(error.error, 'validation-error');
done();
});
});
it('defaults to {} if null schema and null validate supplied', function (done) {
methodUsingNullValidateAndNullSchema.call({ unexpectedArg: 'test' }, (error) => {
// If ValidateMethod handled it, its default stub does no validation. Thus
// there will be no validation-error due to the unexpectedArg. Therefore if we get a
// validation-error, all is good.
assert.equal(error.error, 'validation-error');
done();
});
});
it('uses schema if schema and null validate supplied', function (done) {
methodUsingNullValidateAndDefaultSchema.call({ unexpectedArg: 'test' }, (error) => {
// If ValidateMethod handled it, its default stub does no validation. Thus
// there will be no validation-error due to the unexpectedArg. Therefore if we get a
// validation-error, all is good.
assert.equal(error.error, 'validation-error');
done();
});
});
it('throws error if both schema and validate supplied', function () {
assert.throws(() => {
new ValidatedMethod({ // eslint-disable-line no-new
name: 'methodWithSchemaAndValidate',
mixins: [simpleSchemaMixin],
schema: {},
validate: () => {},
run() {
return 'result';
},
});
// eslint-disable-next-line max-len
}, /"schema" and "validate" options cannot be used together/);
});
it('schemaValidatorOptions overrides our defaults', function (done) {
methodWithSchemaAndFilterTrue.call({ unexpectedArg: 'test' }, (error, result) => {
// with filter set to true, the unexpectedArg will just be quietly thrown away.
assert.equal(result, 'result');
done();
});
});
});