-
Notifications
You must be signed in to change notification settings - Fork 60
/
testStateMachine.js
31 lines (24 loc) · 966 Bytes
/
testStateMachine.js
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
import idx from 'idx'
import React from 'react'
import TestRenderer from 'react-test-renderer'
import { getShortestPaths } from 'xstate/lib/graph'
import { stringify } from './utils'
const testStateMachine = (Component, options = {}) => {
const paths = getShortestPaths(Component.machine, options.extendedState)
Object.keys(paths).forEach(key => {
const initialData = idx(options, _ => _.fixtures.initialData)
const renderer = TestRenderer.create(
<Component initialData={initialData} />
)
const instance = renderer.getInstance()
paths[key].forEach(({ event, state }) => {
const fixtures = idx(options, _ => _.fixtures[state][event])
instance.handleTransition(event, fixtures)
})
const machineState =
instance.state.machineState.toString() ||
stringify(instance.state.machineState.value).join(',')
expect(renderer.toJSON()).toMatchSnapshot(machineState)
})
}
export default testStateMachine