-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathreact-mixin-tests.js
195 lines (164 loc) · 4.96 KB
/
react-mixin-tests.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
var React = require('react')
var Nuclear = require('../src/main')
var toImmutable = require('../src/main').toImmutable
var testStore = new Nuclear.Store({
getInitialState() {
return toImmutable({
count: 0,
map: {
key1: 'value1',
multi: 2,
},
})
},
initialize() {
this.on('increment', state => {
var nextCount = state.get('count')
return state.set('count', ++nextCount)
})
this.on('set', (state, payload) => {
return state.setIn(['map', payload.key], toImmutable(payload.value))
})
},
})
describe('reactor.ReactMixin', () => {
var mountNode
var reactor
var countGetter = ['test', 'count']
var key1Getter = ['test', 'map', 'key1']
var key2Getter = ['test', 'map', 'key2']
var multipliedGetter = [
countGetter,
['test', 'map', 'multi'],
(count, multi) => count * multi,
]
beforeEach(() => {
reactor = new Nuclear.Reactor({
debug: true,
})
reactor.registerStores({
test: testStore,
})
})
describe('when rendering a component with the flux.ReactMixin', () => {
var component
beforeEach(() => {
mountNode = document.createElement('div')
document.body.appendChild(mountNode)
// var componentWillMountSpy = jasmine.createSpy()
var Component = React.createClass({
mixins: [reactor.ReactMixin],
getDataBindings() {
return {
count: countGetter,
multiplied: multipliedGetter,
key1: key1Getter,
key2: key2Getter,
}
},
render() {
return React.DOM.div(null, '')
},
})
component = React.render(React.createElement(Component, null), mountNode)
})
afterEach(() => {
React.unmountComponentAtNode(mountNode)
document.body.removeChild(mountNode)
})
it('should set the component initialState from `getDataBindings()`', () => {
expect(component.state.count).toBe(0)
expect(component.state.multiplied).toBe(0)
expect(component.state.key1).toBe('value1')
expect(component.state.key2).toBe(undefined)
})
it('should update the state automatically when the underyling getters change', () => {
reactor.dispatch('increment')
expect(component.state.count).toBe(1)
expect(component.state.multiplied).toBe(2)
expect(component.state.key1).toBe('value1')
expect(component.state.key2).toBe(undefined)
reactor.dispatch('set', {
key: 'key2',
value: 'value2',
})
expect(component.state.count).toBe(1)
expect(component.state.multiplied).toBe(2)
expect(component.state.key1).toBe('value1')
expect(component.state.key2).toBe('value2')
})
})
describe('when rendering a component with a getInitialState() method', () => {
var component
beforeEach(() => {
mountNode = document.createElement('div')
document.body.appendChild(mountNode)
// var componentWillMountSpy = jasmine.createSpy()
var Component = React.createClass({
mixins: [reactor.ReactMixin],
getInitialState() {
return {
foo: 'bar',
}
},
getDataBindings() {
return {
count: countGetter,
multiplied: multipliedGetter,
key1: key1Getter,
key2: key2Getter,
}
},
render() {
return React.DOM.div(null, '')
},
})
component = React.render(React.createElement(Component, null), mountNode)
})
afterEach(() => {
React.unmountComponentAtNode(mountNode)
document.body.removeChild(mountNode)
})
it('should set the component initialState from `getDataBindings()` and getInitialState', () => {
expect(component.state.foo).toBe('bar')
expect(component.state.count).toBe(0)
expect(component.state.multiplied).toBe(0)
expect(component.state.key1).toBe('value1')
expect(component.state.key2).toBe(undefined)
})
})
describe('after unmounting the component', () => {
beforeEach(() => {
mountNode = document.createElement('div')
document.body.appendChild(mountNode)
// var componentWillMountSpy = jasmine.createSpy()
var Component = React.createClass({
mixins: [reactor.ReactMixin],
getInitialState() {
return {
foo: 'bar',
}
},
getDataBindings() {
return {
count: countGetter,
multiplied: multipliedGetter,
key1: key1Getter,
key2: key2Getter,
}
},
render() {
return React.DOM.div(null, '')
},
})
React.render(React.createElement(Component, null), mountNode)
})
afterEach(() => {
document.body.removeChild(mountNode)
})
it('should unobserve all getters', () => {
React.unmountComponentAtNode(mountNode)
expect(reactor.observerState.get('observersMap').size).toBe(0)
})
})
})