forked from TechEmpower/react-governor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Counter.spec.js
executable file
·173 lines (121 loc) · 4.5 KB
/
Counter.spec.js
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
import React from "react";
import ReactDOM from "react-dom";
import { act } from "react-dom/test-utils";
import TestRenderer from "react-test-renderer";
import Counter from "../../examples/counter/Counter";
it("renders without crashing", () => {
const div = document.createElement("div");
ReactDOM.render(<Counter />, div);
});
it("starts with initial state", () => {
const counter = TestRenderer.create(<Counter/>);
const val = counter.root.findByProps({className: "val"});
expect(val.props.value).toBe(0);
});
it("can increment", async () => {
const counter = TestRenderer.create(<Counter/>);
const val = counter.root.findByProps({className: "val"});
const button = counter.root.findByProps({className: "inc"});
await act(async () => {
expect(val.props.value).toBe(0);
await button.props.onClick();
expect(val.props.value).toBe(1);
await button.props.onClick();
expect(val.props.value).toBe(2);
});
});
it("can decrement", async () => {
const counter = TestRenderer.create(<Counter/>);
const val = counter.root.findByProps({className: "val"});
const button = counter.root.findByProps({className: "dec"});
await act(async () => {
expect(val.props.value).toBe(0);
await button.props.onClick();
expect(val.props.value).toBe(-1);
await button.props.onClick();
expect(val.props.value).toBe(-2);
});
});
it("can add5", async () => {
const counter = TestRenderer.create(<Counter/>);
const val = counter.root.findByProps({className: "val"});
const button = counter.root.findByProps({className: "add5"});
await act(async () => {
expect(val.props.value).toBe(0);
await button.props.onClick();
expect(val.props.value).toBe(5);
await button.props.onClick();
expect(val.props.value).toBe(10);
});
});
it("can set", async () => {
const counter = TestRenderer.create(<Counter />);
const val = counter.root.findByProps({ className: "val" });
const button = counter.root.findByProps({ className: "set" });
await act(async () => {
expect(val.props.value).toBe(0);
await button.props.onClick();
expect(val.props.value).toBe(137);
await button.props.onClick();
expect(val.props.value).toBe(137);
});
});
it("can addSum", async () => {
const counter = TestRenderer.create(<Counter />);
const val = counter.root.findByProps({ className: "val" });
const button = counter.root.findByProps({ className: "addSum" });
await act(async () => {
expect(val.props.value).toBe(0);
await button.props.onClick();
expect(val.props.value).toBe(7);
await button.props.onClick();
expect(val.props.value).toBe(14);
});
});
it("can addNewState", async () => {
const counter = TestRenderer.create(<Counter />);
const val = counter.root.findByProps({ className: "val" });
const newState = counter.root.findByProps({ className: "newState" });
const button = counter.root.findByProps({ className: "addNewState" });
await act(async () => {
expect(newState.props.value).toBe(undefined);
await button.props.onClick();
expect(newState.props.value).toBe("Hello");
expect(val.props.value).toBe(0);
});
});
it("can removeState", async () => {
const counter = TestRenderer.create(<Counter />);
const val = counter.root.findByProps({ className: "val" });
const newState = counter.root.findByProps({ className: "newState" });
const button = counter.root.findByProps({ className: "addNewState" });
const removeButton = counter.root.findByProps({ className: "removeState" });
await act(async() => {
expect(newState.props.value).toBe(undefined);
await button.props.onClick();
expect(newState.props.value).toBe("Hello");
expect(val.props.value).toBe(0);
await removeButton.props.onClick();
expect(val.props.value).toBe(undefined);
});
});
it("can asyncFunc", async () => {
const counter = TestRenderer.create(<Counter />);
const val = counter.root.findByProps({ className: "val" });
const button = counter.root.findByProps({ className: "asyncFunc" });
await act(async () => {
expect(val.props.value).toBe(0);
await button.props.onClick();
expect(val.props.value).toBe(256);
});
});
it("can fetchGoogle", async () => {
const counter = TestRenderer.create(<Counter />);
const val = counter.root.findByProps({ className: "googleStatus" });
const button = counter.root.findByProps({ className: "fetchGoogle" });
await act(async () => {
expect(val.props.value).toBe(undefined);
await button.props.onClick();
expect(val.props.value).toBe(200);
});
});