-
Notifications
You must be signed in to change notification settings - Fork 6
/
CounterProvider.spec.js
executable file
·134 lines (111 loc) · 3.7 KB
/
CounterProvider.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
import React from "react";
import ReactDOM from "react-dom";
import { act } from "react-dom/test-utils";
import TestRenderer from "react-test-renderer";
import CounterProvider from "../../examples/counter-provider/CounterProvider";
import CounterChildOne from "../../examples/counter-provider/CounterChildOne";
import CounterChildTwo from "../../examples/counter-provider/CounterChildTwo";
import CounterParent from "../../examples/counter-provider/CounterParent";
import CounterGrandchild from "../../examples/counter-provider/CounterGrandchildOne";
it("renders without crashing", () => {
const div = document.createElement("div");
ReactDOM.render(<CounterProvider />, div);
});
it("starts with initial state", () => {
const counter = TestRenderer.create(
<CounterProvider>
<CounterChildOne />
<CounterChildTwo />
</CounterProvider>
);
const val = counter.root
.findByType(CounterChildOne)
.findByType("div")
.findByProps({ className: "val" });
const val2 = counter.root
.findByType(CounterChildTwo)
.findByType("div")
.findByProps({ className: "val" });
expect(val.props.value).toBe(0);
expect(val2.props.value).toBe(0);
});
it("shares state among sibling context consumers", async () => {
const counter = TestRenderer.create(
<CounterProvider>
<CounterChildOne />
<CounterChildTwo />
</CounterProvider>
);
const val = counter.root
.findByType(CounterChildOne)
.findByType("div")
.findByProps({ className: "val" });
const buttonOneInc = counter.root
.findByType(CounterChildOne)
.findByProps({ className: "inc" });
const val2 = counter.root
.findByType(CounterChildTwo)
.findByType("div")
.findByProps({ className: "val" });
const buttonTwoDec = counter.root
.findByType(CounterChildTwo)
.findByProps({ className: "dec" });
await act(async () => {
expect(val.props.value).toBe(0);
expect(val2.props.value).toBe(0);
await buttonOneInc.props.onClick();
expect(val.props.value).toBe(1);
expect(val2.props.value).toBe(1);
await buttonTwoDec.props.onClick();
expect(val.props.value).toBe(0);
expect(val2.props.value).toBe(0);
});
});
it("shares state among sibling and children context consumers", async () => {
const counter = TestRenderer.create(
<CounterProvider>
<CounterChildOne />
<CounterChildTwo />
<CounterParent>
<CounterGrandchild />
</CounterParent>
</CounterProvider>
);
const val = counter.root
.findByType(CounterChildOne)
.findByType("div")
.findByProps({ className: "val" });
const buttonOneInc = counter.root
.findByType(CounterChildOne)
.findByProps({ className: "inc" });
const val2 = counter.root
.findByType(CounterChildTwo)
.findByType("div")
.findByProps({ className: "val" });
const buttonTwoDec = counter.root
.findByType(CounterChildTwo)
.findByProps({ className: "dec" });
const gval = counter.root
.findByType(CounterGrandchild)
.findByProps({ className: "val" });
const gbutton = counter.root
.findByType(CounterGrandchild)
.findByProps({ className: "inc" });
await act(async () => {
expect(val.props.value).toBe(0);
expect(val2.props.value).toBe(0);
expect(gval.props.value).toBe(0);
await buttonOneInc.props.onClick();
expect(val.props.value).toBe(1);
expect(val2.props.value).toBe(1);
expect(gval.props.value).toBe(1);
await buttonTwoDec.props.onClick();
expect(val.props.value).toBe(0);
expect(val2.props.value).toBe(0);
expect(gval.props.value).toBe(0);
await gbutton.props.onClick();
expect(val.props.value).toBe(1);
expect(val2.props.value).toBe(1);
expect(gval.props.value).toBe(1);
});
});