deno.land / x / xstate@xstate@4.33.6 / test / parallel.test.ts
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046import { raise, assign } from '../src/actions';import { createMachine, interpret, Machine, StateValue } from '../src';import { testMultiTransition } from './utils';
const composerMachine = Machine({ strict: true, initial: 'ReadOnly', states: { ReadOnly: { id: 'ReadOnly', initial: 'StructureEdit', onEntry: ['selectNone'], states: { StructureEdit: { id: 'StructureEditRO', type: 'parallel', on: { switchToProjectManagement: [ { target: 'ProjectManagement' } ] }, states: { SelectionStatus: { initial: 'SelectedNone', on: { singleClickActivity: [ { target: '.SelectedActivity', actions: ['selectActivity'] } ], singleClickLink: [ { target: '.SelectedLink', actions: ['selectLink'] } ] }, states: { SelectedNone: { onEntry: ['redraw'] }, SelectedActivity: { onEntry: ['redraw'], on: { singleClickCanvas: [ { target: 'SelectedNone', actions: ['selectNone'] } ] } }, SelectedLink: { onEntry: ['redraw'], on: { singleClickCanvas: [ { target: 'SelectedNone', actions: ['selectNone'] } ] } } } }, ClipboardStatus: { initial: 'Empty', states: { Empty: { onEntry: ['emptyClipboard'], on: { cutInClipboardSuccess: [ { target: 'FilledByCut' } ], copyInClipboardSuccess: [ { target: 'FilledByCopy' } ] } }, FilledByCopy: { on: { cutInClipboardSuccess: [ { target: 'FilledByCut' } ], copyInClipboardSuccess: [ { target: 'FilledByCopy' } ], pasteFromClipboardSuccess: [ { target: 'FilledByCopy' } ] } }, FilledByCut: { on: { cutInClipboardSuccess: [ { target: 'FilledByCut' } ], copyInClipboardSuccess: [ { target: 'FilledByCopy' } ], pasteFromClipboardSuccess: [ { target: 'Empty' } ] } } } } } }, ProjectManagement: { id: 'ProjectManagementRO', type: 'parallel', on: { switchToStructureEdit: [ { target: 'StructureEdit' } ] }, states: { SelectionStatus: { initial: 'SelectedNone', on: { singleClickActivity: [ { target: '.SelectedActivity', actions: ['selectActivity'] } ], singleClickLink: [ { target: '.SelectedLink', actions: ['selectLink'] } ] }, states: { SelectedNone: { onEntry: ['redraw'] }, SelectedActivity: { onEntry: ['redraw'], on: { singleClickCanvas: [ { target: 'SelectedNone', actions: ['selectNone'] } ] } }, SelectedLink: { onEntry: ['redraw'], on: { singleClickCanvas: [ { target: 'SelectedNone', actions: ['selectNone'] } ] } } } } } } } } }});
const wakMachine = Machine({ id: 'wakMachine', type: 'parallel', strict: true, states: { wak1: { initial: 'wak1sonA', states: { wak1sonA: { onEntry: 'wak1sonAenter', onExit: 'wak1sonAexit' }, wak1sonB: { onEntry: 'wak1sonBenter', onExit: 'wak1sonBexit' } }, on: { WAK1: '.wak1sonB' }, onEntry: 'wak1enter', onExit: 'wak1exit' }, wak2: { initial: 'wak2sonA', states: { wak2sonA: { onEntry: 'wak2sonAenter', onExit: 'wak2sonAexit' }, wak2sonB: { onEntry: 'wak2sonBenter', onExit: 'wak2sonBexit' } }, on: { WAK2: '.wak2sonB' }, onEntry: 'wak2enter', onExit: 'wak2exit' } }});
const wordMachine = Machine({ id: 'word', type: 'parallel', states: { bold: { initial: 'off', states: { on: { on: { TOGGLE_BOLD: 'off' } }, off: { on: { TOGGLE_BOLD: 'on' } } } }, underline: { initial: 'off', states: { on: { on: { TOGGLE_UNDERLINE: 'off' } }, off: { on: { TOGGLE_UNDERLINE: 'on' } } } }, italics: { initial: 'off', states: { on: { on: { TOGGLE_ITALICS: 'off' } }, off: { on: { TOGGLE_ITALICS: 'on' } } } }, list: { initial: 'none', states: { none: { on: { BULLETS: 'bullets', NUMBERS: 'numbers' } }, bullets: { on: { NONE: 'none', NUMBERS: 'numbers' } }, numbers: { on: { BULLETS: 'bullets', NONE: 'none' } } } } }, on: { RESET: '#word' // TODO: this should be 'word' or [{ internal: false }] }});
const flatParallelMachine = Machine({ type: 'parallel', states: { foo: {}, bar: {}, baz: { initial: 'one', states: { one: { on: { E: 'two' } }, two: {} } } }});
const raisingParallelMachine = Machine({ strict: true, type: 'parallel', states: { OUTER1: { initial: 'C', states: { A: { onEntry: [raise('TURN_OFF')], on: { EVENT_OUTER1_B: 'B', EVENT_OUTER1_C: 'C' } }, B: { onEntry: [raise('TURN_ON')], on: { EVENT_OUTER1_A: 'A', EVENT_OUTER1_C: 'C' } }, C: { onEntry: [raise('CLEAR')], on: { EVENT_OUTER1_A: 'A', EVENT_OUTER1_B: 'B' } } } }, OUTER2: { type: 'parallel', states: { INNER1: { initial: 'ON', states: { OFF: { on: { TURN_ON: 'ON' } }, ON: { on: { CLEAR: 'OFF' } } } }, INNER2: { initial: 'OFF', states: { OFF: { on: { TURN_ON: 'ON' } }, ON: { on: { TURN_OFF: 'OFF' } } } } } } }});
const nestedParallelState = Machine({ type: 'parallel', states: { OUTER1: { initial: 'STATE_OFF', states: { STATE_OFF: { on: { EVENT_COMPLEX: 'STATE_ON', EVENT_SIMPLE: 'STATE_ON' } }, STATE_ON: { type: 'parallel', states: { STATE_NTJ0: { initial: 'STATE_IDLE_0', states: { STATE_IDLE_0: { on: { EVENT_STATE_NTJ0_WORK: 'STATE_WORKING_0' } }, STATE_WORKING_0: { on: { EVENT_STATE_NTJ0_IDLE: 'STATE_IDLE_0' } } } }, STATE_NTJ1: { initial: 'STATE_IDLE_1', states: { STATE_IDLE_1: { on: { EVENT_STATE_NTJ1_WORK: 'STATE_WORKING_1' } }, STATE_WORKING_1: { on: { EVENT_STATE_NTJ1_IDLE: 'STATE_IDLE_1' } } } } } } } }, OUTER2: { initial: 'STATE_OFF', states: { STATE_OFF: { on: { EVENT_COMPLEX: 'STATE_ON_COMPLEX', EVENT_SIMPLE: 'STATE_ON_SIMPLE' } }, STATE_ON_SIMPLE: {}, STATE_ON_COMPLEX: { type: 'parallel', states: { STATE_INNER1: { initial: 'STATE_OFF', states: { STATE_OFF: {}, STATE_ON: {} } }, STATE_INNER2: { initial: 'STATE_OFF', states: { STATE_OFF: {}, STATE_ON: {} } } } } } } }});
const deepFlatParallelMachine = Machine({ type: 'parallel', states: { X: {}, V: { initial: 'A', on: { a: { target: 'V.A' }, b: { target: 'V.B' }, c: { target: 'V.C' } }, states: { A: {}, B: { initial: 'BB', states: { BB: { type: 'parallel', states: { BBB_A: {}, BBB_B: {} } } } }, C: {} } } }});
describe('parallel states', () => { it('should have initial parallel states', () => { const { initialState } = wordMachine;
expect(initialState.value).toEqual({ bold: 'off', italics: 'off', underline: 'off', list: 'none' }); });
const expected: Record<string, Record<string, StateValue>> = { 'bold.off': { TOGGLE_BOLD: { bold: 'on', italics: 'off', underline: 'off', list: 'none' } }, 'bold.on': { TOGGLE_BOLD: { bold: 'off', italics: 'off', underline: 'off', list: 'none' } }, [JSON.stringify({ bold: 'off', italics: 'off', underline: 'on', list: 'bullets' })]: { 'TOGGLE_BOLD, TOGGLE_ITALICS': { bold: 'on', italics: 'on', underline: 'on', list: 'bullets' }, RESET: { bold: 'off', italics: 'off', underline: 'off', list: 'none' } } };
Object.keys(expected).forEach((fromState) => { Object.keys(expected[fromState]).forEach((eventTypes) => { const toState = expected[fromState][eventTypes];
it(`should go from ${fromState} to ${JSON.stringify( toState )} on ${eventTypes}`, () => { const resultState = testMultiTransition( wordMachine, fromState, eventTypes );
expect(resultState.value).toEqual(toState); }); }); });
it('should have all parallel states represented in the state value', () => { const nextState = wakMachine.transition(wakMachine.initialState, 'WAK1');
expect(nextState.value).toEqual({ wak1: 'wak1sonB', wak2: 'wak2sonA' }); });
it('should have all parallel states represented in the state value (2)', () => { const nextState = wakMachine.transition(wakMachine.initialState, 'WAK2');
expect(nextState.value).toEqual({ wak1: 'wak1sonA', wak2: 'wak2sonB' }); });
it('should work with regions without states', () => { expect(flatParallelMachine.initialState.value).toEqual({ foo: {}, bar: {}, baz: 'one' }); });
it('should work with regions without states', () => { const nextState = flatParallelMachine.transition( flatParallelMachine.initialState, 'E' ); expect(nextState.value).toEqual({ foo: {}, bar: {}, baz: 'two' }); });
it('should properly transition to relative substate', () => { const nextState = composerMachine.transition( composerMachine.initialState, 'singleClickActivity' );
expect(nextState.value).toEqual({ ReadOnly: { StructureEdit: { SelectionStatus: 'SelectedActivity', ClipboardStatus: 'Empty' } } }); });
it('should properly transition according to onEntry events on an initial state', () => { expect(raisingParallelMachine.initialState.value).toEqual({ OUTER1: 'C', OUTER2: { INNER1: 'OFF', INNER2: 'OFF' } }); });
it('should properly transition when raising events for a parallel state', () => { const nextState = raisingParallelMachine.transition( raisingParallelMachine.initialState, 'EVENT_OUTER1_B' );
expect(nextState.value).toEqual({ OUTER1: 'B', OUTER2: { INNER1: 'ON', INNER2: 'ON' } }); });
xit('should handle simultaneous orthogonal transitions', () => { type Events = { type: 'CHANGE'; value: string } | { type: 'SAVE' }; const simultaneousMachine = Machine<{ value: string }, Events>({ id: 'yamlEditor', type: 'parallel', context: { value: '' }, states: { editing: { on: { CHANGE: { actions: assign({ value: (_, e) => e.value }) } } }, status: { initial: 'unsaved', states: { unsaved: { on: { SAVE: { target: 'saved', actions: 'save' } } }, saved: { on: { CHANGE: 'unsaved' } } } } } });
const savedState = simultaneousMachine.transition( simultaneousMachine.initialState, 'SAVE' ); const unsavedState = simultaneousMachine.transition(savedState, { type: 'CHANGE', value: 'something' });
expect(unsavedState.value).toEqual({}); });
describe('transitions with nested parallel states', () => { const initialState = nestedParallelState.initialState; const simpleNextState = nestedParallelState.transition( initialState, 'EVENT_SIMPLE' ); const complexNextState = nestedParallelState.transition( initialState, 'EVENT_COMPLEX' );
it('should properly transition when in a simple nested state', () => { const nextState = nestedParallelState.transition( simpleNextState, 'EVENT_STATE_NTJ0_WORK' );
expect(nextState.value).toEqual({ OUTER1: { STATE_ON: { STATE_NTJ0: 'STATE_WORKING_0', STATE_NTJ1: 'STATE_IDLE_1' } }, OUTER2: 'STATE_ON_SIMPLE' }); });
it('should properly transition when in a complex nested state', () => { const nextState = nestedParallelState.transition( complexNextState, 'EVENT_STATE_NTJ0_WORK' );
expect(nextState.value).toEqual({ OUTER1: { STATE_ON: { STATE_NTJ0: 'STATE_WORKING_0', STATE_NTJ1: 'STATE_IDLE_1' } }, OUTER2: { STATE_ON_COMPLEX: { STATE_INNER1: 'STATE_OFF', STATE_INNER2: 'STATE_OFF' } } }); }); });
// https://github.com/statelyai/xstate/issues/191 describe('nested flat parallel states', () => { const machine = Machine({ initial: 'A', states: { A: { on: { 'to-B': 'B' } }, B: { type: 'parallel', states: { C: {}, D: {} } } }, on: { 'to-A': 'A' } });
it('should represent the flat nested parallel states in the state value', () => { const result = machine.transition(machine.initialState, 'to-B');
expect(result.value).toEqual({ B: { C: {}, D: {} } }); }); });
describe('deep flat parallel states', () => { it('should properly evaluate deep flat parallel states', () => { const state1 = deepFlatParallelMachine.transition( deepFlatParallelMachine.initialState, 'a' ); const state2 = deepFlatParallelMachine.transition(state1, 'c'); const state3 = deepFlatParallelMachine.transition(state2, 'b'); expect(state3.value).toEqual({ V: { B: { BB: { BBB_A: {}, BBB_B: {} } } }, X: {} }); });
it('should not overlap resolved state configuration in state resolution', () => { const machine = Machine({ id: 'pipeline', type: 'parallel', states: { foo: { on: { UPDATE: { actions: () => { /* do nothing */ } } } }, bar: { on: { UPDATE: '.baz' }, initial: 'idle', states: { idle: {}, baz: {} } } } });
expect(() => { machine.transition(machine.initialState, 'UPDATE'); }).not.toThrow(); }); });
describe('other', () => { // https://github.com/statelyai/xstate/issues/518 it('regions should be able to transition to orthogonal regions', () => { const testMachine = Machine({ id: 'app', type: 'parallel', states: { Pages: { id: 'Pages', initial: 'About', states: { About: { id: 'About', on: { dashboard: '#Dashboard' } }, Dashboard: { id: 'Dashboard', on: { about: '#About' } } } }, Menu: { id: 'Menu', initial: 'Closed', states: { Closed: { id: 'Closed', on: { toggle: '#Opened' } }, Opened: { id: 'Opened', on: { toggle: '#Closed', 'go to dashboard': '#Dashboard' } } } } } });
const openMenuState = testMachine.transition( testMachine.initialState, 'toggle' );
const dashboardState = testMachine.transition( openMenuState, 'go to dashboard' );
expect( dashboardState.matches({ Menu: 'Opened', Pages: 'Dashboard' }) ).toBe(true); });
// https://github.com/statelyai/xstate/issues/531 it('should calculate the entry set for external transitions in parallel states', () => { const testMachine = Machine<{ log: string[] }>({ id: 'test', context: { log: [] }, type: 'parallel', states: { foo: { initial: 'foobar', states: { foobar: { on: { GOTO_FOOBAZ: 'foobaz' } }, foobaz: { entry: assign({ log: (ctx) => [...ctx.log, 'entered foobaz'] }), on: { GOTO_FOOBAZ: 'foobaz' } } } }, bar: {} } });
const run1 = testMachine.transition( testMachine.initialState, 'GOTO_FOOBAZ' ); const run2 = testMachine.transition(run1, 'GOTO_FOOBAZ');
expect(run2.context.log.length).toBe(2); }); });
it('should raise a "done.state.*" event when all child states reach final state', (done) => { const machine = createMachine({ id: 'test', initial: 'p', states: { p: { type: 'parallel', states: { a: { initial: 'idle', states: { idle: { on: { FINISH: 'finished' } }, finished: { type: 'final' } } }, b: { initial: 'idle', states: { idle: { on: { FINISH: 'finished' } }, finished: { type: 'final' } } }, c: { initial: 'idle', states: { idle: { on: { FINISH: 'finished' } }, finished: { type: 'final' } } } }, onDone: 'success' }, success: { type: 'final' } } });
const service = interpret(machine) .onDone(() => { done(); }) .start();
service.send('FINISH'); });
it('should raise a "done.state.*" event when a pseudostate of a history type is directly on a parallel state', () => { const machine = createMachine({ initial: 'parallelSteps', states: { parallelSteps: { type: 'parallel', states: { hist: { type: 'history' }, one: { initial: 'wait_one', states: { wait_one: { on: { finish_one: { target: 'done' } } }, done: { type: 'final' } } }, two: { initial: 'wait_two', states: { wait_two: { on: { finish_two: { target: 'done' } } }, done: { type: 'final' } } } }, onDone: 'finished' }, finished: {} } });
const service = interpret(machine).start();
service.send({ type: 'finish_one' }); service.send({ type: 'finish_two' });
expect(service.state.value).toBe('finished'); });});
Version Info