deno.land / x / xstate@xstate@4.33.6 / test / patterns.test.ts
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091import { toggle, sequence } from '../src/patterns';import { Machine } from '../src';
describe('patterns', () => { describe('toggle pattern', () => { it('should produce a partial state machine with a binary toggle', () => { expect(toggle('on', 'off', 'SWITCH')).toEqual({ on: { on: { SWITCH: 'off' } }, off: { on: { SWITCH: 'on' } } }); }); });
describe('sequence pattern', () => { it('should work with an array', () => { const seq = ['one', 'two', 'three', 'last'];
const sequenceMachine = Machine({ id: 'sequence', ...sequence(seq) });
expect(sequenceMachine.transition(seq[0], 'NEXT').value).toEqual(seq[1]);
expect(sequenceMachine.transition(seq[1], 'PREV').value).toEqual(seq[0]);
expect( sequenceMachine.transition(seq[seq.length - 1], 'NEXT').value ).toEqual(seq[seq.length - 1]);
expect(sequenceMachine.transition(seq[0], 'PREV').value).toEqual(seq[0]); });
it('should customize the next/prev events', () => { const seq = ['one', 'two', 'three', 'last'];
const sequenceMachine = Machine({ id: 'sequence', ...sequence(seq, { nextEvent: 'FORWARD', prevEvent: 'BACK' }) });
expect(sequenceMachine.transition(seq[0], 'NEXT').value).toEqual(seq[0]);
expect(sequenceMachine.transition(seq[1], 'PREV').value).toEqual(seq[1]);
expect(sequenceMachine.transition(seq[0], 'FORWARD').value).toEqual( seq[1] );
expect(sequenceMachine.transition(seq[1], 'BACK').value).toEqual(seq[0]); });
it('should allow next/prev events to be undefined', () => { const seq = ['one', 'two', 'three', 'last'];
const sequenceMachine = Machine({ id: 'sequence', ...sequence(seq, { nextEvent: 'FORWARD', prevEvent: undefined }) });
expect(sequenceMachine.transition(seq[0], 'FORWARD').value).toEqual( seq[1] );
expect(sequenceMachine.transition(seq[1], 'BACK').value).toEqual(seq[1]);
const backSequenceMachine = Machine({ id: 'backSequence', ...sequence(seq, { nextEvent: undefined, prevEvent: 'BACK' }) });
expect(backSequenceMachine.transition(seq[0], 'FORWARD').value).toEqual( seq[0] );
expect(backSequenceMachine.transition(seq[1], 'BACK').value).toEqual( seq[0] ); }); });});
Version Info