deno.land / x / xstate@xstate@4.33.6 / test / deep.test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181import { Machine } from '../src/index';
describe('deep transitions', () => { const deepMachine = Machine({ key: 'deep', initial: 'A', on: { MACHINE_EVENT: '#deep.DONE' }, states: { DONE: {}, FAIL: {}, A: { on: { A_EVENT: '#deep.DONE', B_EVENT: 'FAIL', // shielded by B's B_EVENT A_S: '#deep.P.Q.R.S', A_P: '#deep.P' }, onEntry: 'ENTER_A', onExit: 'EXIT_A', initial: 'B', states: { B: { on: { B_EVENT: '#deep.DONE' }, onEntry: 'ENTER_B', onExit: 'EXIT_B', initial: 'C', states: { C: { on: { C_EVENT: '#deep.DONE' }, onEntry: 'ENTER_C', onExit: 'EXIT_C', initial: 'D', states: { D: { on: { D_EVENT: '#deep.DONE', D_S: '#deep.P.Q.R.S', D_P: '#deep.P' }, onEntry: 'ENTER_D', onExit: 'EXIT_D' } } } } } } }, P: { on: { P_EVENT: '#deep.DONE', Q_EVENT: 'FAIL' // shielded by Q's Q_EVENT }, onEntry: 'ENTER_P', onExit: 'EXIT_P', initial: 'Q', states: { Q: { on: { Q_EVENT: '#deep.DONE' }, onEntry: 'ENTER_Q', onExit: 'EXIT_Q', initial: 'R', states: { R: { on: { R_EVENT: '#deep.DONE' }, onEntry: 'ENTER_R', onExit: 'EXIT_R', initial: 'S', states: { S: { on: { S_EVENT: '#deep.DONE' }, onEntry: 'ENTER_S', onExit: 'EXIT_S' } } } } } } } } });
describe('exiting super/substates', () => { it('should exit all substates when superstates exits (A_EVENT)', () => { const actual = deepMachine .transition(deepMachine.initialState, 'A_EVENT') .actions.map((a) => a.type); const expected = ['EXIT_D', 'EXIT_C', 'EXIT_B', 'EXIT_A']; expect(actual).toEqual(expected); });
it('should exit substates and superstates when exiting (B_EVENT)', () => { const actual = deepMachine .transition(deepMachine.initialState, 'B_EVENT') .actions.map((a) => a.type); const expected = ['EXIT_D', 'EXIT_C', 'EXIT_B', 'EXIT_A']; expect(actual).toEqual(expected); });
it('should exit substates and superstates when exiting (C_EVENT)', () => { const actual = deepMachine .transition(deepMachine.initialState, 'C_EVENT') .actions.map((a) => a.type); const expected = ['EXIT_D', 'EXIT_C', 'EXIT_B', 'EXIT_A']; expect(actual).toEqual(expected); });
it('should exit superstates when exiting (D_EVENT)', () => { const actual = deepMachine .transition(deepMachine.initialState, 'D_EVENT') .actions.map((a) => a.type); const expected = ['EXIT_D', 'EXIT_C', 'EXIT_B', 'EXIT_A']; expect(actual).toEqual(expected); });
it('should exit substate when machine handles event (MACHINE_EVENT)', () => { const actual = deepMachine .transition(deepMachine.initialState, 'MACHINE_EVENT') .actions.map((a) => a.type); const expected = ['EXIT_D', 'EXIT_C', 'EXIT_B', 'EXIT_A']; expect(actual).toEqual(expected); });
const DBCAPQRS = [ 'EXIT_D', 'EXIT_C', 'EXIT_B', 'EXIT_A', 'ENTER_P', 'ENTER_Q', 'ENTER_R', 'ENTER_S' ];
it('should exit deep and enter deep (A_S)', () => { const actual = deepMachine .transition(deepMachine.initialState, 'A_S') .actions.map((a) => a.type); const expected = DBCAPQRS; expect(actual).toEqual(expected); });
it('should exit deep and enter deep (D_P)', () => { const actual = deepMachine .transition(deepMachine.initialState, 'D_P') .actions.map((a) => a.type); const expected = DBCAPQRS; expect(actual).toEqual(expected); });
it('should exit deep and enter deep (A_P)', () => { const actual = deepMachine .transition(deepMachine.initialState, 'A_P') .actions.map((a) => a.type); const expected = DBCAPQRS; expect(actual).toEqual(expected); });
it('should exit deep and enter deep (D_S)', () => { const actual = deepMachine .transition(deepMachine.initialState, 'D_S') .actions.map((a) => a.type); const expected = DBCAPQRS; expect(actual).toEqual(expected); }); });});
Version Info