-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
104 lines (84 loc) · 2.24 KB
/
index.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
103
104
import { describe, expect, test } from 'vitest';
import { InterpreterStatus } from 'xstate';
import { createInterpret } from './createInterpret';
import { machine } from './fixtures/test.machine';
const {
start,
stop,
sender,
state,
reducer,
matches,
context,
send,
status,
tags,
} = createInterpret(machine);
describe('Acceptation', () => {
const expectFn = (arg: unknown) => expect(arg).toBeInstanceOf(Function);
test.concurrent('start is function', () => {
expectFn(start);
});
test.concurrent('stop is function', () => {
expectFn(stop);
});
test.concurrent('sender is function', () => {
expectFn(sender);
});
test.concurrent('state is function', () => {
expectFn(state);
});
test.concurrent('context is function', () => {
expectFn(context);
});
test.concurrent('matches is function', () => {
expectFn(matches);
});
test.concurrent('reducer is function', () => {
expectFn(reducer);
});
test.concurrent('send is function', () => {
expectFn(send);
});
test.concurrent('tags is function', () => {
expectFn(tags);
});
});
describe('Workflow', () => {
test('#0 -> Start the machine', () => {
expect(status()).toBe(InterpreterStatus.NotStarted);
start();
expect(status()).toBe(InterpreterStatus.Running);
});
test('#1 -> Current state is "off"', () => {
const matcher = matches('off');
expect(matcher()).toBe(true);
});
test('#2 -> Current iterator is "0"', () => {
const currentContext = context();
expect(currentContext().iterator).toBe(0);
});
test('#3 -> Send "TOGGLE" event', () => {
const toggle = sender('TOGGLE');
toggle();
});
test('#4 -> Current state is "on"', () => {
const matcher = matches('on');
expect(matcher()).toBe(true);
});
test('#5 -> The current state has a tag : "busy"', () => {
const hasTag = tags('busy');
expect(hasTag()).toBe(true);
});
test('#6 -> Send "TOGGLE" event again', () => {
send('TOGGLE');
});
test('#7 -> Current iterator is now "2"', () => {
const iterator = context(context => context.iterator);
expect(iterator()).toBe(2);
});
test('#8 -> Stop the machine', () => {
stop();
expect(status()).toBe(InterpreterStatus.Stopped);
});
});