deno.land / x / xstate@xstate@4.33.6 / test / tags.test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138import { createMachine } from '../src';
describe('tags', () => { it('supports tagging states', () => { const machine = createMachine({ initial: 'green', states: { green: { tags: ['go'] }, yellow: { tags: ['go'], on: { TIMER: 'red' } }, red: { tags: ['stop'] } } });
expect(machine.initialState.hasTag('go')).toBeTruthy(); expect(machine.transition('yellow', 'TIMER').hasTag('go')).toBeFalsy(); });
it('supports tags in compound states', () => { const machine = createMachine({ initial: 'red', states: { green: { tags: ['go'] }, yellow: {}, red: { tags: ['stop'], initial: 'walk', states: { walk: { tags: ['crosswalkLight'] }, wait: { tags: ['crosswalkLight'] } } } } });
expect(machine.initialState.hasTag('go')).toBeFalsy(); expect(machine.initialState.hasTag('stop')).toBeTruthy(); expect(machine.initialState.hasTag('crosswalkLight')).toBeTruthy(); });
it('supports tags in parallel states', () => { const machine = createMachine({ type: 'parallel', states: { foo: { initial: 'active', states: { active: { tags: 'yes' }, inactive: { tags: 'no' } } }, bar: { initial: 'active', states: { active: { tags: 'yes', on: { DEACTIVATE: 'inactive' } }, inactive: { tags: 'no' } } } } });
let state = machine.initialState;
expect(state.tags).toEqual(new Set(['yes'])); state = machine.transition(state, 'DEACTIVATE'); expect(state.tags).toEqual(new Set(['yes', 'no'])); });
it('sets tags correctly after not selecting any transition', () => { const machine = createMachine({ initial: 'a', states: { a: { tags: 'myTag' } } });
const state = machine.transition(machine.initialState, { type: 'UNMATCHED' }); expect(state.hasTag('myTag')).toBeTruthy(); });
it('tags can be single (not array)', () => { const machine = createMachine({ initial: 'green', states: { green: { tags: 'go' } } });
expect(machine.initialState.hasTag('go')).toBeTruthy(); });
it('stringifies to an array', () => { const machine = createMachine({ initial: 'green', states: { green: { tags: ['go', 'light'] } } });
const jsonState = machine.initialState.toJSON();
expect(jsonState.tags).toEqual(['go', 'light']); });});
Version Info