Skip to content

Commit 99b1eaa

Browse files
feat(overmind): expose onInitialize on mock to explicitly run
1 parent 1e4959b commit 99b1eaa

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

packages/node_modules/overmind/src/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export function createOvermindMock<Config extends Configuration>(
8080
): {
8181
actions: ResolveMockActions<Config['actions']>
8282
state: ResolveState<Config['state']>
83+
onInitialize: () => Promise<IMutation[]>
8384
} {
8485
const mock = new Overmind(
8586
Object.assign({}, config, {
@@ -116,6 +117,7 @@ export function createOvermindMock<Config extends Configuration>(
116117
return {
117118
actions: mock.actions as any,
118119
state: mock.state,
120+
onInitialize: (mock as any).onInitialize,
119121
}
120122
}
121123

@@ -263,7 +265,15 @@ export class Overmind<Config extends Configuration> implements Configuration {
263265
*/
264266
this.actions = this.getActions(configuration)
265267

266-
if (configuration.onInitialize) {
268+
if (options.testMode) {
269+
const action = this.createAction(
270+
'onInitialize',
271+
configuration.onInitialize
272+
)
273+
274+
const overmind = this as any
275+
overmind.onInitialize = () => action(this)
276+
} else if (configuration.onInitialize) {
267277
const onInitialize = this.createAction(
268278
'onInitialize',
269279
configuration.onInitialize

packages/node_modules/overmind/src/mock.test.ts

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { createOvermindMock, IAction } from './'
1+
import { createOvermindMock, IAction, IOnInitialize } from './'
22

33
type State = {
44
foo: string
55
upperFoo: string
66
}
77

88
describe('Mock', () => {
9-
test('should run action tests', () => {
9+
test('should run action tests', async () => {
1010
type State = {
1111
foo: string
1212
}
1313
const state: State = {
1414
foo: 'bar',
1515
}
16-
const test: Action = ({ state, effects, actions }) => {
16+
const test: Action = ({ state, effects }) => {
1717
state.foo = effects.effect()
1818
}
1919
const actions = { test }
@@ -35,14 +35,50 @@ describe('Mock', () => {
3535
},
3636
})
3737

38-
return overmind.actions.test().then((result) =>
39-
expect(result).toEqual([
40-
{
41-
method: 'set',
42-
path: 'foo',
43-
args: ['bar3'],
44-
},
45-
])
46-
)
38+
const result = await overmind.actions.test()
39+
40+
expect(result).toEqual([
41+
{
42+
method: 'set',
43+
path: 'foo',
44+
args: ['bar3'],
45+
},
46+
])
47+
})
48+
test('should test onInitialize explicitly', async () => {
49+
type State = {
50+
foo: string
51+
}
52+
const state: State = {
53+
foo: 'bar',
54+
}
55+
const onInitialize: OnInitialize = async ({ state }) => {
56+
state.foo += '!'
57+
}
58+
59+
const config = {
60+
onInitialize,
61+
state,
62+
}
63+
64+
type Config = typeof config
65+
66+
interface OnInitialize extends IOnInitialize<Config> {}
67+
68+
const overmind = createOvermindMock(config, {
69+
effect() {
70+
return 'bar3'
71+
},
72+
})
73+
74+
const result = await overmind.onInitialize()
75+
76+
expect(result).toEqual([
77+
{
78+
method: 'set',
79+
path: 'foo',
80+
args: ['bar!'],
81+
},
82+
])
4783
})
4884
})

0 commit comments

Comments
 (0)