@@ -30,6 +30,7 @@ import { MessageList } from '../../MessageList';
30
30
import { Thread } from '../../Thread' ;
31
31
import { MessageProvider } from '../../../context' ;
32
32
import { MessageActionsBox } from '../../MessageActions' ;
33
+ import { DEFAULT_THREAD_PAGE_SIZE } from '../../../constants/limits' ;
33
34
34
35
jest . mock ( '../../Loading' , ( ) => ( {
35
36
LoadingErrorIndicator : jest . fn ( ( ) => < div /> ) ,
@@ -558,39 +559,68 @@ describe('Channel', () => {
558
559
await waitFor ( ( ) => expect ( hasThread ) . toHaveBeenCalledWith ( threadMessage . id ) ) ;
559
560
} ) ;
560
561
561
- it ( 'should be able to load more messages in a thread' , async ( ) => {
562
+ it ( 'should be able to load more messages in a thread until reaching the end ' , async ( ) => {
562
563
const { channel, chatClient } = await initClient ( ) ;
563
564
const getRepliesSpy = jest . spyOn ( channel , 'getReplies' ) ;
564
565
const threadMessage = messages [ 0 ] ;
565
-
566
- const replies = [ generateMessage ( { parent_id : threadMessage . id } ) ] ;
566
+ const replies = Array . from ( { length : DEFAULT_THREAD_PAGE_SIZE } , ( ) =>
567
+ generateMessage ( { parent_id : threadMessage . id } ) ,
568
+ ) ;
567
569
568
570
useMockedApis ( chatClient , [ threadRepliesApi ( replies ) ] ) ;
569
571
570
572
const hasThreadMessages = jest . fn ( ) ;
571
573
572
- await renderComponent (
573
- { channel, chatClient } ,
574
- ( { loadMoreThread, openThread, thread, threadMessages } ) => {
575
- if ( ! thread ) {
576
- // first, open a thread
577
- openThread ( threadMessage , { preventDefault : ( ) => null } ) ;
578
- } else if ( ! threadMessages . length ) {
579
- // then, load more messages in the thread
580
- loadMoreThread ( ) ;
581
- } else {
582
- // then, call our mock fn so we can verify what was passed as threadMessages
583
- hasThreadMessages ( threadMessages ) ;
584
- }
585
- } ,
574
+ let callback = ( { loadMoreThread, openThread, thread, threadMessages } ) => {
575
+ if ( ! thread ) {
576
+ // first, open a thread
577
+ openThread ( threadMessage , { preventDefault : ( ) => null } ) ;
578
+ } else if ( ! threadMessages . length ) {
579
+ // then, load more messages in the thread
580
+ loadMoreThread ( ) ;
581
+ } else {
582
+ // then, call our mock fn so we can verify what was passed as threadMessages
583
+ hasThreadMessages ( threadMessages ) ;
584
+ }
585
+ } ;
586
+ const { rerender } = await render (
587
+ < Chat client = { chatClient } >
588
+ < Channel channel = { channel } >
589
+ < CallbackEffectWithChannelContexts callback = { callback } />
590
+ </ Channel >
591
+ </ Chat > ,
586
592
) ;
587
593
588
594
await waitFor ( ( ) => {
595
+ expect ( getRepliesSpy ) . toHaveBeenCalledTimes ( 1 ) ;
589
596
expect ( getRepliesSpy ) . toHaveBeenCalledWith ( threadMessage . id , expect . any ( Object ) ) ;
590
- } ) ;
591
- await waitFor ( ( ) => {
592
597
expect ( hasThreadMessages ) . toHaveBeenCalledWith ( replies ) ;
593
598
} ) ;
599
+
600
+ useMockedApis ( chatClient , [ threadRepliesApi ( [ ] ) ] ) ;
601
+ callback = ( { loadMoreThread } ) => {
602
+ loadMoreThread ( ) ;
603
+ } ;
604
+ await act ( ( ) => {
605
+ rerender (
606
+ < Chat client = { chatClient } >
607
+ < Channel channel = { channel } >
608
+ < CallbackEffectWithChannelContexts callback = { callback } />
609
+ </ Channel >
610
+ </ Chat > ,
611
+ ) ;
612
+ } ) ;
613
+ expect ( getRepliesSpy ) . toHaveBeenCalledTimes ( 2 ) ;
614
+ await act ( ( ) => {
615
+ rerender (
616
+ < Chat client = { chatClient } >
617
+ < Channel channel = { channel } >
618
+ < CallbackEffectWithChannelContexts callback = { callback } />
619
+ </ Channel >
620
+ </ Chat > ,
621
+ ) ;
622
+ } ) ;
623
+ expect ( getRepliesSpy ) . toHaveBeenCalledTimes ( 2 ) ;
594
624
} ) ;
595
625
596
626
it ( 'should allow closing a thread after it has been opened' , async ( ) => {
0 commit comments