-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateUserDialog.test.tsx
More file actions
182 lines (166 loc) · 5.47 KB
/
CreateUserDialog.test.tsx
File metadata and controls
182 lines (166 loc) · 5.47 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { test, describe, expect, vitest } from "vitest";
import { render, screen, waitFor } from "@testing-library/react";
import { MockedProvider } from "@apollo/client/testing/react";
import { CREATE_USER_MUTATION } from "./CreateUserDialog";
import CreateUserDialog from "./CreateUserDialog";
import userEvent from "@testing-library/user-event";
import { MockLink } from "@apollo/client/testing";
describe("mutations", () => {
test("calls the createUser mutation when the form is submitted", async () => {
// Set up a mock mutation result. This uses the form of MockedResponse['result]
// that is a function which returns a mock result.
// This function is called by Apollo on-demand only when response is needed. This
// allows us to assert that a specific query/mutation was actually called and the
// response was requested.
const onMutationResult = vitest.fn(
() =>
({
data: {
createUser: {
id: "1",
name: "John Doe",
email: "john.doe@example.com",
role: "admin",
},
},
// This is simply MockedResponse['result'] in older versions of Apollo Client.
} satisfies MockLink.MockedResponse["result"])
);
render(
<MockedProvider
mocks={[
{
request: {
query: CREATE_USER_MUTATION,
variables: {
name: "John Doe",
email: "john.doe@example.com",
role: "admin",
},
},
result: onMutationResult,
},
]}
>
<CreateUserDialog
isOpen={true}
onClose={() => {}}
onSuccess={() => {}}
onError={() => {}}
/>
</MockedProvider>
);
await userEvent.type(screen.getByLabelText("Name"), "John Doe");
await userEvent.type(
screen.getByLabelText("Email"),
"john.doe@example.com"
);
await userEvent.type(screen.getByLabelText("Role"), "admin");
// We haven't clicked the button yet, so the mutation hasn't been called.
expect(onMutationResult).not.toHaveBeenCalled();
await userEvent.click(screen.getByRole("button", { name: "Create" }));
// Now the mutation should have been called.
await waitFor(() => expect(onMutationResult).toHaveBeenCalled());
});
test("calls onClose and onSuccess when user creation is successful", async () => {
// Same as above. If your/your team's brain works differently, you could define
// this and the entire MockedResponse once, but make sure you clear the call-counts
// before each test.
// I prefer this approach because the tests become more self-contained and it's
// easy to cross-reference the values we're typing into the form, the values
// in the request, and the values in the response.
const onMutationResult = vitest.fn(
() =>
({
data: {
createUser: {
id: "1",
name: "John Doe",
email: "john.doe@example.com",
role: "admin",
},
},
// This is simply MockedResponse['result'] in older versions of Apollo Client.
} satisfies MockLink.MockedResponse["result"])
);
const onClose = vitest.fn();
const onSuccess = vitest.fn();
render(
<MockedProvider
mocks={[
{
request: {
query: CREATE_USER_MUTATION,
variables: {
name: "John Doe",
email: "john.doe@example.com",
role: "admin",
},
},
result: onMutationResult,
},
]}
>
<CreateUserDialog
isOpen={true}
onClose={onClose}
onSuccess={onSuccess}
onError={() => {}}
/>
</MockedProvider>
);
await userEvent.type(screen.getByLabelText("Name"), "John Doe");
await userEvent.type(
screen.getByLabelText("Email"),
"john.doe@example.com"
);
await userEvent.type(screen.getByLabelText("Role"), "admin");
await userEvent.click(screen.getByRole("button", { name: "Create" }));
await waitFor(() =>
expect(onSuccess).toHaveBeenCalledWith({
id: "1",
name: "John Doe",
email: "john.doe@example.com",
role: "admin",
})
);
await waitFor(() => expect(onClose).toHaveBeenCalled());
});
test("calls onError when user creation fails", async () => {
const onError = vitest.fn();
render(
<MockedProvider
mocks={[
{
request: {
query: CREATE_USER_MUTATION,
variables: {
name: "John Doe",
email: "john.doe@example.com",
role: "admin",
},
},
error: new Error("User creation failed"),
},
]}
>
<CreateUserDialog
isOpen={true}
onClose={() => {}}
onSuccess={() => {}}
onError={onError}
/>
</MockedProvider>
);
await userEvent.type(screen.getByLabelText("Name"), "John Doe");
await userEvent.type(
screen.getByLabelText("Email"),
"john.doe@example.com"
);
await userEvent.type(screen.getByLabelText("Role"), "admin");
await userEvent.click(screen.getByRole("button", { name: "Create" }));
await waitFor(() =>
expect(onError).toHaveBeenCalledWith(new Error("User creation failed"))
);
});
});