Skip to content

Commit d9f3709

Browse files
committed
test: open MessageActionsBox first in MessageActionsBox.test.js
1 parent 07eb261 commit d9f3709

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

src/components/MessageActions/MessageActions.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export const MessageActions = <
128128
aria-haspopup='true'
129129
aria-label={t('aria/Open Message Actions Menu')}
130130
className='str-chat__message-actions-box-button'
131+
data-testid='message-actions-toggle-button'
131132
onClick={dialog?.toggleSingle}
132133
ref={actionsBoxButtonRef}
133134
>

src/components/MessageActions/__tests__/MessageActions.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ describe('<MessageActions /> component', () => {
8989
aria-haspopup="true"
9090
aria-label="Open Message Actions Menu"
9191
className="str-chat__message-actions-box-button"
92+
data-testid="message-actions-toggle-button"
9293
onClick={[Function]}
9394
>
9495
<svg
@@ -236,6 +237,7 @@ describe('<MessageActions /> component', () => {
236237
aria-haspopup="true"
237238
aria-label="Open Message Actions Menu"
238239
className="str-chat__message-actions-box-button"
240+
data-testid="message-actions-toggle-button"
239241
onClick={[Function]}
240242
>
241243
<svg
@@ -285,6 +287,7 @@ describe('<MessageActions /> component', () => {
285287
aria-haspopup="true"
286288
aria-label="Open Message Actions Menu"
287289
className="str-chat__message-actions-box-button"
290+
data-testid="message-actions-toggle-button"
288291
onClick={[Function]}
289292
>
290293
<svg

src/components/MessageActions/__tests__/MessageActionsBox.test.js

+30-24
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ const defaultMessageContextValue = {
2929
messageListRect: {},
3030
};
3131

32+
const TOGGLE_ACTIONS_BUTTON_TEST_ID = 'message-actions-toggle-button';
33+
const toggleOpenMessageActions = async (i = 0) => {
34+
await act(async () => {
35+
await fireEvent.click(screen.getAllByTestId(TOGGLE_ACTIONS_BUTTON_TEST_ID)[i]);
36+
});
37+
};
38+
3239
async function renderComponent(boxProps, messageContext = {}) {
3340
const { client } = await initClientWithChannels();
3441
return render(
@@ -171,7 +178,6 @@ describe('MessageActionsBox', () => {
171178
describe('mark message unread', () => {
172179
afterEach(jest.restoreAllMocks);
173180
const ACTION_TEXT = 'Mark as unread';
174-
const TOGGLE_ACTIONS_BUTTON_TEST_ID = 'message-actions';
175181
const me = generateUser();
176182
const otherUser = generateUser();
177183
const message = generateMessage({ user: otherUser });
@@ -254,9 +260,7 @@ describe('MessageActionsBox', () => {
254260
chatProps: { client },
255261
messageProps: { message },
256262
});
257-
await act(async () => {
258-
await fireEvent.click(screen.getByTestId(TOGGLE_ACTIONS_BUTTON_TEST_ID));
259-
});
263+
await toggleOpenMessageActions();
260264
expect(screen.queryByText(ACTION_TEXT)).not.toBeInTheDocument();
261265
});
262266

@@ -281,9 +285,7 @@ describe('MessageActionsBox', () => {
281285
chatProps: { client },
282286
messageProps: { message: myMessage },
283287
});
284-
await act(async () => {
285-
await fireEvent.click(screen.getByTestId(TOGGLE_ACTIONS_BUTTON_TEST_ID));
286-
});
288+
await toggleOpenMessageActions();
287289
expect(screen.queryByText(ACTION_TEXT)).toBeInTheDocument();
288290
});
289291

@@ -301,9 +303,7 @@ describe('MessageActionsBox', () => {
301303
chatProps: { client },
302304
messageProps: { message, threadList: true },
303305
});
304-
await act(async () => {
305-
await fireEvent.click(screen.getByTestId(TOGGLE_ACTIONS_BUTTON_TEST_ID));
306-
});
306+
await toggleOpenMessageActions();
307307
expect(screen.queryByText(ACTION_TEXT)).not.toBeInTheDocument();
308308
});
309309

@@ -336,9 +336,7 @@ describe('MessageActionsBox', () => {
336336
});
337337
});
338338

339-
await act(async () => {
340-
await fireEvent.click(screen.getByTestId(TOGGLE_ACTIONS_BUTTON_TEST_ID));
341-
});
339+
await toggleOpenMessageActions();
342340
expect(screen.queryByText(ACTION_TEXT)).toBeInTheDocument();
343341
});
344342

@@ -365,20 +363,18 @@ describe('MessageActionsBox', () => {
365363
chatProps: { client },
366364
messageProps: { message: messageWithoutID },
367365
});
368-
await act(async () => {
369-
await fireEvent.click(screen.getByTestId(TOGGLE_ACTIONS_BUTTON_TEST_ID));
370-
});
366+
await toggleOpenMessageActions();
371367
expect(screen.queryByText(ACTION_TEXT)).not.toBeInTheDocument();
372368
});
373369

