forked from ucfopen/Obojobo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.test.js
More file actions
86 lines (62 loc) · 1.96 KB
/
store.test.js
File metadata and controls
86 lines (62 loc) · 1.96 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
import Store from '../../../src/scripts/common/flux/store'
import Dispatcher from '../../../src/scripts/common/flux/dispatcher'
jest.mock('../../../src/scripts/common/flux/dispatcher', () => {
return {
trigger: jest.fn(),
on: jest.fn(),
off: jest.fn()
}
})
describe('Store', () => {
test('creates new instance', () => {
const store = new Store('store-name')
expect(store.name).toBe('store-name')
})
test('init resets state', () => {
const store = new Store('store-name')
store.init()
store.state = { a: 1 }
store.init()
expect(store.state).toEqual({})
})
test('triggerChange dispatched a change event', () => {
const store = new Store('store-name')
store.triggerChange()
expect(Dispatcher.trigger).toHaveBeenCalledWith('store-name:change')
})
test('onChange listens for change event', () => {
const store = new Store('store-name')
const cb = () => {}
store.onChange(cb)
expect(Dispatcher.on).toHaveBeenCalledWith('store-name:change', cb)
})
test('offChange stops listening for change event', () => {
const store = new Store('store-name')
const cb = () => {}
store.offChange(cb)
expect(Dispatcher.off).toHaveBeenCalledWith('store-name:change', cb)
})
test('setAndTrigger updates state and triggers a change', () => {
const store = new Store('store-name')
store.init()
store.setAndTrigger({ a: 1, b: 2 })
expect(store.state).toEqual({ a: 1, b: 2 })
expect(Dispatcher.trigger).toHaveBeenCalledWith('store-name:change')
})
test('getState returns a copy of state', () => {
const store = new Store('store-name')
const state = { a: 1, b: 2 }
store.init()
store.state = state
expect(store.getState()).not.toBe(state)
expect(store.getState()).toEqual(state)
})
test('setState sets a copy of the given state', () => {
const store = new Store('store-name')
const state = { a: 1, b: 2 }
store.init()
store.setState(state)
expect(store.state).not.toBe(state)
expect(store.state).toEqual(state)
})
})