Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
69 lines (58 loc) · 1.87 KB
/
Copy pathtest.js
File metadata and controls
69 lines (58 loc) · 1.87 KB
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
/* global getProvider afterEach test expect */
import { render } from 'preact'
import createStore from 'atom'
import withState from './index.js'
const Provider = getProvider()
const store = createStore([], { count: 0 })
afterEach(() => {
store.setState({ count: 0 })
document.body.innerHTML = ''
})
test('withState exports', () => {
expect(typeof withState).toBe('function')
})
test('withState should render PassedComponent', () => {
const Stateful = withState({
mapper: state => ({ count: state.count })
})(({ count }) =>
<div>
<p>Count: {count}</p>
</div>
)
render(<Provider store={store}><Stateful /></Provider>, document.body)
expect(document.body.innerHTML).toBe('<div><p>Count: 0</p></div>')
})
test('withState should accept mapper function as only argument', () => {
const Stateful = withState(state => ({ count: state.count }))(({ count }) =>
<div>
<p>Count: {count}</p>
</div>
)
render(<Provider store={store}><Stateful /></Provider>, document.body)
expect(document.body.innerHTML).toBe('<div><p>Count: 0</p></div>')
})
test('PassedComponent should update with props change', (done) => {
const Child = withState(({ x }) => ({ x }))(({ count }) =>
<p>Count: {count}</p>
)
const Parent = withState(({ count }) => ({ count }))(({ count }) =>
<div>
<Child count={count} />
</div>
)
// Wait for store to update
const listener = () => {
// Wait for React to re-render with updated state
setTimeout(() => {
expect(document.body.innerHTML).toBe('<div><p>Count: 1</p></div>')
store.unsubscribe(listener)
done()
}, 1000)
}
store.subscribe(listener)
// Do initial render
render(<Provider store={store}><Parent /></Provider>, document.body)
expect(document.body.innerHTML).toBe('<div><p>Count: 0</p></div>')
// Update state so listener is called
store.setState({ count: 1 })
})