374-
it('should be displayed as an option for messages other than message marked unread', async () => {
370+
it('should be displayed as an option for messages not marked and marked unread', async () => {
375371
const otherMsg = generateMessage({
376-
created_at: new Date(new Date(message.created_at).getTime() + 1000),
372+
created_at: new Date(new Date(message.created_at).getTime() + 2000),
377373
});
378374
const read = [
379375
{
380-
first_unread_message_id: message.id,
381-
last_read: new Date(new Date(message.created_at).getTime() - 1000),
376+
first_unread_message_id: otherMsg.id,
377+
last_read: new Date(new Date(otherMsg.created_at).getTime() - 1000),
382378
// last_read_message_id: message.id, // optional
383379
unread_messages: 2,
384380
user: me,
@@ -410,10 +406,17 @@ describe('MessageActionsBox', () => {
410406
</Chat>,
411407
);
412408
});
413-
414-
const [actionsBox1, actionsBox2] = screen.getAllByTestId('message-actions-box');
415-
expect(actionsBox1).toHaveTextContent(ACTION_TEXT);
416-
expect(actionsBox2).toHaveTextContent(ACTION_TEXT);
409+
await toggleOpenMessageActions(0);
410+
let boxes = screen.getAllByTestId('message-actions-box');
411+
// eslint-disable-next-line jest-dom/prefer-in-document
412+
expect(boxes).toHaveLength(1);
413+
expect(boxes[0]).toHaveTextContent(ACTION_TEXT);
414+
415+
await toggleOpenMessageActions(1);
416+
boxes = screen.getAllByTestId('message-actions-box');
417+
// eslint-disable-next-line jest-dom/prefer-in-document
418+
expect(boxes).toHaveLength(1);
419+
expect(boxes[0]).toHaveTextContent(ACTION_TEXT);
417420
});
418421

419422
it('should be displayed and execute API request', async () => {
@@ -431,6 +434,7 @@ describe('MessageActionsBox', () => {
431434
chatProps: { client },
432435
messageProps: { message },
433436
});
437+
await toggleOpenMessageActions();
434438
await act(async () => {
435439
await fireEvent.click(screen.getByTestId(TOGGLE_ACTIONS_BUTTON_TEST_ID));
436440
await fireEvent.click(screen.getByText(ACTION_TEXT));
@@ -456,6 +460,7 @@ describe('MessageActionsBox', () => {
456460
chatProps: { client },
457461
messageProps: { getMarkMessageUnreadSuccessNotification, message },
458462
});
463+
await toggleOpenMessageActions();
459464
await act(async () => {
460465
await fireEvent.click(screen.getByTestId(TOGGLE_ACTIONS_BUTTON_TEST_ID));
461466
await fireEvent.click(screen.getByText(ACTION_TEXT));
@@ -481,6 +486,7 @@ describe('MessageActionsBox', () => {
481486
chatProps: { client },
482487
messageProps: { getMarkMessageUnreadErrorNotification, message },
483488
});
489+
await toggleOpenMessageActions();
484490
await act(async () => {
485491
await fireEvent.click(screen.getByTestId(TOGGLE_ACTIONS_BUTTON_TEST_ID));
486492
await fireEvent.click(screen.getByText(ACTION_TEXT));

0 commit comments

Comments
 (0)