Skip to content

Commit 8f7ecf6

Browse files
feat(overmind): createOvermindMock accepts an initial state callback
1 parent ee06ac2 commit 8f7ecf6

File tree

3 files changed

+126
-43
lines changed

3 files changed

+126
-43
lines changed

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

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { EventType, IAction, Overmind, SERIALIZE, createOvermindMock, rehydrate } from './'
1+
import {
2+
EventType,
3+
IAction,
4+
Overmind,
5+
SERIALIZE,
6+
createOvermindMock,
7+
rehydrate,
8+
} from './'
29
import { namespaced } from './config'
310

411
function toJSON(obj) {
@@ -10,7 +17,7 @@ class StateValue {
1017
toJSON() {
1118
return {
1219
[SERIALIZE]: true,
13-
value: this.value
20+
value: this.value,
1421
}
1522
}
1623
static fromJSON(json: { value: string }) {
@@ -26,7 +33,7 @@ function createDefaultOvermind() {
2633
item: {
2734
isAwesome: true,
2835
},
29-
value: null as unknown as StateValue
36+
value: (null as unknown) as StateValue,
3037
}
3138
const changeFoo: Action<void, string> = (context) => {
3239
context.state.foo = 'bar2'
@@ -68,7 +75,7 @@ function createDefaultOvermind() {
6875
changeFooWithEffect,
6976
changeValue,
7077
waitAndChangeFoo,
71-
rehydrateAction
78+
rehydrateAction,
7279
}
7380
const effects = {
7481
hello() {
@@ -170,14 +177,14 @@ describe('Overmind', () => {
170177
})
171178
app.eventHub.once(EventType.ACTION_START, (data) => {
172179
expect(toJSON(data)).toEqual({
173-
actionId:'doThis',
180+
actionId: 'doThis',
174181
actionName: 'doThis',
175182
isRunning: true,
176183
namespacePath: [],
177184
executionId: 0,
178185
operatorId: 0,
179186
path: [],
180-
type: 'action'
187+
type: 'action',
181188
})
182189
})
183190
app.eventHub.once(EventType.ACTION_END, (data) => {
@@ -242,7 +249,7 @@ describe('Overmind', () => {
242249
method: 'set',
243250
path: 'foo',
244251
hasChangedValue: true,
245-
delimiter: '.'
252+
delimiter: '.',
246253
},
247254
],
248255
executionId: 0,
@@ -268,7 +275,7 @@ describe('Overmind', () => {
268275
method: 'set',
269276
path: 'foo',
270277
hasChangedValue: true,
271-
delimiter: '.'
278+
delimiter: '.',
272279
},
273280
],
274281
executionId: 0,
@@ -294,7 +301,7 @@ describe('Overmind', () => {
294301
method: 'set',
295302
path: 'foo',
296303
hasChangedValue: true,
297-
delimiter: '.'
304+
delimiter: '.',
298305
},
299306
],
300307
executionId: 0,
@@ -388,8 +395,8 @@ describe('Overmind', () => {
388395
expect(app.state.foo).toBe('bar2')
389396
app.reconfigure({
390397
state: {
391-
foo: 'bar'
392-
}
398+
foo: 'bar',
399+
},
393400
})
394401
expect(app.state.foo).toBe('bar2')
395402
})
@@ -399,41 +406,17 @@ describe('Overmind', () => {
399406
const changeFoo = app.actions.changeFoo
400407
app.reconfigure({
401408
state: {
402-
foo: 'bar2'
409+
foo: 'bar2',
403410
},
404411
actions: {
405412
changeFoo(context) {
406413
context.state.foo = 'replaced!'
407-
}
408-
}
414+
},
415+
},
409416
})
410417

411418
expect(app.state.foo).toBe('bar2')
412419
changeFoo()
413420
expect(app.state.foo).toBe('replaced!')
414421
})
415422
})
416-
417-
describe('Overmind mock', () => {
418-
test('should preserve getters', async (done) => {
419-
expect.assertions(1)
420-
const state = {
421-
value: 0,
422-
get valuePlusTwo() {
423-
return this.value + 2
424-
},
425-
}
426-
const updateValue: Action = (context) => {
427-
context.state.value = 15
428-
}
429-
const actions = { updateValue }
430-
const config = { state, actions }
431-
type Config = typeof config
432-
interface Action<Value = void> extends IAction<Config, Value> {}
433-
434-
const mock = createOvermindMock(config)
435-
await mock.actions.updateValue()
436-
expect(mock.state.valuePlusTwo).toEqual(17)
437-
done()
438-
})
439-
})

packages/node_modules/overmind/src/index.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,44 @@ export interface OvermindMock<Config extends IConfiguration>
134134
mutations: IMutation[]
135135
}
136136

137+
export function createOvermindMock<Config extends IConfiguration>(
138+
config: Config
139+
): OvermindMock<Config>
140+
export function createOvermindMock<Config extends IConfiguration>(
141+
config: Config,
142+
setInitialState: (state: Config['state']) => void
143+
): OvermindMock<Config>
144+
export function createOvermindMock<Config extends IConfiguration>(
145+
config: Config,
146+
mockedEffects: NestedPartial<Config['effects']>
147+
): OvermindMock<Config>
137148
export function createOvermindMock<Config extends IConfiguration>(
138149
config: Config,
139-
mockedEffects?: NestedPartial<Config['effects']>
150+
mockedEffects: NestedPartial<Config['effects']>,
151+
setInitialState: (state: Config['state']) => void
152+
): OvermindMock<Config>
153+
export function createOvermindMock<Config extends IConfiguration>(
154+
...args:
155+
| [Config]
156+
| [Config, (state: Config['state']) => void]
157+
| [Config, NestedPartial<Config['effects']>]
158+
| [
159+
Config,
160+
NestedPartial<Config['effects']>,
161+
(state: Config['state']) => void
162+
]
140163
): OvermindMock<Config> {
164+
const setState = typeof args[1] === 'function' ? args[1] : args[2]
165+
const mockedEffects = typeof args[1] === 'function' ? undefined : args[1]
166+
167+
const state = deepCopy(args[0].state)
168+
169+
if (setState) {
170+
;(setState as any)(state)
171+
}
141172
const mock = new Overmind(
142-
Object.assign({}, config, {
143-
state: deepCopy(config.state),
173+
Object.assign({}, args[0], {
174+
state,
144175
}),
145176
{
146177
devtools: false,
@@ -167,7 +198,10 @@ export function createOvermindMock<Config extends IConfiguration>(
167198
} as TestMode
168199
) as OvermindMock<Config>
169200

170-
const action = (mock as any).createAction('onInitialize', config.onInitialize)
201+
const action = (mock as any).createAction(
202+
'onInitialize',
203+
args[0].onInitialize
204+
)
171205

172206
mock.onInitialize = () => action(mock)
173207
mock.mutations = []

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

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,74 @@ describe('Mock', () => {
7676
path: 'foo',
7777
args: ['bar!'],
7878
hasChangedValue: true,
79-
delimiter: '.',
79+
delimiter: '.',
8080
},
8181
])
8282
})
83+
test('should preserve getters', async (done) => {
84+
expect.assertions(1)
85+
const state = {
86+
value: 0,
87+
get valuePlusTwo() {
88+
return this.value + 2
89+
},
90+
}
91+
const updateValue: Action = (context) => {
92+
context.state.value = 15
93+
}
94+
const actions = { updateValue }
95+
const config = { state, actions }
96+
type Config = typeof config
97+
interface Action<Value = void> extends IAction<Config, Value> {}
98+
99+
const mock = createOvermindMock(config)
100+
await mock.actions.updateValue()
101+
expect(mock.state.valuePlusTwo).toEqual(17)
102+
done()
103+
})
104+
test('should allow setting initial state', async () => {
105+
expect.assertions(1)
106+
const state = {
107+
value: 0,
108+
}
109+
const config = { state }
110+
111+
const mock = createOvermindMock(config, (state) => {
112+
state.value = 1
113+
})
114+
expect(mock.state.value).toBe(1)
115+
})
116+
test('should allow setting initial and mock effect', async () => {
117+
expect.assertions(2)
118+
const state = {
119+
value: 0,
120+
}
121+
const config = {
122+
state,
123+
actions: {
124+
runFoo({ effects }) {
125+
return effects.foo()
126+
},
127+
},
128+
effects: {
129+
foo() {
130+
return 'bar'
131+
},
132+
},
133+
}
134+
135+
const mock = createOvermindMock(
136+
config,
137+
{
138+
foo() {
139+
return 'bar2'
140+
},
141+
},
142+
(state) => {
143+
state.value = 1
144+
}
145+
)
146+
expect(mock.state.value).toBe(1)
147+
expect(mock.actions.runFoo()).toBe('bar2')
148+
})
83149
})

0 commit comments

Comments
 (0)