/
state.spec.ts
239 lines (218 loc) · 7.57 KB
/
state.spec.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import { expect } from 'chai'
import sinon from 'sinon'
import { LiveState, LifeCycle, PassiveState } from '../src/state'
describe('state.ts', () => {
context('LiveState', () => {
it('should get and set values', () => {
let state = LiveState.of('v1')
expect(state.peek()).to.equals('v1')
state.update('v2')
expect(state.peek()).to.equals('v2')
})
it('should invoke setup lifecycle', () => {
let state = LiveState.of('v1')
let setup = sinon.fake()
state.attach({
setup,
})
expect(setup.getCalls().length).to.equals(1)
})
it('should invoke teardown lifecycle', () => {
let state = LiveState.of('v1')
let teardown = sinon.fake()
state.attach({
teardown,
})
expect(teardown.getCalls().length).to.equals(0)
state.teardown()
expect(teardown.getCalls().length).to.equals(1)
})
it('should not throw error when attached empty lifecycle', () => {
let state = LiveState.of(1)
state.attach({})
state.teardown()
})
context('pushing live update', () => {
let state: LiveState<string>
let update: sinon.SinonSpy
let remove: () => void
before(() => {
state = LiveState.of('v1')
update = sinon.fake()
})
after(() => {
state.teardown()
})
it('return remover function when attach lifecycle', () => {
remove = state.attach({ update })
expect(remove).to.be.a('function')
})
it('should push live update when attached lifecycle', () => {
expect(update.getCalls().length).to.equals(0)
state.update('v2')
expect(update.getCalls().length).to.equals(1)
expect(update.getCalls()[0].args).to.deep.equals(['v2', 'v1'])
})
it('should not push update when the new value is the same as old value', () => {
state.update('v2')
expect(update.getCalls().length).to.equals(1)
})
it('should update when value is changed multiple times', () => {
state.update('v3')
expect(update.getCalls().length).to.equals(2)
expect(update.getCalls()[1].args).to.deep.equals(['v3', 'v2'])
})
it('remove lifecycle', () => {
remove()
})
it('should not push live update after remove lifecycle', () => {
state.update('v3')
expect(update.getCalls().length).to.equals(2)
})
})
context('state.map', () => {
context('push lifecycle down the chain', () => {
let state1: LiveState<number>
let state2: PassiveState<number>
beforeEach(() => {
state1 = LiveState.of(1)
state2 = state1.map(x => x + 10)
})
afterEach(() => {
state2.teardown()
state1.teardown()
})
it('should trigger setup hook', () => {
expect(state1.peek()).to.equals(1)
expect(state2.peek()).to.equals(11)
})
it('should trigger update lifecycle', () => {
state1.update(2)
expect(state1.peek()).to.equals(2)
expect(state2.peek()).to.equals(12)
})
it('should trigger teardown lifecycle', () => {
let teardown = sinon.fake()
state2.attach({ teardown })
state1.teardown()
expect(teardown.getCalls().length).to.equals(1)
})
})
it('should throw error when directly update mapped state', () => {
let state1 = LiveState.of(1)
let state2 = state1.map(x => x + 10)
let errorMock = mockConsoleError()
expect(() => (state2 as LiveState<any>).update(2)).to.throw(
'Cannot update passive state',
)
expect(errorMock.spy.getCalls().length).to.equals(1)
errorMock.restore()
state1.teardown()
})
})
context('state.watch', () => {
it('should watch for changes', () => {
let state = LiveState.of('v1')
let callback = sinon.fake()
state.watch(callback)
expect(callback.getCalls().length).to.equals(1)
expect(callback.getCalls()[0].args).to.deep.equals(['v1'])
state.update('v2')
expect(callback.getCalls().length).to.equals(2)
expect(callback.getCalls()[1].args).to.deep.equals(['v2'])
})
it('should not receive callback after detach watcher', () => {
let state = LiveState.of('v1')
let callback = sinon.fake()
let detach = state.watch(callback)
expect(callback.getCalls().length).to.equals(1)
detach()
state.update('v2')
expect(callback.getCalls().length).to.equals(1)
})
it('should auto remove watcher after teardown', () => {
let state = LiveState.of('v1')
let callback = sinon.fake()
state.watch(callback)
expect(callback.getCalls().length).to.equals(1)
state.teardown()
state.update('v2')
expect(callback.getCalls().length).to.equals(1)
})
it('should be able to watch derived state update', () => {
let state1 = LiveState.of('v1')
let state2 = state1.map(value => [value, value])
let callback = sinon.fake()
state2.watch(callback)
expect(callback.getCalls().length).to.equals(1)
expect(callback.getCalls()[0].args).to.deep.equals([['v1', 'v1']])
state1.update('v2')
expect(callback.getCalls().length).to.equals(2)
expect(callback.getCalls()[1].args).to.deep.equals([['v2', 'v2']])
})
})
context('LiveState.combine', () => {
let a: LiveState<string>
let b: LiveState<string>
let c: PassiveState<[string, string]>
before(() => {
a = LiveState.of('a')
b = LiveState.of('b')
})
it('should combine state with initial value', () => {
c = LiveState.combine(a, b, (a, b) => [a, b], '[a, b]')
expect(c.peek()).to.deep.equals(['a', 'b'])
})
it('should push update from upstream', () => {
a.update('aa')
expect(c.peek()).to.deep.equals(['aa', 'b'])
b.update('bb')
expect(c.peek()).to.deep.equals(['aa', 'bb'])
})
it('should not re-compute when upstream value is not changed', () => {
let value = c.peek()
a.update('aa')
b.update('bb')
expect(c.peek()).to.equals(value)
})
it('should not allow update combined value from external', () => {
let errorMock = mockConsoleError()
expect(() => (c as LiveState<any>).update('c')).to.throw(
'Cannot update passive state',
)
expect(errorMock.spy.getCalls().length).to.equals(1)
errorMock.restore()
})
context('teardown source state of combined state', () => {
let teardown: sinon.SinonSpy
beforeEach(() => {
a = LiveState.of('a')
b = LiveState.of('a')
c = LiveState.combine(a, b, (a, b) => [a, b], '[a, b]')
teardown = sinon.fake()
c.attach({ teardown })
})
it('when first source state is teardown', () => {
a.teardown()
expect(teardown.getCalls().length).to.equals(1)
})
it('when second source state is teardown', () => {
b.teardown()
expect(teardown.getCalls().length).to.equals(1)
})
it('when both source states are teardown', () => {
a.teardown()
b.teardown()
expect(teardown.getCalls().length).to.equals(1)
})
})
})
})
})
function mockConsoleError() {
let originalError = console.error
let spy = sinon.fake()
console.error = spy
let restore = () => (console.error = originalError)
return { spy, restore }
}