Skip to content

Commit cdad12a

Browse files
committed
WIP use sync engine
1 parent ae75b01 commit cdad12a

File tree

3 files changed

+300
-26
lines changed

3 files changed

+300
-26
lines changed

lib/__tests__/react-most-test.jsx

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,44 @@ import TestUtils from 'react-addons-test-utils';
44
import * as most from 'most';
55
import {compose} from 'ramda';
66
import Most, {connect} from '../react-most';
7-
import {do$, historyStreamOf, intentStreamOf,dispatch} from '../test-utils'
7+
import {sync,hold} from 'most-subject'
8+
import {from } from 'most'
9+
import {INTENT_STREAM, HISTORY_STREAM} from '../react-most'
10+
function getStreamOf(component, name) {
11+
return component.context[name];
12+
}
13+
14+
function historyStreamOf(component) {
15+
return getStreamOf(component, HISTORY_STREAM)
16+
}
17+
18+
function intentStreamOf(component) {
19+
return getStreamOf(component, INTENT_STREAM)
20+
}
21+
function Engine() {
22+
const intentStream = hold(2, sync()),
23+
historyStream = [],
24+
travelStream = sync();
25+
intentStream.send = intentStream.next.bind(intentStream);
26+
historyStream.send = historyStream.push.bind(historyStream);
27+
travelStream.send = travelStream.next.bind(travelStream);
28+
setTimeout(() => intentStream.complete())
29+
30+
function mergeObserve(actionsSinks, f){
31+
let subscriptions = most.mergeArray(actionsSinks)
32+
.recoverWith(e => {
33+
console.error('There is Error in your reducer:',e, e.stack)
34+
return of(x=>x)
35+
})
36+
.subscribe({
37+
next:f,
38+
error: (e)=>console.error('Something is Wrong:',e, e.stack),
39+
});
40+
return subscriptions;
41+
}
42+
historyStream.travel = travelStream;
43+
return {intentStream, mergeObserve, historyStream}
44+
}
845

946
const CounterView = React.createClass({
1047
render(){
@@ -50,36 +87,30 @@ const Counter = counterWrapper(CounterView)
5087

5188
describe('react-most', () => {
5289
describe('actions', ()=>{
53-
it('add intent to intent$ and go through sink$', ()=> {
90+
it.only('add intent to intent$ and go through sink$', ()=> {
5491
let counterWrapper = TestUtils.renderIntoDocument(
55-
<Most >
92+
<Most engine={Engine}>
5693
<Counter history={true} />
5794
</Most>
5895
)
5996
let counter = TestUtils.findRenderedComponentWithType(counterWrapper, Counter)
60-
61-
do$([()=>counter.actions.inc(),
62-
()=>counter.actions.fromPromise(Promise.resolve({type:'inc'})),
63-
()=>counter.actions.fromEvent({type:'inc'})])
64-
65-
return historyStreamOf(counter)
66-
.take$(3)
67-
.then(state=>expect(state.count).toEqual(3))
97+
counter.actions.inc()
98+
counter.actions.inc()
99+
counter.actions.inc()
100+
expect(historyStreamOf(counter)[2].count).toBe(3)
68101
})
69102

70-
it('sink can also generate intent', ()=> {
103+
it.only('sink can also generate intent', ()=> {
71104
let counterWrapper = TestUtils.renderIntoDocument(
72-
<Most >
105+
<Most engine={Engine}>
73106
<Counter history={true} />
74107
</Most>
75108
)
76109
let counter = TestUtils.findRenderedComponentWithType(counterWrapper, Counter)
77110

78-
do$([()=>counter.actions.dec()])
79-
80-
return intentStreamOf(counter)
81-
.take(2).observe(_=>_)
82-
.then(intent=>expect(intent.type).toEqual('dec triggered'))
111+
counter.actions.dec()
112+
return intentStreamOf(counter).reduce((acc,x)=>(acc.push(x), acc), [])
113+
.then(intent=>expect(intent[1].type).toEqual('dec triggered'))
83114
})
84115

85116
it('props will overwirte components default props', ()=>{
@@ -90,10 +121,9 @@ describe('react-most', () => {
90121
)
91122
let counter = TestUtils.findRenderedComponentWithType(counterWrapper, Counter)
92123

93-
do$([()=>counter.actions.inc()])
94-
return historyStreamOf(counter)
95-
.take$(1)
96-
.then(state=>expect(state.count).toBe(10))
124+
return run(historyStreamOf(counter),
125+
dispatch([{type:'inc'}], counter),
126+
[(value)=>expect(value.count).toBe(10)])
97127
})
98128

99129
it('props that not overlap with views defaultProps can not be changed', ()=>{

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
"testWDebugger": "node --harmony $(which bugger) ./node_modules/jest-cli/bin/jest.js --runInBand"
2727
},
2828
"dependencies": {
29+
"jest": "^18.0.0",
2930
"most": "^1.1.1",
3031
"most-subject": "^5.2.0",
3132
"ramda": "^0.23.0",
3233
"react": "^15.3.2",
34+
"react-most-spec": "v0.1.2",
3335
"rxjs": "^5.0.0-rc.4"
3436
},
3537
"jest": {
@@ -56,6 +58,7 @@
5658
"lodash": "^4.0.0",
5759
"react-addons-test-utils": "^15.2.0",
5860
"react-dom": "^15.3.2",
61+
"react-most-spec": "^0.1.1",
5962
"redux": "^3.0.4"
6063
},
6164
"author": "Jichao Ouyang",

0 commit comments

Comments
 (0)