Skip to content

Commit 7f39a48

Browse files
committed
test history
1 parent e62adc8 commit 7f39a48

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

lib/__tests__/react-most-test.jsx

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const CounterView = props=> (
1010
<span className="count">{props.count}</span>
1111
<span className="wrapperProps">{props.wrapperProps}</span>
1212
<span className="overwritedProps">{props.overwritedProps}</span>
13+
<span className="backward" onClick={props.history.backward}>-</span>
14+
<span className="forward" onClick={props.history.forward}>+</span>
1315
</div>
1416
)
1517

@@ -50,11 +52,12 @@ describe('react-most', () => {
5052
let counter = TestUtils.findRenderedComponentWithType(counterWrapper, Counter)
5153

5254
do$([()=>counter.actions.inc(),
53-
()=>counter.actions.inc()])
55+
()=>counter.actions.fromPromise(Promise.resolve({type:'inc'})),
56+
()=>counter.actions.fromEvent({type:'inc'})])
5457

5558
return historyStreamOf(counter)
56-
.take$(2)
57-
.then(state=>expect(state.count).toEqual(2))
59+
.take$(3)
60+
.then(state=>expect(state.count).toEqual(3))
5861
})
5962

6063
it('sink can also generate intent', ()=> {
@@ -134,4 +137,41 @@ describe('react-most', () => {
134137
})
135138
})
136139
});
140+
141+
describe('history', ()=>{
142+
it('can undo', ()=> {
143+
let counterWrapper = TestUtils.renderIntoDocument(
144+
<Most >
145+
<Counter history={true} />
146+
</Most>
147+
)
148+
let counter = TestUtils.findRenderedComponentWithType(counterWrapper, Counter)
149+
return do$([
150+
()=>counter.actions.inc(),
151+
()=>counter.actions.inc(),
152+
()=>counter.actions.inc()
153+
]).then(()=>{
154+
let backward = TestUtils.findRenderedDOMComponentWithClass(counterWrapper, 'backward')
155+
TestUtils.Simulate.click(backward)
156+
TestUtils.Simulate.click(backward)
157+
TestUtils.Simulate.click(backward)
158+
}).then(()=>{
159+
let count = TestUtils.findRenderedDOMComponentWithClass(counterWrapper, 'count')
160+
expect(count.textContent).toBe('1')
161+
}).then(()=>{
162+
let forward = TestUtils.findRenderedDOMComponentWithClass(counterWrapper, 'forward')
163+
TestUtils.Simulate.click(forward)
164+
}).then(()=>{
165+
let count = TestUtils.findRenderedDOMComponentWithClass(counterWrapper, 'count')
166+
expect(count.textContent).toBe('2')
167+
}).then(()=>{
168+
let forward = TestUtils.findRenderedDOMComponentWithClass(counterWrapper, 'forward')
169+
TestUtils.Simulate.click(forward)
170+
TestUtils.Simulate.click(forward)
171+
}).then(()=>{
172+
let count = TestUtils.findRenderedDOMComponentWithClass(counterWrapper, 'count')
173+
expect(count.textContent).toBe('3')
174+
})
175+
})
176+
})
137177
})

lib/react-most.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function connect(main, initprops={}) {
4646
}
4747
else if(sinks[name] instanceof Function){
4848
_actions[name] = (...args)=>{
49-
return this.context[intentStream].send(sinks[name].apply(null, args));
49+
return this.context[intentStream].send(sinks[name].apply(this, args));
5050
}
5151
}
5252
}
@@ -60,7 +60,7 @@ export function connect(main, initprops={}) {
6060
}
6161
componentDidMount(){
6262
this.context[flatObserve](this.actionsSinks, (action)=>{
63-
if(action instanceof Function)
63+
if(action instanceof Function) {
6464
this.setState((prevState, props)=>{
6565
let newState = action.call(this, prevState,props);
6666
if(initprops.history && newState != prevState){
@@ -69,8 +69,10 @@ export function connect(main, initprops={}) {
6969
}
7070
return newState;
7171
});
72-
else
72+
} else {
73+
/* istanbul ignore next */
7374
console.warn('action', action,'need to be a Function which map from current state to new state');
75+
}
7476
});
7577
}
7678
render() {
@@ -88,6 +90,7 @@ let Most = React.createClass({
8890
let engineClass = this.props && this.props.engine || mostEngine
8991
let engine = engineClass();
9092
// TODO: add support for ReactiveX
93+
/* istanbul ignore next */
9194
if(process.env.NODE_ENV==='debug') {
9295
if(engineClass == mostEngine) {
9396
engine.intentStream.timestamp()

0 commit comments

Comments
 (0)