All files / xstate/test final.test.ts

100% Statements 14/14
100% Branches 0/0
100% Functions 2/2
100% Lines 14/14

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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 931x 1x 1x     1x                                                                                                                                   1x 1x 1x 1x 1x   1x       1x   1x         1x 1x      
import { Machine } from '../src/index';
import { assert } from 'chai';
import { done } from '../src/actions';
 
// @ts-ignore
const finalMachine = Machine({
  id: 'final',
  initial: 'green',
  states: {
    green: {
      on: {
        TIMER: 'yellow'
      }
    },
    yellow: { on: { TIMER: 'red' } },
    red: {
      type: 'parallel',
      states: {
        crosswalk1: {
          initial: 'walk',
          states: {
            walk: {
              on: { PED_WAIT: 'wait' }
            },
            wait: {
              on: { PED_STOP: 'stop' }
            },
            stop: {
              type: 'final'
            }
          },
          onDone: {
            actions: 'stopCrosswalk1'
          }
        },
        crosswalk2: {
          initial: 'walk',
          states: {
            walk: {
              on: { PED_WAIT: 'wait' }
            },
            wait: {
              on: { PED_STOP: 'stop' }
            },
            stop: {
              on: { PED_STOP: 'stop2' }
            },
            stop2: {
              type: 'final'
            }
          },
          on: {
            [done('final.red.crosswalk2')]: {
              actions: 'stopCrosswalk2'
            }
          }
        }
      },
      on: {
        [done('final.red')]: {
          actions: 'prepareGreenLight'
        }
      }
    }
  },
  onDone: {
    // this action should never occur because final states are not direct children of machine
    actions: 'shouldNeverOccur'
  }
});
 
describe('final states', () => {
  it('should emit the "done.state.final.red" event when all nested states are finalized', () => {
    const redState = finalMachine.transition('yellow', 'TIMER');
    const waitState = finalMachine.transition(redState, 'PED_WAIT');
    const stopState = finalMachine.transition(waitState, 'PED_STOP');
 
    assert.sameDeepMembers(stopState.actions, [
      { type: 'stopCrosswalk1', exec: undefined }
    ]);
 
    const stopState2 = finalMachine.transition(stopState, 'PED_STOP');
 
    assert.sameDeepMembers(stopState2.actions, [
      { type: 'stopCrosswalk2', exec: undefined },
      { type: 'prepareGreenLight', exec: undefined }
    ]);
 
    const greenState = finalMachine.transition(stopState, 'TIMER');
    assert.isEmpty(greenState.actions);
  });
});