Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
73 lines (61 loc) · 2.12 KB
/
Copy pathtest.js
File metadata and controls
73 lines (61 loc) · 2.12 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
70
71
72
73
/* global afterEach test expect */
import createStore from 'atom'
import { createContext, render } from 'preact'
import { useContext } from 'preact/hooks'
import { useMappedState } from './index'
const store = createStore([], { count: 0, other: 'a' })
const Context = createContext(store)
afterEach(() => {
store.setState({ count: 0 })
document.body.innerHTML = ''
})
test('useMappedState exports', () => {
expect(typeof useMappedState).toBe('function')
})
test('useMappedState should render PassedComponent', (done) => {
const Stateful = () => {
const store = useContext(Context)
const { count } = useMappedState(store, ({ count }) => ({ count }))
return (
<p>Count: {count}</p>
)
}
// Wait for store to update
const listener = () => {
// Wait for React to re-render with updated state
render(<Context.Provider value={store}><Stateful /></Context.Provider>, document.body)
setTimeout(() => {
expect(document.body.innerHTML).toBe('<p>Count: 1</p>')
done()
}, 2000)
store.unsubscribe(listener)
}
store.subscribe(listener)
render(<Context.Provider value={store}><Stateful /></Context.Provider>, document.body)
expect(document.body.innerHTML).toBe('<p>Count: 0</p>')
store.setState({ count: 1 })
})
test('useMappedState can be used more than once per Component', (done) => {
const Stateful = () => {
const store = useContext(Context)
const { count } = useMappedState(store, ({ count }) => ({ count }))
const { other } = useMappedState(store, ({ other }) => ({ other }))
return (
<p>{count}/{other}</p>
)
}
// Wait for store to update
const listener = () => {
// Wait for React to re-render with updated state
render(<Context.Provider value={store}><Stateful /></Context.Provider>, document.body)
setTimeout(() => {
expect(document.body.innerHTML).toBe('<p>1/a</p>')
done()
}, 2000)
store.unsubscribe(listener)
}
store.subscribe(listener)
render(<Context.Provider value={store}><Stateful /></Context.Provider>, document.body)
expect(document.body.innerHTML).toBe('<p>0/a</p>')
store.setState({ count: 1 })
})