deno.land / x / xstate@xstate@4.33.6 / test / rehydration.test.ts

rehydration.test.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { 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']); }); });});
xstate

Version Info

Tagged at
2 years ago