All files / xstate/test types.test.ts

88.89% Statements 8/9
100% Branches 0/0
75% Functions 3/4
88.89% Lines 8/9

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 891x 1x     1x     1x                                                 1x                                                                                                   1x   1x 1x      
import { assert } from 'chai';
import { Machine } from '../src/index';
 
function noop(_x) {
  return;
}
 
describe('StateSchema', () => {
  interface LightStateSchema {
    data: {
      interval: number;
    };
    states: {
      green: {
        data: { name: string };
      };
      yellow: {};
      red: {
        states: {
          walk: any;
          wait: any;
          stop: any;
        };
      };
    };
  }
 
  type LightEvents =
    | { type: 'TIMER' }
    | { type: 'POWER_OUTAGE' }
    | { type: 'PED_COUNTDOWN'; duration: number };
 
  const lightMachine = Machine<undefined, LightStateSchema, LightEvents>({
    key: 'light',
    initial: 'green',
    data: { interval: 1000 },
    states: {
      green: {
        data: { name: 'greenLight' },
        on: {
          TIMER: 'yellow',
          POWER_OUTAGE: 'red'
        }
      },
      yellow: {
        on: {
          TIMER: 'red',
          POWER_OUTAGE: 'red'
        }
      },
      red: {
        on: {
          TIMER: 'green',
          POWER_OUTAGE: 'red'
        },
        initial: 'walk',
        states: {
          walk: {
            on: {
              PED_COUNTDOWN: 'wait'
            }
          },
          wait: {
            on: {
              PED_COUNTDOWN: {
                target: 'stop',
                cond: (_, e) => {
                  return e.duration === 0;
                }
              }
            }
          },
          stop: {
            on: {
              '': { target: 'green' }
            }
          }
        }
      }
    }
  });
 
  noop(lightMachine);
 
  it('should work with a StateSchema defined', () => {
    assert.ok(true, 'Tests will not compile if types are wrong');
  });
});