All files / xstate/test state.test.ts

100% Statements 24/24
100% Branches 0/0
100% Functions 8/8
100% Lines 24/24

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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 1341x 1x   1x                                                                                                                                                           1x 1x 1x     1x 1x 1x     1x 1x 1x 1x           1x 1x 1x     1x 1x 1x     1x 1x 1x                 1x         1x              
import { assert } from 'chai';
import { Machine } from '../src/index';
 
const machine = Machine({
  initial: 'one',
  states: {
    one: {
      onEntry: ['enter'],
      on: {
        EXTERNAL: {
          target: 'one',
          internal: false
        },
        INERT: {
          target: 'one',
          internal: true
        },
        INTERNAL: {
          target: 'one',
          internal: true,
          actions: ['doSomething']
        },
        TO_TWO: 'two',
        TO_THREE: 'three',
        FORBIDDEN_EVENT: undefined
      }
    },
    two: {
      initial: 'deep',
      states: {
        deep: {
          initial: 'foo',
          states: {
            foo: {
              on: {
                FOO_EVENT: 'bar',
                FORBIDDEN_EVENT: undefined
              }
            },
            bar: {
              on: {
                BAR_EVENT: 'foo'
              }
            }
          }
        }
      },
      on: {
        DEEP_EVENT: '.'
      }
    },
    three: {
      type: 'parallel',
      states: {
        first: {
          initial: 'p31',
          states: {
            p31: {
              on: { P31: '.' }
            }
          }
        },
        second: {
          initial: 'p32',
          states: {
            p32: {
              on: { P32: '.' }
            }
          }
        }
      },
      on: {
        THREE_EVENT: '.'
      }
    }
  },
  on: {
    MACHINE_EVENT: '.two'
  }
});
 
describe('State', () => {
  it('should indicate that it is not changed if initial state', () => {
    assert.isUndefined(machine.initialState.changed);
  });
 
  it('states from external transitions with onEntry actions should be changed', () => {
    const changedState = machine.transition(machine.initialState, 'EXTERNAL');
    assert.isTrue(changedState.changed, 'changed due to onEntry action');
  });
 
  it('states from internal transitions with no actions should be unchanged', () => {
    const changedState = machine.transition(machine.initialState, 'EXTERNAL');
    const unchangedState = machine.transition(changedState, 'INERT');
    assert.isFalse(
      unchangedState.changed,
      'unchanged - same state, no actions'
    );
  });
 
  it('states from internal transitions with actions should be changed', () => {
    const changedState = machine.transition(machine.initialState, 'INTERNAL');
    assert.isTrue(changedState.changed, 'changed - transition actions');
  });
 
  it('normal state transitions should be changed', () => {
    const changedState = machine.transition(machine.initialState, 'TO_TWO');
    assert.isTrue(changedState.changed, 'changed - different state');
  });
 
  describe('.nextEvents', () => {
    it('returns the next possible events for the current state', () => {
      assert.deepEqual(machine.initialState.nextEvents, [
        'EXTERNAL',
        'INERT',
        'INTERNAL',
        'TO_TWO',
        'TO_THREE',
        'MACHINE_EVENT'
      ]);
 
      assert.deepEqual(
        machine.transition(machine.initialState, 'TO_TWO').nextEvents,
        ['FOO_EVENT', 'DEEP_EVENT', 'MACHINE_EVENT']
      );
 
      assert.deepEqual(
        machine.transition(machine.initialState, 'TO_THREE').nextEvents,
        ['P31', 'P32', 'THREE_EVENT', 'MACHINE_EVENT']
      );
    });
  });
});