/
flux-angular-spec.js
391 lines (313 loc) · 12.3 KB
/
flux-angular-spec.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
const angular = require('angular')
require('angular-mocks')
require('../src/flux-angular')
describe('FLUX-ANGULAR', function() {
describe('.store', function() {
let $scope, $rootScope, flux, fluxProvider, cb, MyStore, MyStoreB, $browser
function initialize(options) {
options = options || {}
angular
.module('test', ['flux'])
.config(function(_fluxProvider_) {
fluxProvider = _fluxProvider_
fluxProvider.setImmutableDefaults({ asynchronous: false })
if (angular.isDefined(options.useEvalAsync)) {
fluxProvider.useEvalAsync(options.useEvalAsync)
}
})
.store('MyStore', function() {
return {
initialize: function() {
this.state = this.immutable({
items: [],
name: 'test',
})
},
handlers: {
addItem: 'addItem',
setName: 'setName',
},
addItem: function(payload) {
this.state.push('items', payload.item)
},
setName: function(payload) {
this.state.set('name', payload.name)
},
exports: {
getItems: function() {
return this.state.get('items')
},
getFirstItem: function() {
return this.exports.getItems()[0]
},
get items() {
return this.state.get('items')
},
get name() {
return this.state.get('name')
},
},
}
})
.store('MyStoreB', function() {
return {
initialize: function() {
this.state = this.immutable({ items: [] })
},
handlers: {
addItem: 'addItem',
addItemWithBadWaitFor: 'addItemWithBadWaitFor',
},
addItem: function(payload) {
this.waitFor('MyStore', function() {
this.state.push('items', payload.item)
})
},
addItemWithBadWaitFor: function(payload) {
this.waitFor('NotAStore', function() {
this.state.push('items', payload.item)
})
},
exports: {
getItems: function() {
return this.state.get('items')
},
},
}
})
.store('MyStoreC', function() {
return {
initialize: function() {
this.state = this.immutable({ items: [] }, { asynchronous: true })
},
exports: {},
}
})
angular.mock.module('test')
}
describe('with default fluxProvider options', function() {
beforeEach(function() {
initialize()
})
beforeEach(inject(function(
_$rootScope_,
_MyStore_,
_MyStoreB_,
_flux_,
_$browser_
) {
$rootScope = _$rootScope_
MyStore = _MyStore_
MyStoreB = _MyStoreB_
flux = _flux_
$browser = _$browser_
spyOn($rootScope, '$evalAsync').and.callThrough()
$scope = $rootScope.$new()
cb = jasmine.createSpy('callback')
}))
describe('initializing state', function() {
it('should not expose the private tree property', function() {
expect(MyStore.__tree).toBeUndefined()
})
it('should not error if initialize is called again to reset the state', function() {
flux.dispatch('addItem', { item: 'foo' })
expect(MyStore.items.length).toEqual(1)
flux.dispatcher.storeInstances.MyStore.initialize()
expect(MyStore.items.length).toEqual(0)
})
it('should not lose any event bindings if it is initialized again', function() {
$scope.$listenTo(MyStore, cb)
flux.dispatcher.storeInstances.MyStore.initialize()
flux.dispatch('addItem', { item: 'foo' })
expect(cb.calls.count()).toEqual(3) // once for initialization, once for state reset, and once for addItem
})
})
describe('accessors', function() {
it('should expose immutable data', function() {
expect(Object.isFrozen(MyStore.items)).toBe(true)
})
it('should allow both getter and function accessors', function() {
expect(MyStore.items.length).toEqual(0)
expect(MyStore.getItems().length).toEqual(0)
})
it('should export identical object identities even when accessors are different', function() {
expect(MyStore.getItems()).toBe(MyStore.items)
})
it('should bind export methods to the store instance', function() {
flux.dispatch('addItem', { item: 'foo' })
expect(MyStore.getFirstItem()).toEqual('foo')
})
})
describe('handlers', function() {
it('should call the correct handler with the payload', function() {
flux.dispatch('addItem', { item: 'foo' })
expect(MyStore.items.length).toEqual(1)
expect(MyStore.items[0]).toEqual('foo')
})
})
describe('scope event listeners', function() {
beforeEach(function() {
fluxProvider.useEvalAsync(true) // set to true because default is to turn it off for tests
})
it('should have a $listenTo method', function() {
expect($scope.$listenTo).toBeDefined()
})
it('should invoke the callback immediately upon setting up the store so that state is available to angular as it is initializing', function() {
$scope.$listenTo(MyStore, cb)
expect($scope.$evalAsync.calls.count()).toEqual(0)
expect(cb.calls.count()).toEqual(1)
expect(cb.calls.argsFor(0)[0]).toEqual({})
})
it('should call $evalAsync and the callback when state is changed on any part of the tree', function() {
$scope.$listenTo(MyStore, cb)
cb.calls.reset()
flux.dispatch('addItem', { item: 'foo' })
expect($scope.$evalAsync.calls.count()).toEqual(1)
expect(cb.calls.count()).toEqual(0)
$browser.defer.flush()
expect(cb.calls.count()).toEqual(1)
flux.dispatch('setName', { name: 'bar' })
expect($scope.$evalAsync.calls.count()).toEqual(2)
$browser.defer.flush()
expect(cb.calls.count()).toEqual(2)
})
it('should call the callback if a specific cursor is listened to and changed', function() {
$scope.$listenTo(MyStore, ['items'], cb)
cb.calls.reset()
flux.dispatch('addItem', { item: 'foo' })
expect($scope.$evalAsync.calls.count()).toEqual(1)
$browser.defer.flush()
expect(cb.calls.count()).toEqual(1)
flux.dispatch('setName', { name: 'bar' })
expect($scope.$evalAsync.calls.count()).toEqual(1)
expect(cb.calls.count()).toEqual(1)
})
it('should remove the listener when the scope is destroyed', function() {
$scope.$listenTo(MyStore, ['items'], cb)
cb.calls.reset()
// need to keep a ref to evalAsync since it is removed when the scope is destroyed
const evalAsync = $scope.$evalAsync
$scope.$destroy()
flux.dispatch('addItem', { item: 'foo' })
expect(evalAsync.calls.count()).toEqual(0)
expect(cb.calls.count()).toEqual(0)
})
})
describe('flux event listeners', function() {
beforeEach(function() {
fluxProvider.useEvalAsync(true) // set to true because default is to turn it off for tests
})
it('should have a listenTo method', function() {
expect(flux.listenTo).toBeDefined()
})
it('should NOT invoke the callback immediately upon setting up the listener', function() {
flux.listenTo(MyStore, cb)
expect($rootScope.$evalAsync.calls.count()).toEqual(0)
expect(cb.calls.count()).toEqual(0)
})
it('should call $evalAsync with the callback when state is changed on any part of the tree', function() {
flux.listenTo(MyStore, cb)
flux.dispatch('addItem', { item: 'foo' })
expect($rootScope.$evalAsync.calls.count()).toEqual(1)
expect(cb.calls.count()).toEqual(0)
$browser.defer.flush()
expect(cb.calls.count()).toEqual(1)
flux.dispatch('setName', { name: 'bar' })
expect($rootScope.$evalAsync.calls.count()).toEqual(2)
$browser.defer.flush()
expect(cb.calls.count()).toEqual(2)
})
it('should call the callback when a specific cursor is listened to and changed', function() {
flux.listenTo(MyStore, ['items'], cb)
flux.dispatch('addItem', { item: 'foo' })
expect($rootScope.$evalAsync.calls.count()).toEqual(1)
$browser.defer.flush()
expect(cb.calls.count()).toEqual(1)
flux.dispatch('setName', { name: 'bar' })
expect($rootScope.$evalAsync.calls.count()).toEqual(1)
expect(cb.calls.count()).toEqual(1)
})
it('should remove the listener when the returned callback is called', function() {
const unsubscribe = flux.listenTo(MyStore, ['items'], cb)
unsubscribe()
flux.dispatch('addItem', { item: 'foo' })
expect($rootScope.$evalAsync.calls.count()).toEqual(0)
expect(cb.calls.count()).toEqual(0)
})
})
describe('waiting for other stores', function() {
it('should wait for other store defined to finish first', function() {
$scope.$listenTo(MyStoreB, function() {
cb('MyStoreB')
})
$scope.$listenTo(MyStore, function() {
cb('MyStore')
})
cb.calls.reset()
flux.dispatch('addItem', { item: 'test' })
expect(cb.calls.argsFor(0)[0]).toEqual('MyStore')
expect(cb.calls.argsFor(1)[0]).toEqual('MyStoreB')
})
it('should throw an error when store waited for is not injected', function() {
expect(
flux.dispatch.bind(flux, 'addItemWithBadWaitFor', 'foo')
).toThrow()
})
})
describe('options', function() {
it('should allow local options to override immutable defaults', inject(function(
MyStoreC
) {
// eslint-disable-line no-unused-vars
expect(
flux.dispatcher.storeInstances.MyStore.state.options.asynchronous
).toBe(false)
expect(
flux.dispatcher.storeInstances.MyStoreC.state.options.asynchronous
).toBe(true)
}))
})
})
describe('when useEvalAsync = false', function() {
let $rootScope, $scope, MyStore, cb, flux
beforeEach(function() {
initialize({ useEvalAsync: false })
})
beforeEach(inject(function(_$rootScope_, _MyStore_, _flux_) {
$rootScope = _$rootScope_
$scope = $rootScope.$new()
MyStore = _MyStore_
flux = _flux_
spyOn($rootScope, '$evalAsync').and.callThrough()
cb = jasmine.createSpy('callback')
}))
it('should call the callback when $listenTo is first attached', function() {
$scope.$listenTo(MyStore, cb)
expect(cb.calls.count()).toEqual(1)
expect($scope.$evalAsync.calls.count()).toEqual(0)
})
describe('after first attachment', function() {
beforeEach(function() {
$scope.$listenTo(MyStore, cb)
cb.calls.reset()
})
it('should call the callback when state is changed on any part of the tree', function() {
flux.dispatch('addItem', { item: 'foo' })
expect(cb.calls.count()).toEqual(1)
flux.dispatch('setName', { name: 'bar' })
expect(cb.calls.count()).toEqual(2)
expect($scope.$evalAsync.calls.count()).toEqual(0)
})
it('should group multiple callback calls within a short timeframe into one $apply', function() {
let cb2 = jasmine.createSpy('callback2')
$scope.$listenTo(MyStore, cb2)
cb2.calls.reset()
flux.dispatch('setName', { name: 'bar ' })
expect(cb.calls.count()).toEqual(1)
expect(cb2.calls.count()).toEqual(1)
expect($scope.$evalAsync.calls.count()).toEqual(0)
})
})
})
})
})