forked from meteor/validated-method
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidated-method-tests.js
152 lines (131 loc) · 3.58 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
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';
}
});
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('checks schema', (done) => {
[methodWithArgs, methodWithSchemaMixin].forEach((method) => {
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: [function (args) { return 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();
});
});
});