-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorderPhase.test.jsx
More file actions
166 lines (134 loc) · 5.59 KB
/
orderPhase.test.jsx
File metadata and controls
166 lines (134 loc) · 5.59 KB
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
import {render, screen} from "@testing-library/react";
import App from "../App";
import userEvent from "@testing-library/user-event";
test("Order phases for happy path", async () => {
// render app
// Don't need to wrap in provider; already wrapped!
render(<App />);
// add ice cream scoops and toppings
const vanillaInput = await screen.findByRole("spinbutton", {
name: "Vanilla",
});
userEvent.clear(vanillaInput);
userEvent.type(vanillaInput, "1");
const chocolateInput = screen.getByRole("spinbutton", {name: "Chocolate"});
userEvent.clear(chocolateInput);
userEvent.type(chocolateInput, "2");
const cherriesCheckbox = await screen.findByRole("checkbox", {
name: "Cherries",
});
userEvent.click(cherriesCheckbox);
// find and click order summary button
const orderSummaryButton = screen.getByRole("button", {
name: /order sundae/i,
});
userEvent.click(orderSummaryButton);
// check summary subtotals
const summaryHeading = screen.getByRole("heading", {name: "Order Summary"});
expect(summaryHeading).toBeInTheDocument();
const scoopsHeading = screen.getByRole("heading", {name: "Scoops: $6.00"});
expect(scoopsHeading).toBeInTheDocument();
const toppingsHeading = screen.getByRole("heading", {
name: "Toppings: $1.50",
});
expect(toppingsHeading).toBeInTheDocument();
// check summary option items
expect(screen.getByText("1 Vanilla")).toBeInTheDocument();
expect(screen.getByText("2 Chocolate")).toBeInTheDocument();
expect(screen.getByText("Cherries")).toBeInTheDocument();
// // alternatively...
// // const optionItems = screen.getAllByRole('listitem');
// // const optionItemsText = optionItems.map((item) => item.textContent);
// // expect(optionItemsText).toEqual(['1 Vanilla', '2 Chocolate', 'Cherries']);
// accept terms and click button
const tcCheckbox = screen.getByRole("checkbox", {
name: /terms and conditions/i,
});
userEvent.click(tcCheckbox);
const confirmOrderButton = screen.getByRole("button", {
name: /confirm order/i,
});
userEvent.click(confirmOrderButton);
// Expect "loading" to show
const loading = screen.getByText(/loading/i);
expect(loading).toBeInTheDocument();
// check confirmation page text
// this one is async because there is a POST request to server in between summary
// and confirmation pages
const thankYouHeader = await screen.findByRole("heading", {
name: /thank you/i,
});
expect(thankYouHeader).toBeInTheDocument();
// expect that loading has disappeared
const notLoading = screen.queryByText("loading");
expect(notLoading).not.toBeInTheDocument();
const orderNumber = await screen.findByText(/order number/i);
expect(orderNumber).toBeInTheDocument();
// find and click "new order" button on confirmation page
const newOrderButton = screen.getByRole("button", {name: /new order/i});
userEvent.click(newOrderButton);
// check that scoops and toppings have been reset
const scoopsTotal = await screen.findByText("Scoops total: $0.00");
expect(scoopsTotal).toBeInTheDocument();
const toppingsTotal = screen.getByText("Toppings total: $0.00");
expect(toppingsTotal).toBeInTheDocument();
// wait for items to appear so that Testing Library doesn't get angry about stuff
// happening after test is over
await screen.findByRole("spinbutton", {name: "Vanilla"});
await screen.findByRole("checkbox", {name: "Cherries"});
});
test("Toppings header is not on summary page if no toppings ordered", async () => {
// render app
render(<App />);
// add ice cream scoops but no toppings
const vanillaInput = await screen.findByRole("spinbutton", {
name: "Vanilla",
});
userEvent.clear(vanillaInput);
userEvent.type(vanillaInput, "1");
const chocolateInput = screen.getByRole("spinbutton", {name: "Chocolate"});
userEvent.clear(chocolateInput);
userEvent.type(chocolateInput, "2");
// find and click order summary button
const orderSummaryButton = screen.getByRole("button", {
name: /order sundae/i,
});
userEvent.click(orderSummaryButton);
const scoopsHeading = screen.getByRole("heading", {name: "Scoops: $6.00"});
expect(scoopsHeading).toBeInTheDocument();
const toppingsHeading = screen.queryByRole("heading", {name: /toppings/i});
expect(toppingsHeading).not.toBeInTheDocument();
});
// Additional test to verify fix to erroneous code
// See https://www.udemy.com/course/react-testing-library/learn/#questions/17055082/
test("Toppings header is not on summary page if toppings ordered, then removed", async () => {
// render app
render(<App />);
// add ice cream scoops
const vanillaInput = await screen.findByRole("spinbutton", {
name: "Vanilla",
});
userEvent.clear(vanillaInput);
userEvent.type(vanillaInput, "1");
// add a topping and confirm
const cherriesTopping = await screen.findByRole("checkbox", {
name: "Cherries",
});
userEvent.click(cherriesTopping);
expect(cherriesTopping).toBeChecked();
const toppingsTotal = screen.getByText("Toppings total: $", {exact: false});
expect(toppingsTotal).toHaveTextContent("1.50");
// remove the topping
userEvent.click(cherriesTopping);
expect(cherriesTopping).not.toBeChecked();
expect(toppingsTotal).toHaveTextContent("0.00");
// find and click order summary button
const orderSummaryButton = screen.getByRole("button", {
name: /order sundae/i,
});
userEvent.click(orderSummaryButton);
const scoopsHeading = screen.getByRole("heading", {name: "Scoops: $2.00"});
expect(scoopsHeading).toBeInTheDocument();
const toppingsHeading = screen.queryByRole("heading", {name: /toppings/i});
expect(toppingsHeading).not.toBeInTheDocument();
});