All files / xstate/test machine.test.ts

93.1% Statements 27/29
100% Branches 0/0
85.71% Functions 12/14
93.1% Lines 27/29

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 134 135 136 137 138 1391x 1x 1x 1x   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';
import { State } from '../src/State';
import { interpret } from '../src/interpreter';
 
const pedestrianStates = {
  initial: 'walk',
  states: {
    walk: {
      on: {
        PED_COUNTDOWN: 'wait'
      }
    },
    wait: {
      on: {
        PED_COUNTDOWN: 'stop'
      }
    },
    stop: {}
  }
};
 
interface LightStateSchema {
  states: {
    green: any;
    yellow: any;
    red: any;
  };
}
 
const lightMachine = Machine<undefined, LightStateSchema>({
  key: 'light',
  initial: 'green',
  states: {
    green: {
      on: {
        TIMER: 'yellow',
        POWER_OUTAGE: 'red',
        FORBIDDEN_EVENT: undefined
      }
    },
    yellow: {
      on: {
        TIMER: 'red',
        POWER_OUTAGE: 'red'
      }
    },
    red: {
      on: {
        TIMER: 'green',
        POWER_OUTAGE: 'red'
      },
      ...pedestrianStates
    }
  }
});
 
const configMachine = Machine(
  {
    id: 'config',
    initial: 'foo',
    states: {
      foo: {
        onEntry: 'entryAction',
        on: {
          EVENT: {
            target: 'bar',
            cond: 'someCondition'
          }
        }
      },
      bar: {}
    }
  },
  {
    actions: {
      entryAction: () => {
        throw new Error('original entry');
      }
    },
    guards: {
      someCondition: () => false
    }
  }
);
 
describe('machine', () => {
  describe('machine.states', () => {
    it('should properly register machine states', () => {
      assert.deepEqual(Object.keys(lightMachine.states), [
        'green',
        'yellow',
        'red'
      ]);
    });
  });
 
  describe('machine.events', () => {
    it('should return the set of events accepted by machine', () => {
      assert.sameMembers(lightMachine.events, [
        'TIMER',
        'POWER_OUTAGE',
        'PED_COUNTDOWN'
      ]);
    });
  });
 
  describe('machine.initialState', () => {
    it('should return a State instance', () => {
      assert.instanceOf(lightMachine.initialState, State);
    });
 
    it('should return the initial state', () => {
      assert.equal(lightMachine.initialState.value, 'green');
    });
  });
 
  describe('machine.withConfig', () => {
    const differentMachine = configMachine.withConfig({
      actions: {
        entryAction: () => {
          throw new Error('new entry');
        }
      },
      guards: { someCondition: () => true }
    });
 
    const interpreter = interpret(differentMachine);
 
    assert.throws(
      () => interpreter.start(),
      /new entry/,
      'different action should be used'
    );
 
    assert.deepEqual(differentMachine.transition('foo', 'EVENT').value, 'bar');
  });
});