@@ -29,6 +29,13 @@ const defaultMessageContextValue = {
29
29
messageListRect : { } ,
30
30
} ;
31
31
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
+
32
39
async function renderComponent ( boxProps , messageContext = { } ) {
33
40
const { client } = await initClientWithChannels ( ) ;
34
41
return render (
@@ -171,7 +178,6 @@ describe('MessageActionsBox', () => {
171
178
describe ( 'mark message unread' , ( ) => {
172
179
afterEach ( jest . restoreAllMocks ) ;
173
180
const ACTION_TEXT = 'Mark as unread' ;
174
- const TOGGLE_ACTIONS_BUTTON_TEST_ID = 'message-actions' ;
175
181
const me = generateUser ( ) ;
176
182
const otherUser = generateUser ( ) ;
177
183
const message = generateMessage ( { user : otherUser } ) ;
@@ -254,9 +260,7 @@ describe('MessageActionsBox', () => {
254
260
chatProps : { client } ,
255
261
messageProps : { message } ,
256
262
} ) ;
257
- await act ( async ( ) => {
258
- await fireEvent . click ( screen . getByTestId ( TOGGLE_ACTIONS_BUTTON_TEST_ID ) ) ;
259
- } ) ;
263
+ await toggleOpenMessageActions ( ) ;
260
264
expect ( screen . queryByText ( ACTION_TEXT ) ) . not . toBeInTheDocument ( ) ;
261
265
} ) ;
262
266
@@ -281,9 +285,7 @@ describe('MessageActionsBox', () => {
281
285
chatProps : { client } ,
282
286
messageProps : { message : myMessage } ,
283
287
} ) ;
284
- await act ( async ( ) => {
285
- await fireEvent . click ( screen . getByTestId ( TOGGLE_ACTIONS_BUTTON_TEST_ID ) ) ;
286
- } ) ;
288
+ await toggleOpenMessageActions ( ) ;
287
289
expect ( screen . queryByText ( ACTION_TEXT ) ) . toBeInTheDocument ( ) ;
288
290
} ) ;
289
291
@@ -301,9 +303,7 @@ describe('MessageActionsBox', () => {
301
303
chatProps : { client } ,
302
304
messageProps : { message, threadList : true } ,
303
305
} ) ;
304
- await act ( async ( ) => {
305
- await fireEvent . click ( screen . getByTestId ( TOGGLE_ACTIONS_BUTTON_TEST_ID ) ) ;
306
- } ) ;
306
+ await toggleOpenMessageActions ( ) ;
307
307
expect ( screen . queryByText ( ACTION_TEXT ) ) . not . toBeInTheDocument ( ) ;
308
308
} ) ;
309
309
@@ -336,9 +336,7 @@ describe('MessageActionsBox', () => {
336
336
} ) ;
337
337
} ) ;
338
338
339
- await act ( async ( ) => {
340
- await fireEvent . click ( screen . getByTestId ( TOGGLE_ACTIONS_BUTTON_TEST_ID ) ) ;
341
- } ) ;
339
+ await toggleOpenMessageActions ( ) ;
342
340
expect ( screen . queryByText ( ACTION_TEXT ) ) . toBeInTheDocument ( ) ;
343
341
} ) ;
344
342
@@ -365,20 +363,18 @@ describe('MessageActionsBox', () => {
365
363
chatProps : { client } ,
366
364
messageProps : { message : messageWithoutID } ,
367
365
} ) ;
368
- await act ( async ( ) => {
369
- await fireEvent . click ( screen . getByTestId ( TOGGLE_ACTIONS_BUTTON_TEST_ID ) ) ;
370
- } ) ;
366
+ await toggleOpenMessageActions ( ) ;
371
367
expect ( screen . queryByText ( ACTION_TEXT ) ) . not . toBeInTheDocument ( ) ;
372
368
} ) ;
373
369
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 ( ) => {
375
371
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 ) ,
377
373
} ) ;
378
374
const read = [
379
375
{
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 ) ,
382
378
// last_read_message_id: message.id, // optional
383
379
unread_messages : 2 ,
384
380
user : me ,
@@ -410,10 +406,17 @@ describe('MessageActionsBox', () => {
410
406
</ Chat > ,
411
407
) ;
412
408
} ) ;
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 ) ;
417
420
} ) ;
418
421
419
422
it ( 'should be displayed and execute API request' , async ( ) => {
@@ -431,6 +434,7 @@ describe('MessageActionsBox', () => {
431
434
chatProps : { client } ,
432
435
messageProps : { message } ,
433
436
} ) ;
437
+ await toggleOpenMessageActions ( ) ;
434
438
await act ( async ( ) => {
435
439
await fireEvent . click ( screen . getByTestId ( TOGGLE_ACTIONS_BUTTON_TEST_ID ) ) ;
436
440
await fireEvent . click ( screen . getByText ( ACTION_TEXT ) ) ;
@@ -456,6 +460,7 @@ describe('MessageActionsBox', () => {
456
460
chatProps : { client } ,
457
461
messageProps : { getMarkMessageUnreadSuccessNotification, message } ,
458
462
} ) ;
463
+ await toggleOpenMessageActions ( ) ;
459
464
await act ( async ( ) => {
460
465
await fireEvent . click ( screen . getByTestId ( TOGGLE_ACTIONS_BUTTON_TEST_ID ) ) ;
461
466
await fireEvent . click ( screen . getByText ( ACTION_TEXT ) ) ;
@@ -481,6 +486,7 @@ describe('MessageActionsBox', () => {
481
486
chatProps : { client } ,
482
487
messageProps : { getMarkMessageUnreadErrorNotification, message } ,
483
488
} ) ;
489
+ await toggleOpenMessageActions ( ) ;
484
490
await act ( async ( ) => {
485
491
await fireEvent . click ( screen . getByTestId ( TOGGLE_ACTIONS_BUTTON_TEST_ID ) ) ;
486
492
await fireEvent . click ( screen . getByText ( ACTION_TEXT ) ) ;
0 commit comments