deno.land / x / xstate@xstate@4.33.6 / test / rehydration.test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102import { createMachine, interpret, State } from '../src';
describe('rehydration', () => { describe('using persisted state', () => { it('should be able to use `hasTag` immediately', () => { const machine = createMachine({ initial: 'a', states: { a: { tags: 'foo' } } });
const persistedState = JSON.stringify(machine.initialState); const restoredState = State.create(JSON.parse(persistedState));
const service = interpret(machine).start(restoredState);
expect(service.state.hasTag('foo')).toBe(true); });
it('should call exit actions when machine gets stopped immediately', () => { const actual: string[] = []; const machine = createMachine({ exit: () => actual.push('root'), initial: 'a', states: { a: { exit: () => actual.push('a') } } });
const persistedState = JSON.stringify(machine.initialState); const restoredState = State.create(JSON.parse(persistedState));
interpret(machine).start(restoredState).stop();
expect(actual).toEqual(['a', 'root']); });
it('should get correct result back from `can` immediately', () => { const machine = createMachine({ on: { FOO: { actions: () => {} } } });
const persistedState = JSON.stringify(interpret(machine).start().state); const restoredState = JSON.parse(persistedState); const service = interpret(machine).start(restoredState);
expect(service.state.can('FOO')).toBe(true); }); });
describe('using state value', () => { it('should be able to use `hasTag` immediately', () => { const machine = createMachine({ initial: 'inactive', states: { inactive: { on: { NEXT: 'active' } }, active: { tags: 'foo' } } });
const service = interpret(machine);
service.start('active');
expect(service.state.hasTag('foo')).toBe(true); });
it('should call exit actions when machine gets stopped immediately', () => { const actual: string[] = []; const machine = createMachine({ exit: () => actual.push('root'), initial: 'inactive', states: { inactive: { on: { NEXT: 'active' } }, active: { exit: () => actual.push('active') } } });
interpret(machine).start('active').stop();
expect(actual).toEqual(['active', 'root']); }); });});
Version Info