deno.land / x / xstate@xstate@4.33.6 / test / utils.ts

نووسراو ببینە
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { AnyState, AnyStateMachine, matchesState, StateValue} from '../src/index';
export function testMultiTransition( machine: AnyStateMachine, fromState: string, eventTypes: string): AnyState { const computeNext = (state: AnyState | string, eventType: string) => { if (typeof state === 'string' && state[0] === '{') { state = JSON.parse(state); } const nextState = machine.transition(state, eventType); return nextState; };
const [firstEventType, ...restEvents] = eventTypes.split(/,\s?/);
const resultState = restEvents.reduce<AnyState>( computeNext, computeNext(fromState, firstEventType) );
return resultState;}
export function testAll( machine: AnyStateMachine, expected: Record<string, Record<string, StateValue | undefined>>): void { Object.keys(expected).forEach((fromState) => { Object.keys(expected[fromState]).forEach((eventTypes) => { const toState = expected[fromState][eventTypes];
it(`should go from ${fromState} to ${JSON.stringify( toState )} on ${eventTypes}`, () => { const resultState = testMultiTransition(machine, fromState, eventTypes);
if (toState === undefined) { // undefined means that the state didn't transition expect(resultState.actions).toEqual([]); expect(resultState.changed).toBe(false); } else if (typeof toState === 'string') { expect(matchesState(toState, resultState.value)).toBeTruthy(); } else { expect(resultState.value).toEqual(toState); } }); }); });}
xstate

Version Info

Tagged at
2 years ago