-
Notifications
You must be signed in to change notification settings - Fork 28
/
validated-method-tests.js
212 lines (183 loc) · 4.98 KB
/
validated-method-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
import { Meteor } from 'meteor/meteor';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import { assert } from 'meteor/practicalmeteor:chai';
const plainMethod = new ValidatedMethod({
name: 'plainMethod',
validate: new SimpleSchema({}).validator(),
run() {
return 'result';
}
});
const noArgsMethod = new ValidatedMethod({
name: 'noArgsMethod',
validate: null,
run() {
return 'result';
}
});
const methodWithArgs = new ValidatedMethod({
name: 'methodWithArgs',
validate: new SimpleSchema({
int: { type: Number },
string: { type: String },
}).validator(),
run() {
return 'result';
}
});
const methodThrowsImmediately = new ValidatedMethod({
name: 'methodThrowsImmediately',
validate: null,
run() {
throw new Meteor.Error('error');
}
});
const methodReturnsName = new ValidatedMethod({
name: 'methodReturnsName',
validate: null,
run() {
return this.name;
}
});
const methodWithSchemaMixin = new ValidatedMethod({
name: 'methodWithSchemaMixin',
mixins: [schemaMixin],
schema: new SimpleSchema({
int: { type: Number },
string: { type: String },
}),
run() {
return 'result';
}
});
let resultReceived = false;
const methodWithApplyOptions = new ValidatedMethod({
name: 'methodWithApplyOptions',
validate: new SimpleSchema({}).validator(),
applyOptions: {
onResultReceived: function() {
resultReceived = true;
}
},
run() {
return 'result';
}
});
const asyncMethod = new ValidatedMethod({
name: 'asyncMethod',
validate: new SimpleSchema({
param: { type: Number },
}).validator(),
async run({ param }) {
return new Promise(resolve => {
setTimeout(() => {
resolve(['result', 'result2', param]);
}, 1000);
})
.then(result => {
return result.join(',');
});
}
});
function schemaMixin(methodOptions) {
methodOptions.validate = methodOptions.schema.validator();
return methodOptions;
}
describe('mdg:method', () => {
it('defines a method that can be called', (done) => {
plainMethod.call({}, (error, result) => {
assert.equal(result, 'result');
Meteor.call(plainMethod.name, {}, (error, result) => {
assert.equal(result, 'result');
done();
});
});
});
it('allows methods that take no arguments', (done) => {
noArgsMethod.call((error, result) => {
assert.equal(result, 'result');
Meteor.call(noArgsMethod.name, (error, result) => {
assert.equal(result, 'result');
done();
});
});
});
it('allows methods to be called asynchronously', (done) => {
asyncMethod.callAsync({ param: 3000 })
.then(result => {
assert.equal(result, 'result,result2,3000');
done();
});
});
[methodWithArgs, methodWithSchemaMixin].forEach((method) => {
it('checks schema ' + method.name, (done) => {
method.call({}, (error, result) => {
// 2 invalid fields
assert.equal(error.errors.length, 2);
method.call({
int: 5,
string: "what",
}, (error, result) => {
// All good!
assert.equal(result, 'result');
done();
});
});
});
});
it('throws error if no callback passed', (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', () => {
assert.throws(() => {
new ValidatedMethod({
name: 'methodWithFaultySchemaMixin',
mixins: [function nonReturningFunction() {}],
schema: null,
run() {
return 'result';
}
});
}, /Error in methodWithFaultySchemaMixin method: The function 'nonReturningFunction' didn't return the options object/);
assert.throws(() => {
new ValidatedMethod({
name: 'methodWithFaultySchemaMixin',
mixins: [args => args, function () {}],
schema: null,
run() {
return 'result';
}
});
}, /Error in methodWithFaultySchemaMixin method: One of the mixins didn't return the options object/);
});
it('has access to the name on this.name', (done) => {
const ret = methodReturnsName._execute();
assert.equal(ret, 'methodReturnsName');
methodReturnsName.call({}, (err, res) => {
// The Method knows its own name
assert.equal(res, 'methodReturnsName');
done();
});
});
it('can accept Meteor.apply options', (done) => {
if (Meteor.isServer) {
// the only apply option that I can think of to test is client side only
return done();
}
resultReceived = false;
methodWithApplyOptions.call({}, (err, res) => {
// The Method knows its own name
assert.equal(resultReceived, true);
done();
});
});
});