All files / xstate/test utils.ts

100% Statements 21/21
100% Branches 8/8
100% Functions 6/6
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  1x 1x   1x         116x     130x 13x   130x 130x     116x     1x 6x 35x 104x   104x     104x   104x   20x 20x 84x 79x   5x            
import { StateNode, State } from '../src/index';
import { assert } from 'chai';
import { matchesState } from '../lib';
 
export function testMultiTransition<TExt>(
  machine: StateNode<TExt>,
  fromState: string,
  eventTypes: string
) {
  const resultState = eventTypes
    .split(/,\s?/)
    .reduce((state: State<TExt> | string, eventType) => {
      if (typeof state === 'string' && state[0] === '{') {
        state = JSON.parse(state);
      }
      const nextState = machine.transition(state, eventType);
      return nextState;
    }, fromState) as State<TExt>;
 
  return resultState;
}
 
export function testAll(machine: StateNode, expected: {}): 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
          assert.isEmpty(resultState.actions);
          assert.isUndefined(resultState.history);
        } else if (typeof toState === 'string') {
          assert.ok(matchesState(resultState.value, toState));
        } else {
          assert.deepEqual(resultState.value, toState);
        }
      });
    });
  });
}