-
Notifications
You must be signed in to change notification settings - Fork 0
/
method-factory-tests.js
160 lines (140 loc) · 5.24 KB
/
method-factory-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
/* global describe it */
import { Meteor } from 'meteor/meteor'
import { check } from 'meteor/check'
import { Random } from 'meteor/random'
import { ValidatedMethod } from 'meteor/mdg:validated-method'
import { createMethodFactory } from 'meteor/leaonline:method-factory'
import { expect } from 'chai'
import SimpleSchema from 'simpl-schema'
const schemaFactory = def => new SimpleSchema(def)
const methodExists = name => expect(Meteor.server.method_handlers[name]).to.be.a('function')
const expectThrow = async ({ fn, message, details }) => {
try {
await fn()
expect.fail()
} catch (e) {
if (message) expect(e.message).to.include(message)
if (details) expect(e.details).to.deep.equal(details)
}
}
function testMixin (options) {
check(options.foo, String)
return options
}
const withMixins = createMethodFactory({ mixins: [testMixin] })
class CustomMethod extends ValidatedMethod {
constructor (...args) {
super(...args)
this.foo = 'bar'
}
}
describe('defaults, no params', function () {
it('creates a validated method by default', async () => {
const createMethod = createMethodFactory()
const someMethod = createMethod({
name: 'someMethod',
validate: () => {},
run: () => 'hi',
applyOptions: { noRetry: true }
})
methodExists('someMethod')
expect(await someMethod.call()).to.equal('hi')
})
})
describe('custom class', function () {
it('throws if the custom class is not extending ValidatedMethod', function () {
expect(() => createMethodFactory({ custom: Date })).to.throw()
})
it('creates an extended ValidatedMethod', function () {
const createMethod = createMethodFactory({ custom: CustomMethod })
const method = createMethod({ name: 'fooTest', validate: () => {}, run: () => {} })
expect(method.foo).equal('bar')
})
})
describe('with schema', function () {
it('throws if the given schemaFactory is not a function', function () {
const expectThrow = value => expect(() => createMethodFactory({ schemaFactory: value })).to.throw()
expectThrow([])
expectThrow({})
expectThrow(new SimpleSchema({ title: String }))
expectThrow(1)
expectThrow('foo')
})
it('allows to define schema as validation base', async () => {
const createMethod = createMethodFactory({ schemaFactory })
const methodArgs = { name: Random.id(), schema: { title: String }, run: ({ title }) => `Hello, ${title}` }
const method = createMethod(methodArgs)
// expected fails
await expectThrow({ fn: () => method.call() })
await expectThrow({ fn: () => method.call({}) })
await expectThrow({ fn: () => method.call({ foo: 'bar'}) })
// expected pass
expect(await method.call({ title: 'Mr.x' })).to.equal('Hello, Mr.x')
})
it('also works with check/match', async () => {
const checkMatchFactory = schema => ({
validate (args) {
check(args, schema)
}
})
const createMethod = createMethodFactory({ schemaFactory: checkMatchFactory })
const methodArgs = { name: Random.id(), schema: { title: String }, run: ({ title }) => `Hello, ${title}` }
const method = createMethod(methodArgs)
// expected fails
await expectThrow({ fn: () => method.call() })
await expectThrow({ fn: () => method.call({}) })
await expectThrow({ fn: () => method.call({ foo: 'bar'}) })
// expected pass
expect(await method.call({ title: 'Mr.x' })).to.equal('Hello, Mr.x')
})
it('allows to override schema validation with a custom validate function', async () => {
const createMethod = createMethodFactory({ schemaFactory })
const methodArgs = {
name: Random.id(),
validate: () => {},
schema: { title: String },
run: ({ title }) => `Hello, ${title}`
}
const method = createMethod(methodArgs)
expect(await method.call({})).to.equal('Hello, undefined')
})
})
describe('with mixins', function () {
const validate = () => {}
const run = () => {}
it('throws if mixins are not an array of functions', function () {
const expectThrow = mixins => expect(() => createMethodFactory({ mixins })).to.throw()
expectThrow(1)
expectThrow('foo')
expectThrow({})
expectThrow(false)
expectThrow([1])
expectThrow(['foo'])
expectThrow([{}])
expectThrow([false])
})
it('allows to define mixins on the abstract factory level', function () {
// expect error
expect(() => withMixins({ name: Random.id(), validate, run })).to.throw()
// expect pass
withMixins({ name: Random.id(), validate, run, foo: 'bar' }).call()
})
it('allows to define mixins on the factory level', function () {
const createMethod = createMethodFactory()
const mixins = [testMixin]
// expect error
expect(() => createMethod({ name: Random.id(), validate, run, mixins })).to.throw()
// expect pass
createMethod({ name: Random.id(), validate, run, foo: 'bar', mixins }).call()
})
it('allows to define mixins on both levels together', function () {
const mixins = [function (options) {
check(options.bar, String)
return options
}]
// expect error
expect(() => withMixins({ name: Random.id(), validate, run, mixins, foo: 'bar' })).to.throw()
// expect pass
withMixins({ name: Random.id(), validate, run, foo: 'bar', bar: 'baz', mixins }).call()
})
})