Skip to content

Commit cf80060

Browse files
Merge pull request cerebral#490 from schiller-manuel/next
fix(overmind-statechart): fix cerebral#493 and cerebral#488
2 parents 0f64daf + 8291628 commit cf80060

File tree

2 files changed

+301
-197
lines changed

2 files changed

+301
-197
lines changed

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,49 @@ describe('Statecharts', () => {
129129
expect(instance.state.count).toBe(1)
130130
})
131131

132+
test('should run onInitializeOvermind action', () => {
133+
const onInitializeOvermind = ({ state }: Context) => {
134+
state.count++
135+
}
136+
137+
const state = {
138+
count: 0,
139+
}
140+
const actions = {
141+
onInitializeOvermind,
142+
}
143+
144+
const config = {
145+
state,
146+
actions,
147+
}
148+
149+
const chart: Statechart<
150+
Context,
151+
{
152+
foo: void
153+
}
154+
> = {
155+
initial: 'foo',
156+
states: {foo: {}}
157+
}
158+
159+
type Context = IContext<{
160+
state: typeof state
161+
actions: typeof actions
162+
}>
163+
164+
const instance = createOvermind(
165+
statechart(config, {
166+
id1: chart,
167+
}
168+
))
169+
170+
expect(instance.state.states).toEqual([['id1', 'foo']])
171+
expect(instance.state.actions).toEqual({})
172+
expect(instance.state.count).toBe(1)
173+
})
174+
132175
test('should run entry action', () => {
133176
const increaseCount = ({ state }: Context) => {
134177
state.count++
@@ -181,6 +224,65 @@ describe('Statecharts', () => {
181224
expect(instance.state.count).toBe(1)
182225
})
183226

227+
test('should run entry action of initial state when onInitializeOvermind is present', () => {
228+
const onInitializeOvermind = ({ state }: Context) => {
229+
state.initCount++
230+
}
231+
const increaseCount = ({ state }: Context) => {
232+
state.actionCount++
233+
}
234+
const state = {
235+
actionCount: 0,
236+
initCount: 0
237+
}
238+
const actions = {
239+
increaseCount,
240+
onInitializeOvermind
241+
}
242+
const config = {
243+
state,
244+
actions,
245+
}
246+
247+
const chart: Statechart<
248+
typeof config,
249+
{
250+
foo: void
251+
}
252+
> = {
253+
initial: 'foo',
254+
states: {
255+
foo: {
256+
entry: 'increaseCount',
257+
},
258+
},
259+
}
260+
261+
type Context = IContext<{
262+
state: typeof state
263+
actions: typeof actions
264+
}>
265+
266+
const instance = createOvermind(
267+
statechart(config, {
268+
id1: chart,
269+
})
270+
)
271+
272+
expect(instance.state.states).toEqual([['id1', 'foo']])
273+
expect(instance.state.actions).toEqual({ increaseCount: false})
274+
expect(
275+
instance.state.matches({
276+
id1: {
277+
foo: true,
278+
},
279+
})
280+
).toEqual(true)
281+
expect(instance.state.initCount).toBe(1)
282+
expect(instance.state.actionCount).toBe(1)
283+
284+
})
285+
184286
test('should run exit action', () => {
185287
const increaseCount = ({ state }: Context) => {
186288
state.count++

0 commit comments

Comments
 (0)