-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.test.ts
80 lines (66 loc) · 2.67 KB
/
operations.test.ts
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
import { expect } from 'chai'
import { UserLogic } from '.'
const test = (definition, context, expected) => {
expect(new UserLogic({definition}).evaluate(context)).to.deep.equal(expected)
}
describe('UserLogic operations', () => {
it("should do 'not' ops", () => {
test({not: true}, {}, false)
test({not: false}, {}, true)
})
it("should do 'eq' ops", () => {
test({eq: ['a', 'b']}, {}, false)
test({eq: ['a', 'a']}, {}, true)
})
it("should do 'and' ops", () => {
test({and: [true, false]}, {}, false)
test({and: [true, true]}, {}, true)
test({and: [true, true, false]}, {}, false)
})
it("should do 'or' ops", () => {
test({or: [false, false]}, {}, false)
test({or: [false, true]}, {}, true)
test({or: [true, true]}, {}, true)
test({or: [false, false, true]}, {}, true)
})
it("should do literals", () => {
test({literal: {test: 4}}, {}, {test: 4})
test({literal: {test: [1, 2, 3]}}, {}, {test: [1, 2, 3]})
test({literal: {test: {foo: [1, 2, 3]}}}, {}, {test: {foo: [1, 2, 3]}})
test({literal: {test: {$logic: {or: [false, 5]}}}}, {}, {test: 5})
test({literal: {test: [{$logic: {or: [false, 5]}}]}}, {}, {test: [5]})
})
it("should do value templates", () => {
test("$foo.bar", {foo: {bar: 5}}, 5)
})
it("should do string templates", () => {
test("`Yes ${foo.bar}!`", {foo: {bar: 5}}, 'Yes 5!')
})
it("should do objects", () => {
test({object: {foo: {not: 1}, bar: {and: [true, false]}}}, {}, {foo: false, bar: false})
})
it("should do object properties", () => {
test({'object-property': [{literal: {foo: {bar: 3}}}, 'foo']}, {}, {bar: 3})
test({'object-property': [{literal: {foo: {bar: 3}}}, 'foo.bar']}, {}, 3)
test({'object-property': [{literal: {foo: {bar: 3}}}, 'foo', 'bar']}, {}, 3)
test({'object-property': [
{literal: {foo: {bar: 3}}},
'$key',
'bar'
]}, {key: 'foo'}, 3)
})
it('should do ifs', () => {
test({'if': [true, 'yup', 'nope']}, {}, 'yup')
test({'if': [false, 'yup', 'nope']}, {}, 'nope')
})
it('should do map', () => {
test({'map': [['one', 'two'], "`${map.value}!`"]}, {}, ['one!', 'two!'])
test({'map': [{'literal': {'first': 'one', 'second': 'two'}}, "`${map.key} ${map.value}!`"]}, {}, {'first': 'first one!', 'second': 'second two!'})
})
it('should do split', () => {
test({'split': ['one two', ' ']}, {}, ['one', 'two'])
})
it('should do join', () => {
test({'join': [['one', 'two'], ' ']}, {}, 'one two')
})
})