Skip to content

Commit fceaeff

Browse files
committed
Add react-mixin-tests
1 parent 0f5f18a commit fceaeff

File tree

5 files changed

+10625
-10393
lines changed

5 files changed

+10625
-10393
lines changed

NEXT.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
# TODO for 0.6.0
22

3-
- [ ] Flux chat example written in NuclearJS (only need README)
3+
## Essential for 0.6.0
4+
- [x] Flux chat example written in NuclearJS (only need README)
45
- [x] Implement `flux.ReactMixin` as part of NuclearJS
5-
- [ ] Test `flux.ReactMixin`
6-
- [ ] Document util methods such as `toImmutable`
7-
- [ ] Implement code coverage and put badge on README
8-
- [ ] Explain `getters` with diagram as functional lenses
6+
- [x] Test `flux.ReactMixin`
7+
- [ ] Create release documentation with API breaking changes and migration guide
8+
- [ ] Dedupe docs and make sure they are organized
9+
10+
## Planned for 0.6.1
911
- [ ] Document module pattern and prescribed way of testing NuclearJS modules
12+
- [ ] Explain `getters` with diagram as functional lenses
1013
- [ ] Polyfill all lodash methods in `utils` taken from 0.5.0 (only need `deepClone` and `isObject`)
14+
- [ ] Document util methods such as `toImmutable`
15+
- [ ] Implement code coverage and put badge on README

dist/nuclear.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/flux-chat/bundle.js

+10,416-10,386
Large diffs are not rendered by default.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"karma-chrome-launcher": "^0.1.5",
4343
"webpack-dev-server": "^1.6.6",
4444
"karma-webpack": "^1.3.1",
45-
"karma-jasmine-html-reporter": "^0.1.5"
45+
"karma-jasmine-html-reporter": "^0.1.5",
46+
"react": "^0.13.2"
4647
}
4748
}

tests/react-mixin-tests.js

+196
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
var React = require('react')
2+
var Nuclear = require('../src/main')
3+
var toImmutable = require('../src/main').toImmutable
4+
5+
var testStore = new Nuclear.Store({
6+
getInitialState() {
7+
return toImmutable({
8+
count: 0,
9+
10+
map: {
11+
key1: 'value1',
12+
multi: 2,
13+
}
14+
})
15+
},
16+
17+
initialize() {
18+
this.on('increment', state => {
19+
var nextCount = state.get('count')
20+
return state.set('count', ++nextCount)
21+
})
22+
23+
this.on('set', (state, payload) => {
24+
return state.setIn(['map', payload.key], toImmutable(payload.value))
25+
})
26+
}
27+
})
28+
29+
describe('reactor.ReactMixin', () => {
30+
var mountNode
31+
var reactor
32+
var countGetter = ['test', 'count']
33+
var key1Getter = ['test', 'map', 'key1']
34+
var key2Getter = ['test', 'map', 'key2']
35+
var multipliedGetter = [
36+
countGetter,
37+
['test', 'map', 'multi'],
38+
(count, multi) => count * multi
39+
]
40+
41+
beforeEach(() => {
42+
reactor = new Nuclear.Reactor({
43+
debug: true,
44+
})
45+
reactor.registerStores({
46+
test: testStore
47+
})
48+
})
49+
50+
51+
describe("when rendering a component with the flux.ReactMixin", () => {
52+
var component
53+
beforeEach(() => {
54+
mountNode = document.createElement('div')
55+
document.body.appendChild(mountNode)
56+
//var componentWillMountSpy = jasmine.createSpy()
57+
var Component = React.createClass({
58+
mixins: [reactor.ReactMixin],
59+
60+
getDataBindings() {
61+
return {
62+
count: countGetter,
63+
multiplied: multipliedGetter,
64+
key1: key1Getter,
65+
key2: key2Getter,
66+
}
67+
},
68+
69+
render() {
70+
return React.DOM.div(null, '')
71+
},
72+
})
73+
74+
component = React.render(React.createElement(Component, null), mountNode)
75+
})
76+
77+
afterEach(() => {
78+
React.unmountComponentAtNode(mountNode)
79+
document.body.removeChild(mountNode)
80+
})
81+
82+
it("should set the component initialState from `getDataBindings()`", () => {
83+
expect(component.state.count).toBe(0)
84+
expect(component.state.multiplied).toBe(0)
85+
expect(component.state.key1).toBe('value1')
86+
expect(component.state.key2).toBe(undefined)
87+
})
88+
89+
it("should update the state automatically when the underyling getters change", () => {
90+
reactor.dispatch('increment')
91+
92+
expect(component.state.count).toBe(1)
93+
expect(component.state.multiplied).toBe(2)
94+
expect(component.state.key1).toBe('value1')
95+
expect(component.state.key2).toBe(undefined)
96+
97+
reactor.dispatch('set', {
98+
key: 'key2',
99+
value: 'value2',
100+
})
101+
102+
expect(component.state.count).toBe(1)
103+
expect(component.state.multiplied).toBe(2)
104+
expect(component.state.key1).toBe('value1')
105+
expect(component.state.key2).toBe('value2')
106+
})
107+
})
108+
109+
describe("when rendering a component with a getInitialState() method", () => {
110+
var component
111+
beforeEach(() => {
112+
mountNode = document.createElement('div')
113+
document.body.appendChild(mountNode)
114+
//var componentWillMountSpy = jasmine.createSpy()
115+
var Component = React.createClass({
116+
mixins: [reactor.ReactMixin],
117+
118+
getInitialState() {
119+
return {
120+
foo: 'bar',
121+
}
122+
},
123+
124+
getDataBindings() {
125+
return {
126+
count: countGetter,
127+
multiplied: multipliedGetter,
128+
key1: key1Getter,
129+
key2: key2Getter,
130+
}
131+
},
132+
133+
render() {
134+
return React.DOM.div(null, '')
135+
},
136+
})
137+
138+
component = React.render(React.createElement(Component, null), mountNode)
139+
})
140+
141+
afterEach(() => {
142+
React.unmountComponentAtNode(mountNode)
143+
document.body.removeChild(mountNode)
144+
})
145+
146+
it("should set the component initialState from `getDataBindings()` and getInitialState", () => {
147+
expect(component.state.foo).toBe('bar')
148+
expect(component.state.count).toBe(0)
149+
expect(component.state.multiplied).toBe(0)
150+
expect(component.state.key1).toBe('value1')
151+
expect(component.state.key2).toBe(undefined)
152+
})
153+
})
154+
155+
describe("after unmounting the component", () => {
156+
var component
157+
beforeEach(() => {
158+
mountNode = document.createElement('div')
159+
document.body.appendChild(mountNode)
160+
//var componentWillMountSpy = jasmine.createSpy()
161+
var Component = React.createClass({
162+
mixins: [reactor.ReactMixin],
163+
164+
getInitialState() {
165+
return {
166+
foo: 'bar',
167+
}
168+
},
169+
170+
getDataBindings() {
171+
return {
172+
count: countGetter,
173+
multiplied: multipliedGetter,
174+
key1: key1Getter,
175+
key2: key2Getter,
176+
}
177+
},
178+
179+
render() {
180+
return React.DOM.div(null, '')
181+
},
182+
})
183+
184+
component = React.render(React.createElement(Component, null), mountNode)
185+
})
186+
187+
afterEach(() => {
188+
document.body.removeChild(mountNode)
189+
})
190+
191+
it("should unobserve all getters", () => {
192+
React.unmountComponentAtNode(mountNode)
193+
expect(reactor.__changeObserver.__observers.length).toBe(0)
194+
})
195+
})
196+
})

0 commit comments

Comments
 (0)