Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
84 lines (72 loc) · 2.41 KB
/
Copy pathtest.js
File metadata and controls
84 lines (72 loc) · 2.41 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
74
75
76
77
78
79
80
81
82
83
84
/* global afterEach test expect */
import createStore from 'atom'
import { createContext, render } from 'preact'
import { useContext } from 'preact/hooks'
import { useStorePath } from './use-store-path'
const store = createStore([], {
count: 0,
nested: { items: { value: 'a', untouched: 'b' } }
})
const Context = createContext(store)
afterEach(() => {
store.setState({ count: 0 })
document.body.innerHTML = ''
})
test('useStorePath exports', () => {
expect(typeof useStorePath).toBe('function')
})
test('useStorePath should return value, set value, and update', (done) => {
let setter
const Stateful = () => {
const store = useContext(Context)
const [count, setCount] = useStorePath(store, 'count')
setter = () => setCount(count + 1)
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>')
setter()
})
test('useStorePath can be used more than once per Component', (done) => {
let setter
const Stateful = () => {
const store = useContext(Context)
const [count] = useStorePath(store, 'count')
const [value, setValue] = useStorePath(store, 'nested.items.value')
setter = () => setValue('changed')
return (
<p>{count}/{value}</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/changed</p>')
expect(store.getState().nested.items.untouched).toBe('b')
expect(store.getState().nested.items.value).toBe('changed')
expect(store.getState().count).toBe(1)
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 })
setter()
})