-
Notifications
You must be signed in to change notification settings - Fork 355
/
preferences.spec.tsx
209 lines (186 loc) · 5.84 KB
/
preferences.spec.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import sinon from "sinon";
import { pureMerge } from "coral-common/utils";
import { GQLResolver } from "coral-framework/schema";
import {
act,
createResolversStub,
CreateTestRendererParams,
waitForElement,
within,
} from "coral-framework/testHelpers";
import { commenters, settings, stories, viewerPassive } from "../fixtures";
import create from "./create";
const story = stories[0];
const viewer = viewerPassive;
async function createTestRenderer(
params: CreateTestRendererParams<GQLResolver> = {}
) {
const { testRenderer, context } = create({
...params,
resolvers: pureMerge(
createResolversStub<GQLResolver>({
Query: {
settings: () => settings,
viewer: () => viewer,
stream: () => story,
},
}),
params.resolvers
),
initLocalState: (localRecord, source, environment) => {
localRecord.setValue("PREFERENCES", "profileTab");
if (params.initLocalState) {
params.initLocalState(localRecord, source, environment);
}
},
});
const ignoredCommenters = await waitForElement(() =>
within(testRenderer.root).queryByTestID("profile-account-ignoredCommenters")
);
return {
testRenderer,
context,
ignoredCommenters,
};
}
it("render notifications form", async () => {
const updateNotificationSettings = sinon
.stub()
.callsFake((_: any, { input: { clientMutationId, ...notifications } }) => {
expectAndFail(notifications).toMatchObject({
onReply: true,
onFeatured: true,
onStaffReplies: true,
onModeration: true,
digestFrequency: "HOURLY",
});
return {
user: pureMerge<typeof viewer>(viewer, {
notifications,
}),
clientMutationId,
};
});
const { testRenderer } = await createTestRenderer({
resolvers: createResolversStub<GQLResolver>({
Mutation: {
updateNotificationSettings,
},
}),
});
const container = await waitForElement(() =>
within(testRenderer.root).getByTestID("profile-account-notifications")
);
expect(await within(container).axe()).toHaveNoViolations();
const form = within(container).getByType("form");
// Get the form fields.
const onReply = await waitForElement(() =>
within(form).getByID("onReply", { exact: false })
);
const onStaffReplies = await waitForElement(() =>
within(form).getByID("onStaffReplies", { exact: false })
);
const onModeration = await waitForElement(() =>
within(form).getByID("onModeration", { exact: false })
);
const onFeatured = await waitForElement(() =>
within(form).getByID("onFeatured", { exact: false })
);
const digestFrequency = await waitForElement(() =>
within(form).getByID("digestFrequency", { exact: false })
);
const save = await waitForElement(() => within(form).getByType("button"));
// The save button should be disabled for unchanged fields.
expect(save.props.disabled).toEqual(true);
// The digest frequency select should be disabled with no options enabled.
expect(digestFrequency.props.disabled).toEqual(true);
// Enable the options.
act(() => {
onReply.props.onChange(true);
onStaffReplies.props.onChange(true);
onModeration.props.onChange(true);
onFeatured.props.onChange(true);
});
// The digest frequency select should now be enabled.
expect(digestFrequency.props.disabled).toEqual(false);
// Change the digest frequency.
act(() => {
digestFrequency.props.onChange("HOURLY");
});
// Submit the form.
await act(async () => {
await form.props.onSubmit();
});
// Ensure that the mutation was called and that the save button is now
// disabled.
expect(updateNotificationSettings.calledOnce).toEqual(true);
expect(save.props.disabled).toEqual(true);
// Change a notification option.
act(() => {
onReply.props.onChange(false);
});
// The save button should now be enabled.
expect(save.props.disabled).toEqual(false);
// Change a notification back (making it pristine).
act(() => {
onReply.props.onChange(true);
});
// The save button should now be disabled.
expect(save.props.disabled).toEqual(true);
});
it("render empty ignored users list", async () => {
const { ignoredCommenters } = await createTestRenderer();
const editButton = within(ignoredCommenters).getByText("Manage");
act(() => {
editButton.props.onClick();
});
await waitForElement(() =>
within(ignoredCommenters).getByText(
"You are not currently ignoring anyone",
{
exact: false,
}
)
);
});
it("render ignored users list", async () => {
const { ignoredCommenters } = await createTestRenderer({
resolvers: createResolversStub<GQLResolver>({
Query: {
viewer: () =>
pureMerge<typeof viewer>(viewer, {
ignoredUsers: [commenters[0], commenters[1]],
}),
},
Mutation: {
removeUserIgnore: ({ variables }) => {
expectAndFail(variables).toMatchObject({
// we sort by username now, so this user
// comes first because of their username
userID: commenters[1].id,
});
return {};
},
},
}),
});
const editButton = within(ignoredCommenters).getByText("Manage");
act(() => {
editButton.props.onClick();
});
within(ignoredCommenters).getByText(commenters[0].username!);
within(ignoredCommenters).getByText(commenters[1].username!);
const stopIgnoreButtons = within(ignoredCommenters).getAllByText(
"Stop ignoring",
{ selector: "button" }
);
// Stop ignoring first users.
await act(async () => {
stopIgnoreButtons[0].props.onClick();
});
// First user should be replaced with "you are no longer ignoring"
await waitForElement(() =>
within(ignoredCommenters).getByText("You are no longer ignoring")
);
within(ignoredCommenters).getByText(commenters[0].username!);
});