Skip to content

Commit 16253e4

Browse files
committed
use react-most-spec to simplify the tests
1 parent 84d7f01 commit 16253e4

File tree

4 files changed

+441
-93
lines changed

4 files changed

+441
-93
lines changed

examples/todomvc/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"ramda": "^0.22.1",
3737
"react": "^15.3.2",
3838
"react-dom": "^15.3.2",
39-
"react-most": "^0.6.5",
39+
"react-most": "^0.7.0",
4040
"rest": "^1.3.1",
4141
"union-type": "^0.3.3"
4242
},
@@ -52,6 +52,7 @@
5252
"envify": "^3.4.1",
5353
"jest-cli": "^0.7.0",
5454
"react-addons-test-utils": "^15.3.2",
55+
"react-most-spec": "^0.2.3",
5556
"watchify": "^3.6.1"
5657
},
5758
"author": "Jichao Ouyang",

examples/todomvc/src/components/MainSection.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ export default connect((intent$)=>{
3636
let lensComplete = r.lensProp('done')
3737
let lensTodo = index => r.compose(lensTodos, r.lensIndex(index))
3838
let lensTodoComplete = index => r.compose(lensTodo(index), lensComplete)
39-
let nextId = r.compose(r.inc, r.prop('id'), r.last, r.sortBy(r.prop('id')))
39+
let nextId = r.compose(r.last, r.map(x=>x.id+1), r.sortBy(r.prop('id')))
4040
let sinks$ = intent$.map(Intent.case({
4141
Add: (todo) => state=>{
42-
return r.over(lensTodos, r.append(r.assoc('id', nextId(state.todos), todo)), state)
42+
return r.over(lensTodos, r.append(r.assoc('id', nextId(state.todos)||0, todo)), state)
4343
},
4444
Edit: (todo,index) => r.set(lensTodo(index), todo),
4545
Clear: () => r.over(lensTodos, r.filter(todo=>!todo.done)),

examples/todomvc/src/components/__tests__/MainSection-spec.jsx

Lines changed: 46 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import Intent from '../../intent'
66
import MainSection from '../MainSection.jsx'
77
import Footer, {FILTER_FUNC} from '../Footer.jsx'
88
import TodoItem from '../TodoItem.jsx'
9-
import Most from '../../../../../lib/react-most'
10-
import {do$, historyStreamOf} from '../../../../../lib/test-utils'
9+
import Most from 'react-most'
10+
import {stateStreamOf, stateHistoryOf,
11+
intentStreamOf, intentHistoryOf,
12+
run, dispatch,
13+
Engine } from 'react-most-spec';
1114
import TestUtils from 'react-addons-test-utils'
1215

1316
describe('MainSection', ()=>{
@@ -66,87 +69,59 @@ describe('MainSection', ()=>{
6669

6770
describe('Behavior', ()=> {
6871
let mainSectionWrapper, mainSection, send
69-
beforeEach(()=>{
70-
mainSectionWrapper = TestUtils.renderIntoDocument(
71-
<Most>
72-
<MainSection history={true}/>
73-
</Most>
74-
)
75-
mainSection = TestUtils.findRenderedComponentWithType(mainSectionWrapper, MainSection);
76-
send = intent => mainSection.actions.fromPromise(when(intent))
77-
})
72+
7873
describe('data sink', ()=>{
74+
beforeEach(()=>{
75+
mainSectionWrapper = TestUtils.renderIntoDocument(
76+
<Most>
77+
<MainSection history={true}/>
78+
</Most>
79+
)
80+
mainSection = TestUtils.findRenderedComponentWithType(mainSectionWrapper, MainSection);
81+
})
7982
it('should render default state', ()=>{
8083
expect(mainSection.state.todos).toEqual([
8184
{id:0, text:'Loading...dadada', done:false},
8285
])
8386
})
8487
it('should get data from rest response to MainSection', ()=>{
85-
return historyStreamOf(mainSection).take$(1).then(state=>expect(state.todos).toEqual(JSON.parse(RESPONSE)))
88+
return run(stateStreamOf(mainSection),
89+
dispatch([], mainSection),
90+
[
91+
state=>expect(state.todos).toEqual(JSON.parse(RESPONSE))
92+
])
8693
})
8794
});
8895

89-
describe('edit', ()=>{
90-
it('should update todo id 0 item text', ()=>{
91-
do$([
92-
()=>send(Intent.Edit({id:0, text:'hehedayo'}, 0)),
93-
])
94-
return historyStreamOf(mainSection).take$(2).then(state=>expect(state.todos[0]).toEqual({"id": 0, "text": "hehedayo"}))
95-
})
96-
});
97-
98-
describe('clear', ()=> {
99-
it('should remove all done todos', ()=>{
100-
do$([
101-
()=>send(Intent.Edit({id:0,text:'done',done:true}, 0)),
102-
()=>send(Intent.Clear()),
103-
])
104-
return historyStreamOf(mainSection)
105-
.take$(3)
106-
.then(state=>{
107-
expect(state.todos).toEqual([{"done": false, "id": 1, "text": "Give it a Star on Github"}])
108-
})
109-
})
110-
})
111-
112-
describe('delete', ()=> {
113-
it('should remove todos id 0', ()=>{
114-
do$([
115-
()=>send(Intent.Delete(0)),
116-
])
117-
return historyStreamOf(mainSection)
118-
.take$(2)
119-
.then(state=>{
120-
expect(state.todos).toEqual([{"done": false, "id": 1, "text": "Give it a Star on Github"}])
121-
})
96+
describe('sync', ()=>{
97+
beforeEach(()=>{
98+
mainSectionWrapper = TestUtils.renderIntoDocument(
99+
<Most engine={Engine}>
100+
<MainSection history={true}/>
101+
</Most>
102+
)
103+
mainSection = TestUtils.findRenderedComponentWithType(mainSectionWrapper, MainSection);
122104
})
123-
})
124-
125-
describe('done', ()=> {
126-
it('should complete todo 0', ()=>{
127-
do$([
128-
()=>send(Intent.Done(0)),
129-
])
130-
return historyStreamOf(mainSection)
131-
.take$(2)
132-
.then(state=>{
133-
expect(state.todos).toEqual([{"done": true, "id": 0, "text": "Try React Most"}, {"done": false, "id": 1, "text": "Give it a Star on Github"}])
105+
describe('edit', ()=>{
106+
it('should update todo id 0 item text', ()=>{
107+
return dispatch([
108+
Intent.Edit({id:0, text:'heheda0'}, 0),
109+
Intent.Done(0),
110+
Intent.Clear(),
111+
Intent.Add({text:'heheda1'}),
112+
Intent.Delete(0),
113+
Intent.Filter(FILTER_FUNC['SHOW_COMPLETED']),
114+
], mainSection).then(()=>{
115+
let state = stateHistoryOf(mainSection)
116+
expect(state[0].todos[0]).toEqual({"id": 0, "text": "heheda0"})
117+
expect(state[1].todos[0]).toEqual({"id": 0, "text": "heheda0", "done": true})
118+
expect(state[2].todos).toEqual([])
119+
expect(state[3].todos[0]).toEqual({"id": 0, "text": "heheda1"})
120+
expect(state[4].todos).toEqual([])
121+
expect(state[5].filter).toEqual(FILTER_FUNC['SHOW_COMPLETED'])
134122
})
135-
})
136-
})
137-
138-
describe('click filter completed', ()=> {
139-
it('should only use SHOW_COMPLETED filter', ()=>{
140-
do$([
141-
()=>send(Intent.Edit({id:0, done:true}, 0)),
142-
()=>send(Intent.Filter(FILTER_FUNC['SHOW_COMPLETED'])),
143-
])
144-
return historyStreamOf(mainSection)
145-
.take$(3)
146-
.then(state=>{
147-
expect(state.filter).toEqual(FILTER_FUNC['SHOW_COMPLETED'])
148-
})
149-
})
123+
})
124+
});
150125
})
151126
})
152127
});

0 commit comments

Comments
 (0)