-
Notifications
You must be signed in to change notification settings - Fork 50
/
add-wallet.test.tsx
189 lines (126 loc) · 5.31 KB
/
add-wallet.test.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
import userEvent from "@testing-library/user-event";
import {
failedPostAccountRequest,
failedWalletsRequest,
} from "./mocks/handlers";
import { server } from "./mocks/server";
import {
renderRoot,
screen,
waitFor,
waitForElementToBeRemoved,
within,
} from "./utils";
test("clicking 'Add new wallet' button opens the modal", async () => {
renderRoot();
await waitForElementToBeRemoved(() => screen.getByLabelText("Loading..."));
expect(screen.queryByTestId("modal")).not.toBeInTheDocument();
// open modal
userEvent.click(screen.getByText(/Add new wallet/i));
await waitFor(() => screen.getByTestId("modal"));
});
test("clicking the 'Close icon' button in the modal closes the modal", async () => {
renderRoot();
await waitForElementToBeRemoved(() => screen.getByLabelText("Loading..."));
expect(screen.queryByTestId("modal")).not.toBeInTheDocument();
// open modal
userEvent.click(screen.getByText(/Add new wallet/i));
const modal = await screen.findByTestId("modal");
expect(modal).toBeInTheDocument();
const closeButton = within(modal).getByLabelText(/Close button/i);
expect(closeButton).toBeInTheDocument();
userEvent.click(closeButton);
await waitFor(() =>
expect(screen.queryByTestId("modal")).not.toBeInTheDocument()
);
});
test("shows 'Network error' when GET'/accounts' fails", async () => {
server.use(failedWalletsRequest);
renderRoot();
await waitForElementToBeRemoved(() => screen.getByLabelText("Loading..."));
userEvent.click(screen.getByText(/Add new wallet/i));
const modal = await screen.findByTestId("modal");
const withinModal = within(modal);
// fetch available wallets
await waitFor(() =>
expect(withinModal.queryByLabelText("Loading...")).not.toBeInTheDocument()
);
const errorMessage = await withinModal.findByText(/Network error/i);
const tryButton = await withinModal.findByText(/try again/i);
expect(errorMessage).toBeInTheDocument();
expect(tryButton).toBeInTheDocument();
});
test("makes request when 'Try again' button is clicked on error for GET'/accounts'", async () => {
server.use(failedWalletsRequest);
renderRoot();
await waitForElementToBeRemoved(() => screen.getByLabelText("Loading..."));
userEvent.click(screen.getByText(/Add new wallet/i));
const modal = await screen.findByTestId("modal");
const withinModal = within(modal);
// fetch available wallets
await waitFor(() =>
expect(withinModal.queryByLabelText("Loading...")).not.toBeInTheDocument()
);
const tryButton = await withinModal.findByText(/try again/i);
userEvent.click(tryButton);
expect(await withinModal.findByLabelText("Loading...")).toBeInTheDocument();
});
test("'Add new wallet' form", async () => {
renderRoot();
await waitForElementToBeRemoved(() => screen.getByLabelText("Loading..."));
userEvent.click(screen.getByText(/Add new wallet/i));
const modal = await screen.findByTestId("modal");
const withinModal = within(modal);
// fetch available wallets
await waitFor(() =>
expect(withinModal.queryByLabelText("Loading...")).not.toBeInTheDocument()
);
expect(withinModal.getByText(/Add new wallet/i)).toBeInTheDocument();
expect(withinModal.getByText(/Create wallet/i)).toBeInTheDocument();
const walletSelect = screen.getByRole("combobox");
expect(walletSelect).toBeInTheDocument();
});
test("can submit wallet form successfully POST'/accounts'", async () => {
renderRoot();
await waitForElementToBeRemoved(() => screen.getByLabelText("Loading..."));
userEvent.click(screen.getByText(/Add new wallet/i));
const withinModal = within(screen.getByTestId("modal"));
// fetch available wallets
await waitFor(() =>
expect(withinModal.queryByLabelText("Loading...")).not.toBeInTheDocument()
);
const submitButton = withinModal.getByText(/Create wallet/i);
const walletSelect = screen.getByRole("combobox");
userEvent.selectOptions(walletSelect, "XLM");
userEvent.click(submitButton);
expect(within(submitButton).getByLabelText("Loading...")).toBeInTheDocument();
// close modal when request is successful
await waitFor(() =>
expect(screen.queryByTestId("modal")).not.toBeInTheDocument()
);
const accountName = await screen.findByText(/Stellar/i);
const accountBalance = await screen.findByText("0");
const accountCurrency = await screen.findByText("XLM");
expect(accountName).toBeInTheDocument();
expect(accountBalance).toBeInTheDocument();
expect(accountCurrency).toBeInTheDocument();
});
test("shows error when POST'/accounts' fails", async () => {
server.use(failedPostAccountRequest);
renderRoot();
await waitForElementToBeRemoved(() => screen.getByLabelText("Loading..."));
userEvent.click(screen.getByText(/Add new wallet/i));
const withinModal = within(screen.getByTestId("modal"));
// fetch available wallets
await waitFor(() =>
expect(withinModal.queryByLabelText("Loading...")).not.toBeInTheDocument()
);
const submitButton = withinModal.getByText(/Create wallet/i);
const walletSelect = screen.getByRole("combobox");
userEvent.selectOptions(walletSelect, "XLM");
userEvent.click(submitButton);
expect(within(submitButton).getByLabelText("Loading...")).toBeInTheDocument();
const errorMessage = await withinModal.findByText(/Network error/i);
expect(errorMessage).toBeInTheDocument();
expect(submitButton).toBeInTheDocument();
});