-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathasLogic.test.js
More file actions
90 lines (77 loc) · 2.18 KB
/
asLogic.test.js
File metadata and controls
90 lines (77 loc) · 2.18 KB
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
import { asLogicSync, asLogicAsync } from './asLogic.js'
const module = {
hello: (name = 'World', last = '') => `Hello, ${name || 'World'}${last.length ? ' ' : ''}${last}!`
}
describe('asLogicSync', () => {
it('Should be able to create a simple rule.', () => {
const builder = asLogicSync(module)
const f = builder({
hello: 'World'
})
expect(f()).toBe('Hello, World!')
const f2 = builder({
hello: { var: '' }
})
expect(f2('Jesse')).toBe('Hello, Jesse!')
const f3 = builder({
hello: null
})
expect(f3()).toBe('Hello, World!')
const f4 = builder({
hello: ['Jesse', 'Mitchell']
})
expect(f4()).toBe('Hello, Jesse Mitchell!')
})
it('Should not allow you to create a builder with methods that do not exist', () => {
const builder = asLogicSync(module)
expect(() => builder({
hello: { '+': [1, 2] }
})).toThrow()
})
it('Should let you select logic kept', () => {
const builder = asLogicSync(module, ['+'])
const f = builder({
hello: { '+': [1, 2] }
})
expect(f()).toBe('Hello, 3!')
})
})
describe('asLogicAsync', () => {
it('Should be able to create a simple rule.', async () => {
const builder = asLogicAsync(module)
const f = await builder({
hello: 'World'
})
expect(await f()).toBe('Hello, World!')
const f2 = await builder({
hello: { var: '' }
})
expect(await f2('Jesse')).toBe('Hello, Jesse!')
const f3 = await builder({
hello: null
})
expect(await f3()).toBe('Hello, World!')
const f4 = await builder({
hello: ['Jesse', 'Mitchell']
})
expect(await f4()).toBe('Hello, Jesse Mitchell!')
})
it('Should not allow you to create a builder with methods that do not exist', async () => {
const builder = asLogicAsync(module)
try {
await builder({
hello: { '+': [1, 2] }
})
} catch (e) {
return
}
throw new Error('Should have thrown an error')
})
it('Should let you select logic kept', async () => {
const builder = asLogicAsync(module, ['+'])
const f = await builder({
hello: { '+': [1, 2] }
})
expect(await f()).toBe('Hello, 3!')
})
})