-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathSendWysiwygComposer-test.tsx
More file actions
356 lines (303 loc) · 14.3 KB
/
SendWysiwygComposer-test.tsx
File metadata and controls
356 lines (303 loc) · 14.3 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
Copyright 2024 New Vector Ltd.
Copyright 2022 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/
import "@testing-library/jest-dom";
import React from "react";
import { act, fireEvent, render, screen, waitFor } from "jest-matrix-react";
import { initOnce } from "@vector-im/matrix-wysiwyg";
import MatrixClientContext from "../../../../../../src/contexts/MatrixClientContext";
import defaultDispatcher from "../../../../../../src/dispatcher/dispatcher";
import { Action } from "../../../../../../src/dispatcher/actions";
import { flushPromises } from "../../../../../test-utils";
import { SendWysiwygComposer } from "../../../../../../src/components/views/rooms/wysiwyg_composer/";
import { aboveLeftOf } from "../../../../../../src/components/structures/ContextMenu";
import {
type ComposerInsertPayload,
ComposerType,
} from "../../../../../../src/dispatcher/payloads/ComposerInsertPayload";
import { setSelection } from "../../../../../../src/components/views/rooms/wysiwyg_composer/utils/selection";
import { createMocks } from "./utils";
import { ScopedRoomContextProvider } from "../../../../../../src/contexts/ScopedRoomContext.tsx";
import { E2EStatus } from "../../../../../../src/utils/ShieldUtils.ts";
jest.mock("../../../../../../src/components/views/rooms/EmojiButton", () => ({
EmojiButton: ({ addEmoji }: { addEmoji: (emoji: string) => void }) => {
return (
<button aria-label="Emoji" type="button" onClick={() => addEmoji("🦫")}>
Emoji
</button>
);
},
}));
beforeAll(initOnce, 10000);
describe("SendWysiwygComposer", () => {
afterEach(() => {
jest.resetAllMocks();
});
const { defaultRoomContext, mockClient } = createMocks();
const registerId = defaultDispatcher.register((payload) => {
switch (payload.action) {
case Action.ComposerInsert: {
const insertPayload = payload as ComposerInsertPayload;
if (insertPayload.composerType) break;
// re-dispatch to the correct composer
defaultDispatcher.dispatch<ComposerInsertPayload>({
...insertPayload,
timelineRenderingType: insertPayload.timelineRenderingType!,
composerType: ComposerType.Send,
});
break;
}
}
});
afterAll(() => {
defaultDispatcher.unregister(registerId);
});
const customRender = (
onChange = (_content: string): void => void 0,
onSend = (): void => void 0,
disabled = false,
isRichTextEnabled = true,
placeholder?: string,
e2eStatus?: E2EStatus,
) => {
return render(
<MatrixClientContext.Provider value={mockClient}>
<ScopedRoomContextProvider {...defaultRoomContext}>
<SendWysiwygComposer
onChange={onChange}
onSend={onSend}
disabled={disabled}
isRichTextEnabled={isRichTextEnabled}
menuPosition={aboveLeftOf({ top: 0, bottom: 0, right: 0 })}
placeholder={placeholder}
e2eStatus={e2eStatus}
/>
</ScopedRoomContextProvider>
</MatrixClientContext.Provider>,
);
};
it("Should render WysiwygComposer when isRichTextEnabled is at true", async () => {
// When
customRender(jest.fn(), jest.fn(), false, true);
// Then
expect(await screen.findByTestId("WysiwygComposer", undefined, { timeout: 5000 })).toBeInTheDocument();
});
it("Should render PlainTextComposer when isRichTextEnabled is at false", async () => {
// When
customRender(jest.fn(), jest.fn(), false, false);
// Then
expect(await screen.findByTestId("PlainTextComposer")).toBeInTheDocument();
});
describe.each([{ isRichTextEnabled: true }, { isRichTextEnabled: false }])(
"Should focus when receiving an Action.FocusSendMessageComposer action",
({ isRichTextEnabled }) => {
afterEach(() => {
jest.resetAllMocks();
});
it("Should focus when receiving an Action.FocusSendMessageComposer action", async () => {
// Given we don't have focus
customRender(jest.fn(), jest.fn(), false, isRichTextEnabled);
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
// When we send the right action
defaultDispatcher.dispatch({
action: Action.FocusSendMessageComposer,
context: null,
});
// Then the component gets the focus
await waitFor(() => expect(screen.getByRole("textbox")).toHaveFocus());
});
it("Should focus and clear when receiving an Action.ClearAndFocusSendMessageComposer", async () => {
// Given we don't have focus
const onChange = jest.fn();
customRender(onChange, jest.fn(), false, isRichTextEnabled);
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
fireEvent.input(screen.getByRole("textbox"), {
data: "foo bar",
inputType: "insertText",
});
// When we send the right action
defaultDispatcher.dispatch({
action: Action.ClearAndFocusSendMessageComposer,
timelineRenderingType: defaultRoomContext.timelineRenderingType,
});
// Then the component gets the focus
await waitFor(() => {
expect(screen.getByRole("textbox")).toHaveTextContent(/^$/);
expect(screen.getByRole("textbox")).toHaveFocus();
});
});
it("Should focus when receiving a reply_to_event action", async () => {
// Given we don't have focus
customRender(jest.fn(), jest.fn(), false, isRichTextEnabled);
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
// When we send the right action
defaultDispatcher.dispatch({
action: "reply_to_event",
context: null,
});
// Then the component gets the focus
await waitFor(() => expect(screen.getByRole("textbox")).toHaveFocus());
});
it("Should not focus when disabled", async () => {
// Given we don't have focus and we are disabled
customRender(jest.fn(), jest.fn(), true, isRichTextEnabled);
expect(screen.getByRole("textbox")).not.toHaveFocus();
// When we send an action that would cause us to get focus
defaultDispatcher.dispatch({
action: Action.FocusSendMessageComposer,
context: null,
});
// (Send a second event to exercise the clearTimeout logic)
defaultDispatcher.dispatch({
action: Action.FocusSendMessageComposer,
context: null,
});
// Wait for event dispatch to happen
await act(async () => {
await flushPromises();
});
// Then we don't get it because we are disabled
expect(screen.getByRole("textbox")).not.toHaveFocus();
});
},
);
describe.each([{ isRichTextEnabled: true }, { isRichTextEnabled: false }])(
"Placeholder when %s",
({ isRichTextEnabled }) => {
afterEach(() => {
jest.resetAllMocks();
});
it("Should not has placeholder", async () => {
// When
customRender(jest.fn(), jest.fn(), false, isRichTextEnabled);
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
// Then
expect(screen.getByRole("textbox")).not.toHaveClass("mx_WysiwygComposer_Editor_content_placeholder");
});
it("Should has placeholder", async () => {
// When
customRender(jest.fn(), jest.fn(), false, isRichTextEnabled, "my placeholder");
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
// Then
expect(screen.getByRole("textbox")).toHaveClass("mx_WysiwygComposer_Editor_content_placeholder");
});
it("Should display or not placeholder when editor content change", async () => {
// When
customRender(jest.fn(), jest.fn(), false, isRichTextEnabled, "my placeholder");
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
screen.getByRole("textbox").innerHTML = "f";
fireEvent.input(screen.getByRole("textbox"), {
data: "f",
inputType: "insertText",
});
// Then
await waitFor(() =>
expect(screen.getByRole("textbox")).not.toHaveClass(
"mx_WysiwygComposer_Editor_content_placeholder",
),
);
// When
screen.getByRole("textbox").innerHTML = "";
fireEvent.input(screen.getByRole("textbox"), {
inputType: "deleteContentBackward",
});
// Then
await waitFor(() =>
expect(screen.getByRole("textbox")).toHaveClass("mx_WysiwygComposer_Editor_content_placeholder"),
);
});
},
);
describe.each([{ isRichTextEnabled: true }, { isRichTextEnabled: false }])(
"Emoji when %s",
({ isRichTextEnabled }) => {
let emojiButton: HTMLElement;
beforeEach(async () => {
customRender(jest.fn(), jest.fn(), false, isRichTextEnabled);
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
emojiButton = screen.getByLabelText("Emoji");
});
afterEach(() => {
jest.resetAllMocks();
});
it("Should add an emoji in an empty composer", async () => {
// When
emojiButton.click();
// Then
await waitFor(() => expect(screen.getByRole("textbox")).toHaveTextContent(/🦫/));
});
it("Should add an emoji in the middle of a word", async () => {
// When
screen.getByRole("textbox").focus();
screen.getByRole("textbox").innerHTML = "word";
fireEvent.input(screen.getByRole("textbox"), {
data: "word",
inputType: "insertText",
});
const textNode = screen.getByRole("textbox").firstChild;
await setSelection({
anchorNode: textNode,
anchorOffset: 2,
focusNode: textNode,
focusOffset: 2,
isForward: true,
});
// the event is not automatically fired by jest
document.dispatchEvent(new CustomEvent("selectionchange"));
emojiButton.click();
// Then
await waitFor(() => expect(screen.getByRole("textbox")).toHaveTextContent(/wo🦫rd/));
});
it("Should add an emoji when a word is selected", async () => {
// When
screen.getByRole("textbox").focus();
screen.getByRole("textbox").innerHTML = "word";
fireEvent.input(screen.getByRole("textbox"), {
data: "word",
inputType: "insertText",
});
const textNode = screen.getByRole("textbox").firstChild;
await setSelection({
anchorNode: textNode,
anchorOffset: 3,
focusNode: textNode,
focusOffset: 2,
isForward: false,
});
// the event is not automatically fired by jest
document.dispatchEvent(new CustomEvent("selectionchange"));
emojiButton.click();
// Then
await waitFor(() => expect(screen.getByRole("textbox")).toHaveTextContent(/wo🦫d/));
});
},
);
describe.each([{ isRichTextEnabled: true }, { isRichTextEnabled: false }])(
"Left icon when %s",
({ isRichTextEnabled }) => {
it.each([
[E2EStatus.Verified, "Everyone in this room is verified"],
[E2EStatus.Warning, "Someone is using an unknown session"],
[undefined, undefined],
])("Should render left icon when e2eStatus is %s", async (e2eStatus, expectedLabel) => {
// When
customRender(jest.fn(), jest.fn(), false, isRichTextEnabled, undefined, e2eStatus);
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
const leftIcon = screen.getByTestId("e2e-icon");
// Then
expect(leftIcon).toBeInTheDocument();
expect(leftIcon).toHaveClass("mx_E2EIcon");
if (expectedLabel) {
// eslint-disable-next-line jest/no-conditional-expect
expect(leftIcon).toHaveAccessibleName(expectedLabel);
} else {
// eslint-disable-next-line jest/no-conditional-expect
expect(leftIcon.querySelector("svg")).not.toBeInTheDocument();
}
});
},
);
});