Skip to content

Commit 16c07fb

Browse files
feat(overmind): make overmind mock compatible with Provider
1 parent 6a3cf80 commit 16c07fb

File tree

7 files changed

+141
-20
lines changed

7 files changed

+141
-20
lines changed

package-lock.json

Lines changed: 6 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
"raw-loader": "0.5.1",
8888
"react": "^16.8.1",
8989
"react-dom": "^16.8.1",
90-
"react-test-renderer": "16.4.1",
90+
"react-test-renderer": "^16.8.1",
9191
"repo-cooker": "^6.3.0",
9292
"rimraf": "2.6.2",
9393
"source-map-loader": "0.2.4",

packages/node_modules/overmind-react/src/__snapshots__/index.test.tsx.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ exports[`React should allow using component as normal, even when not connected 2
1212
</h1>
1313
`;
1414

15+
exports[`React should allow using hooks 1`] = `
16+
<h1>
17+
bar
18+
</h1>
19+
`;
20+
21+
exports[`React should allow using mocked Overmind 1`] = `
22+
<h1>
23+
bar
24+
</h1>
25+
`;
26+
1527
exports[`React should connect actions and state to class components 1`] = `
1628
<h1>
1729
bar

packages/node_modules/overmind-react/src/index.test.tsx

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Overmind, IAction } from 'overmind'
1+
import { Overmind, IAction, createOvermindMock } from 'overmind'
22
import * as React from 'react'
33
import * as renderer from 'react-test-renderer'
44
import { IConnect, createConnect, createHook, Provider } from './'
@@ -185,4 +185,105 @@ describe('React', () => {
185185

186186
expect(renderCount).toBe(2)
187187
})
188+
test('should allow using hooks', () => {
189+
let renderCount = 0
190+
191+
const doThis: Action = ({ state }) => {
192+
state.foo = 'bar2'
193+
}
194+
const config = {
195+
state: {
196+
foo: 'bar',
197+
},
198+
actions: {
199+
doThis,
200+
},
201+
}
202+
203+
type IConfig = {
204+
state: {
205+
foo: typeof config.state.foo
206+
}
207+
actions: {
208+
doThis: typeof doThis
209+
}
210+
}
211+
212+
const app = new Overmind(config)
213+
214+
interface Action<Input = void> extends IAction<IConfig, Input> {}
215+
216+
const useOvermind = createHook<typeof app>()
217+
218+
const FooComponent: React.FunctionComponent = () => {
219+
const { state } = useOvermind()
220+
renderCount++
221+
222+
return <h1>{state.foo}</h1>
223+
}
224+
225+
const tree = renderer
226+
.create(
227+
<Provider value={app}>
228+
<FooComponent />
229+
</Provider>
230+
)
231+
.toJSON()
232+
233+
expect(renderCount).toBe(1)
234+
235+
renderer.act(() => {
236+
app.actions.doThis()
237+
})
238+
239+
expect(renderCount).toBe(2)
240+
expect(tree).toMatchSnapshot()
241+
})
242+
test('should allow using mocked Overmind', () => {
243+
let renderCount = 0
244+
245+
const doThis: Action = ({ state }) => {
246+
state.foo = 'bar2'
247+
}
248+
const config = {
249+
state: {
250+
foo: 'bar',
251+
},
252+
actions: {
253+
doThis,
254+
},
255+
}
256+
257+
type IConfig = {
258+
state: {
259+
foo: typeof config.state.foo
260+
}
261+
actions: {
262+
doThis: typeof doThis
263+
}
264+
}
265+
266+
interface Action<Input = void> extends IAction<IConfig, Input> {}
267+
268+
const useOvermind = createHook<typeof config>()
269+
270+
const FooComponent: React.FunctionComponent = () => {
271+
const { state } = useOvermind()
272+
renderCount++
273+
274+
return <h1>{state.foo}</h1>
275+
}
276+
277+
const mock = createOvermindMock(config)
278+
const tree = renderer
279+
.create(
280+
<Provider value={mock}>
281+
<FooComponent />
282+
</Provider>
283+
)
284+
.toJSON()
285+
286+
expect(renderCount).toBe(1)
287+
expect(tree).toMatchSnapshot()
288+
})
188289
})

packages/node_modules/overmind-react/src/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { IConfiguration, IContext, EventType, Overmind } from 'overmind'
1+
import {
2+
IConfiguration,
3+
IContext,
4+
EventType,
5+
Overmind,
6+
OvermindMock,
7+
} from 'overmind'
28
import {
39
// @ts-ignore
410
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
@@ -47,7 +53,7 @@ const context = createContext<Overmind<IConfiguration>>({} as Overmind<
4753
let nextComponentId = 0
4854

4955
export const Provider: React.ProviderExoticComponent<
50-
React.ProviderProps<Overmind<IConfiguration>>
56+
React.ProviderProps<Overmind<IConfiguration> | OvermindMock<IConfiguration>>
5157
> = context.Provider
5258

5359
export const createHook = <Config extends IConfiguration>(

packages/node_modules/overmind/src/index.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,17 @@ function deepCopy(obj) {
8484
return obj
8585
}
8686

87-
export function createOvermindMock<Config extends IConfiguration>(
88-
config: Config,
89-
mockedEffects?: NestedPartial<Config['effects']>
90-
): {
87+
export interface OvermindMock<Config extends IConfiguration> {
9188
actions: ResolveMockActions<Config['actions']>
89+
effects: Config['effects']
9290
state: ResolveState<Config['state']>
9391
onInitialize: () => Promise<IMutation[]>
94-
} {
92+
}
93+
94+
export function createOvermindMock<Config extends IConfiguration>(
95+
config: Config,
96+
mockedEffects?: NestedPartial<Config['effects']>
97+
): OvermindMock<Config> {
9598
const mock = new Overmind(
9699
Object.assign({}, config, {
97100
state: deepCopy(config.state),
@@ -126,9 +129,11 @@ export function createOvermindMock<Config extends IConfiguration>(
126129

127130
return {
128131
actions: mock.actions as any,
132+
effects: mock.effects,
129133
state: mock.state,
130134
onInitialize: (mock as any).onInitialize,
131-
}
135+
proxyStateTree: (mock as any).proxyStateTree,
136+
} as any
132137
}
133138

134139
const hotReloadingCache = {}

packages/overmind-website/examples/guide/usingovermindwithreact/hook_provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const overmind = new Overmind({
6767
actions: {}
6868
})
6969
70-
export const useOvermind = createHook(overmind)
70+
export const useOvermind = createHook()
7171
`,
7272
},
7373
{

0 commit comments

Comments
 (0)