-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.spec.tsx
190 lines (160 loc) · 4.66 KB
/
index.spec.tsx
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import * as React from "react";
import * as TestRenderer from "react-test-renderer";
import { get as lodashGet } from "lodash";
import { useMachine } from ".";
import { MachineConfig } from "xstate";
// simple ----------------------------------
enum SimpleStateName {
GREEN = "GREEN",
YELLOW = "YELLOW",
RED = "RED"
}
interface SimpleStateSchema {
states: {
[SimpleStateName.GREEN]: {};
[SimpleStateName.YELLOW]: {};
[SimpleStateName.RED]: {};
};
}
enum SimpleAction {
TIMER = "TIMER"
}
type SimpleEvents = { type: SimpleAction.TIMER };
interface SimpleContext {
counter: number;
}
const SimpleInitialContext: SimpleContext = {
counter: 0
};
const simpleMachineConfig: MachineConfig<
SimpleContext,
SimpleStateSchema,
SimpleEvents
> = {
id: "light",
initial: SimpleStateName.GREEN,
states: {
[SimpleStateName.GREEN]: {
on: { TIMER: SimpleStateName.YELLOW }
},
[SimpleStateName.YELLOW]: {
on: { TIMER: SimpleStateName.RED }
},
[SimpleStateName.RED]: {
on: { TIMER: SimpleStateName.GREEN }
}
}
};
// side effect ----------------------------------
enum SimpleStateNameWithSideEffect {
OFF = "OFF",
ON = "ON"
}
interface SimpleStateSchemaWithSideEffect {
states: {
[SimpleStateNameWithSideEffect.OFF]: {};
[SimpleStateNameWithSideEffect.ON]: {};
};
}
enum SimpleActionWithSideEffect {
ACTIVATE = "ACTIVATE",
DEACTIVATE = "DEACTIVATE"
}
enum SimpleCallbackActionWithSideEffect {
SWITCHED = "SWITCHED"
}
type SimpleEventsWithSideEffect =
| { type: SimpleActionWithSideEffect.ACTIVATE }
| { type: SimpleActionWithSideEffect.DEACTIVATE };
interface SimpleContextWithSideEffect {
counter: number;
}
const simpleInitialContextWithSideEffect: SimpleContextWithSideEffect = {
counter: 0
};
const simpleMachineConfigWithSideEffect: MachineConfig<
SimpleContextWithSideEffect,
SimpleStateSchemaWithSideEffect,
SimpleEventsWithSideEffect
> = {
id: "switch",
initial: SimpleStateNameWithSideEffect.OFF,
states: {
[SimpleStateNameWithSideEffect.OFF]: {
on: {
"": {
target: SimpleStateNameWithSideEffect.ON,
actions: [SimpleCallbackActionWithSideEffect.SWITCHED]
}
}
},
[SimpleStateNameWithSideEffect.ON]: {}
}
};
describe("testing useMachine", () => {
it("should successfully render a component with a simple machine and call useEffect", () => {
const spy = jest.spyOn<any, any>(React, "useEffect");
const SimpleTestComponent = () => {
const machine = useMachine(simpleMachineConfig, {}, SimpleInitialContext);
return <div>{machine.state.value}</div>;
};
let testComponent = TestRenderer.create(<SimpleTestComponent />);
const renderedJSON = testComponent.toJSON();
expect(lodashGet(renderedJSON, "children[0]")).toBe(SimpleStateName.GREEN);
testComponent && testComponent.unmount();
expect(spy).toHaveBeenCalled();
spy.mockRestore();
});
it("should successfully re-render an updated component when send is called to change machine state", () => {
const TestComponentWithAction = () => {
const machine = useMachine(simpleMachineConfig, {}, SimpleInitialContext);
const sendTimer = () => {
machine.send(SimpleAction.TIMER);
};
return (
<React.Fragment>
<div>{machine.state.value}</div>
<button onClick={sendTimer}></button>
</React.Fragment>
);
};
const testComponent = TestRenderer.create(<TestComponentWithAction />);
// check starting component state
const initialRenderedJSON = testComponent.toJSON();
expect(lodashGet(initialRenderedJSON, "[0].children[0]")).toBe(
SimpleStateName.GREEN
);
// click button
TestRenderer.act(
() =>
testComponent.root &&
testComponent.root.findByType("button").props.onClick()
);
const afterClickRenderedJSON = testComponent.toJSON();
// check component state after click
expect(lodashGet(afterClickRenderedJSON, "[0].children[0]")).toBe(
SimpleStateName.YELLOW
);
});
it("should successfully execute a side-effect when one is included in the machine config", () => {
const spy = jest.fn();
const actionsMock = {
switched: spy
};
const TestComponentWithSideEffect = () => {
const machine = useMachine(
simpleMachineConfigWithSideEffect,
{
actions: {
[SimpleCallbackActionWithSideEffect.SWITCHED]: actionsMock.switched
}
},
simpleInitialContextWithSideEffect
);
return null;
};
TestRenderer.create(<TestComponentWithSideEffect />);
expect(spy).toHaveBeenCalled();
spy.mockRestore();
});
});