/
Controller.test.js
176 lines (174 loc) · 4.32 KB
/
Controller.test.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
/* eslint-env mocha */
import Controller from './Controller'
import Module from './Module'
import assert from 'assert'
describe('Controller', () => {
it('should instantiate with initial state', () => {
const controller = new Controller({
state: {
foo: 'bar'
}
})
assert.deepEqual(controller.getState(), {foo: 'bar'})
})
it('should instantiate with signals defined', () => {
const controller = new Controller({
signals: {
foo: []
}
})
assert.ok(controller.getSignal('foo'))
})
it('should instantiate providers defined', () => {
const controller = new Controller({
signals: {
foo: [
function testAction (context) {
assert.equal(context.foo, 'bar')
}
]
},
providers: [
function TestProvider (context) {
context.foo = 'bar'
return context
}
]
})
controller.getSignal('foo')()
})
it('should instantiate modules defined as objects', () => {
const controller = new Controller({
modules: {
foo: {
state: {
foo: 'bar'
}
}
}
})
assert.deepEqual(controller.getState(), {foo: {foo: 'bar'}})
})
it('should instantiate modules defined as functions', () => {
const controller = new Controller({
modules: {
foo: () => ({
state: {
foo: 'bar'
}
})
}
})
assert.deepEqual(controller.getState(), {foo: {foo: 'bar'}})
})
it('should pass instance of module on functions module instantiation', () => {
const controller = new Controller({
modules: {
foo: (module) => {
assert.ok(module instanceof Module)
return {
state: {
foo: 'bar'
}
}
}
}
})
assert.deepEqual(controller.getState(), {foo: {foo: 'bar'}})
})
it('should expose method to get signals', () => {
const controller = new Controller({
signals: {
foo: []
},
modules: {
moduleA: {
signals: {
foo: []
}
}
}
})
assert.ok(controller.getSignal('foo'))
assert.ok(controller.getSignal('moduleA.foo'))
})
it('should expose method to get model', () => {
const controller = new Controller({
signals: {
foo: []
},
modules: {
moduleA: {
signals: {
foo: []
}
}
}
})
assert.equal(controller.getModel(), controller.model)
})
it('should create JSON stringify friendly value of unserializable payload property to signal', () => {
const controller = new Controller({
devtools: {init () {}},
signals: {
foo: [({input}) => assert.equal(JSON.stringify(input), '{"date":"[Date]"}')]
}
})
controller.getSignal('foo')({
date: new Date()
})
})
it('should ignore when passing in unserializable payload to signal', () => {
const controller = new Controller({
devtools: {init () {}},
signals: {
foo: [
({input}) => assert.deepEqual(input, {})
]
}
})
controller.getSignal('foo')(new Date())
})
it('should throw when pointing to a non existing signal', () => {
const controller = new Controller({})
assert.throws(() => {
controller.getSignal('foo.bar')()
})
})
it('should return undefined when grabbing non existing state', () => {
const controller = new Controller({})
assert.equal(controller.getState('foo.bar'), undefined)
})
it('should flush at async action, path end and end of signal', (done) => {
let flushCount = 0
const controller = new Controller({
signals: {
test: [
() => {},
() => Promise.resolve(),
({path}) => path.foo(), {
foo: [() => {}]
}
]
}
})
controller.on('flush', () => flushCount++)
controller.runTree.once('end', () => {
assert.equal(flushCount, 3)
done()
})
controller.getSignal('test')()
})
it('should flush model after module initialization', () => {
const controller = new Controller({
modules: {
editor: {
state: {
this: 'that'
}
}
}
})
assert.deepEqual(controller.model.flush(), {})
})
})