forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.tsx
More file actions
177 lines (143 loc) · 3.56 KB
/
index.test.tsx
File metadata and controls
177 lines (143 loc) · 3.56 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { Overmind, createOvermindMock, IContext } from 'overmind'
import * as React from 'react'
import * as renderer from 'react-test-renderer'
import { Provider, createStateHook } from './'
describe('React', () => {
test('should allow using hooks', () => {
let renderCount = 0
const doThis = ({ state }: Context) => {
state.foo = 'bar2'
}
const state = {
foo: 'bar',
}
const actions = {
doThis,
}
const config = {
state,
actions,
}
type Context = IContext<{
state: typeof state
actions: typeof actions
}>
const app = new Overmind(config)
const useState = createStateHook<Context>()
const FooComponent: React.FunctionComponent = () => {
const state = useState()
renderCount++
return <h1>{state.foo}</h1>
}
const tree = renderer
.create(
<Provider value={app}>
<FooComponent />
</Provider>
)
.toJSON()
expect(renderCount).toBe(1)
renderer.act(() => {
app.actions.doThis()
})
expect(renderCount).toBe(2)
expect(tree).toMatchSnapshot()
})
test('should allow using hooks with scoped tracking', () => {
let renderCount = 0
const doThis = ({ state }: Context) => {
state.foo.push({ foo: 'bar2' })
}
const doThat = ({ state }: Context) => {
state.foo[0].foo = 'bar3'
}
const state = {
foo: [{ foo: 'bar' }],
}
const actions = {
doThis,
doThat,
}
const config = {
state,
actions,
}
type Context = IContext<{
state: typeof state
actions: typeof actions
}>
const app = new Overmind(config)
const useState = createStateHook<Context>()
const FooComponent: React.FunctionComponent = () => {
const state = useState((state) => state.foo[0])
renderCount++
return <h1>{state.foo}</h1>
}
const tree = renderer
.create(
<Provider value={app}>
<FooComponent />
</Provider>
)
.toJSON()
expect(renderCount).toBe(1)
renderer.act(() => {
app.actions.doThis()
})
expect(renderCount).toBe(1)
renderer.act(() => {
app.actions.doThat()
})
expect(renderCount).toBe(2)
// This is not showing the expected result, but logging the rendering does, so must be the
// library messing it up
expect(tree).toMatchSnapshot()
})
test('should allow using mocked Overmind', () => {
let renderCount = 0
const doThis = ({ state }: Context) => {
state.foo = 'bar2'
}
const state = {
foo: 'bar',
}
const actions = {
doThis,
}
const config = {
state,
actions,
}
type Context = IContext<{
state: typeof state
actions: typeof actions
}>
const useState = createStateHook<Context>()
const FooComponent: React.FunctionComponent = () => {
const state = useState()
renderCount++
return <h1>{state.foo}</h1>
}
const mock = createOvermindMock(config)
const tree = renderer
.create(
<Provider value={mock}>
<FooComponent />
</Provider>
)
.toJSON()
expect(renderCount).toBe(1)
expect(tree).toMatchSnapshot()
})
test('should throw an error without provider', () => {
expect.assertions(1)
const useState = createStateHook()
const FooComponent = () => {
const state = useState()
return <h1>{(state as any).foo}</h1>
}
expect(() => {
renderer.create(<FooComponent />).toJSON()
}).toThrow(Error)
})
})