All files / xstate/test data.test.ts

100% Statements 12/12
100% Branches 0/0
100% Functions 3/3
100% Lines 12/12

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 93 94 95 96 97 98 99 100 101 102 1031x 1x   1x 1x                                                     1x                                                                                       1x 1x 1x         1x 1x     1x 1x                              
import { assert } from 'chai';
import { Machine } from '../src/index';
 
describe('state data', () => {
  const pedestrianStates = {
    initial: 'walk',
    states: {
      walk: {
        data: { walkData: 'walk data' },
        on: {
          PED_COUNTDOWN: 'wait'
        },
        onEntry: 'enter_walk',
        onExit: 'exit_walk'
      },
      wait: {
        data: { waitData: 'wait data' },
        on: {
          PED_COUNTDOWN: 'stop'
        },
        onEntry: 'enter_wait',
        onExit: 'exit_wait'
      },
      stop: {
        data: { stopData: 'stop data' },
        onEntry: 'enter_stop',
        onExit: 'exit_stop'
      }
    }
  };
 
  const lightMachine = Machine({
    key: 'light',
    initial: 'green',
    states: {
      green: {
        data: ['green', 'array', 'data'],
        on: {
          TIMER: 'yellow',
          POWER_OUTAGE: 'red',
          NOTHING: 'green'
        },
        onEntry: 'enter_green',
        onExit: 'exit_green'
      },
      yellow: {
        data: { yellowData: 'yellow data' },
        on: {
          TIMER: 'red',
          POWER_OUTAGE: 'red'
        },
        onEntry: 'enter_yellow',
        onExit: 'exit_yellow'
      },
      red: {
        data: {
          redData: {
            nested: {
              red: 'data',
              array: [1, 2, 3]
            }
          }
        },
        on: {
          TIMER: 'green',
          POWER_OUTAGE: 'red',
          NOTHING: 'red'
        },
        onEntry: 'enter_red',
        onExit: 'exit_red',
        ...pedestrianStates
      }
    }
  });
 
  it('states should aggregate data', () => {
    const yellowState = lightMachine.transition('green', 'TIMER');
    assert.deepEqual(yellowState.data, {
      'light.yellow': {
        yellowData: 'yellow data'
      }
    });
    assert.notProperty(yellowState.data, 'light.green');
    assert.notProperty(yellowState.data, 'light');
  });
 
  it('states should aggregate data (deep)', () => {
    assert.deepEqual(lightMachine.transition('yellow', 'TIMER').data, {
      'light.red': {
        redData: {
          nested: {
            array: [1, 2, 3],
            red: 'data'
          }
        }
      },
      'light.red.walk': {
        walkData: 'walk data'
      }
    });
  });
});