-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
88 lines (72 loc) · 2.42 KB
/
test.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
"use strict"
const Rx = require('rx')
const test = require('tape')
const invariant = require('invariant')
const curry = require('ramda/src/curry')
const { createStore } = require('./lib')
const eq = (x) => (y) => x === y
const always = (x) => ( ) => x
const counter = curry((initialCount, actions$) => {
invariant(Number.isInteger(initialCount), "counter requires a numeric initialCount parameter")
const increment$ = actions$.filter(eq('increment'))
const decrement$ = actions$.filter(eq('decrement'))
return Rx.Observable.merge(
increment$.map(always(1)),
decrement$.map(always(-1))
)
.scan((total, x) => total + x, initialCount)
.startWith(initialCount)
})
test('counter responds to increment and decrement', (assert) => {
let initialCount = 0
const store = createStore(counter, initialCount)
assert.plan(5)
store.state$.subscribe((state) => {
assert.equal(state, initialCount)
if(initialCount < 3) {
initialCount++
} else {
initialCount--
}
})
store.dispatch('increment')
store.dispatch('increment')
store.dispatch('increment')
store.dispatch('decrement')
})
const React = require('react')
const { PropTypes, createElement } = React
const { div } = React.DOM
const TestUtils = require('react-addons-test-utils')
const jsdom = require('jsdom')
const compose = require('recompose/compose')
const createSpy = require('recompose/createSpy')
const { Provider } = require('./lib')
const { observePropsFromStore } = require('./lib')
global.document = jsdom.jsdom('<html><body></body></html>')
global.window = document.defaultView
test('component gets props from store and rerenders when changed', (assert) => {
let initialCount = 0
let initialState = {
count: initialCount
}
const store = createStore(({count}, actions$) => ({
count: counter(count, actions$)
}), initialState)
const spy = createSpy()
const CurrentCount = compose(
observePropsFromStore(({state$}) => state$),
spy
)(({count}) => div('Current count is ' + count))
TestUtils.renderIntoDocument(
createElement(Provider, {store}, createElement(CurrentCount))
)
assert.plan(3)
assert.deepEqual(spy.getProps(), {count: initialCount})
store.dispatch('increment')
assert.deepEqual(spy.getProps(), {count: initialCount+1})
store.dispatch('increment')
store.dispatch('increment')
store.dispatch('decrement')
assert.deepEqual(spy.getProps(), {count: initialCount+1+1+1-1})
})