@@ -638,30 +638,39 @@ describe('ChannelService', () => {
638638 expect ( spy ) . not . toHaveBeenCalled ( ) ;
639639 } ) ;
640640
641- it ( 'should load messages of the channel , if channel is not yet initialized' , async ( ) => {
641+ it ( `shouldn't emit channel switch start , if the channel is already active` , async ( ) => {
642642 await init ( ) ;
643- const channel = generateMockChannels ( 1 ) [ 0 ] ;
644- channel . cid = 'not-initialized' ;
643+ const spy = jasmine . createSpy ( ) ;
644+ service . channelSwitchState$ . subscribe ( spy ) ;
645+ spy . calls . reset ( ) ;
646+ service . setAsActiveChannel ( getLatestValue ( service . activeChannel$ ) ! ) ;
647+
648+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
649+ } ) ;
650+
651+ it ( 'should load messages of the active channel on reload, if channel is not yet initialized' , async ( ) => {
652+ await init ( ) ;
653+ const channel = getActiveChannel ( ) ;
645654 ( channel as unknown as { initialized : boolean } ) . initialized = false ;
646655 spyOn ( channel , 'watch' ) . and . callThrough ( ) ;
647- service . setAsActiveChannel ( channel ) ;
656+ spyOn ( channel . messagePaginator , 'reload' ) ;
657+ await service . reloadActiveChannelMessages ( ) ;
648658
649659 expect ( channel . watch ) . toHaveBeenCalledWith ( ) ;
660+ expect ( channel . messagePaginator . reload ) . not . toHaveBeenCalled ( ) ;
650661 } ) ;
651662
652- it ( 'should emit error, if loading messages of the channel failed' , async ( ) => {
663+ it ( 'should emit error, if loading messages of the active channel failed' , async ( ) => {
653664 await init ( ) ;
654- const channel = generateMockChannels ( 1 ) [ 0 ] ;
655- channel . cid = 'not-initialized' ;
665+ const channel = getActiveChannel ( ) ;
656666 ( channel as unknown as { initialized : boolean } ) . initialized = false ;
657667 const error = apiError ( 'GetOrCreateChannel failed with error: "error"' ) ;
658668 spyOn ( channel , 'watch' ) . and . rejectWith ( error ) ;
659669 const notificationService = TestBed . inject ( NotificationService ) ;
660670 spyOn ( notificationService , 'addTemporaryNotification' ) ;
661671 const paginationStateSpy = jasmine . createSpy ( ) ;
662672 service . activeChannelMessagesPaginationState$ . subscribe ( paginationStateSpy ) ;
663- service . setAsActiveChannel ( channel ) ;
664- await settle ( ) ;
673+ await service . reloadActiveChannelMessages ( ) ;
665674
666675 expect ( paginationStateSpy ) . toHaveBeenCalledWith (
667676 jasmine . objectContaining ( { isLoading : true } )
@@ -674,6 +683,14 @@ describe('ChannelService', () => {
674683 ) ;
675684 } ) ;
676685
686+ it ( 'should return the same messages from #activeChannelMessages as #activeChannelMessages$' , async ( ) => {
687+ await init ( ) ;
688+ const messages = getLatestValue ( service . activeChannelMessages$ ) ;
689+
690+ expect ( service . activeChannelMessages ) . toBe ( messages ) ;
691+ expect ( service . activeChannelMessages . every ( ( m ) => ! ! m . readBy ) ) . toBeTrue ( ) ;
692+ } ) ;
693+
677694 it ( 'should emit #activeChannelMessages$' , async ( ) => {
678695 await init ( ) ;
679696 const spy = jasmine . createSpy ( ) ;
@@ -873,7 +890,7 @@ describe('ChannelService', () => {
873890
874891 it ( 'should load more thread replies' , async ( ) => {
875892 await init ( ) ;
876- const parentMessage = service . activeChannelMessages [ 0 ] as StreamMessage ;
893+ const parentMessage = service . activeChannelMessages [ 0 ] ;
877894 await service . setAsActiveParentMessage (
878895 getLatestValue ( service . activeChannelMessages$ ) . find (
879896 ( m ) => m . id === parentMessage . id
@@ -2469,15 +2486,16 @@ describe('ChannelService', () => {
24692486 ) . not . toBeUndefined ( ) ;
24702487 } ) ;
24712488
2472- it ( 'should only update #readBy of the latest message , if read state changes' , async ( ) => {
2489+ it ( 'should only update the messages with changed #readBy , if read state changes' , async ( ) => {
24732490 await init ( ) ;
24742491 const channel = getActiveChannel ( ) ;
24752492 const before = getLatestValue ( service . activeChannelMessages$ ) ;
2493+ const lastReadIndex = 10 ;
24762494 const user = { id : 'jack' , name : 'Jack' } as UserResponse ;
2477- // jack read every message
2495+ // jack read the messages until (and including) lastReadIndex
24782496 const read = {
24792497 [ user . id ] : {
2480- last_read : dateToNs ( new Date ( ) ) ,
2498+ last_read : before [ lastReadIndex ] . created_at ,
24812499 user,
24822500 unread_messages : 0 ,
24832501 } ,
@@ -2489,9 +2507,30 @@ describe('ChannelService', () => {
24892507 const after = getLatestValue ( service . activeChannelMessages$ ) ;
24902508
24912509 expect ( after ) . not . toBe ( before ) ;
2492- expect ( after [ after . length - 1 ] ) . not . toBe ( before [ before . length - 1 ] ) ;
2493- expect ( after [ after . length - 1 ] . readBy . map ( ( u ) => u . id ) ) . toEqual ( [ 'jack' ] ) ;
2494- after . slice ( 0 , - 1 ) . forEach ( ( m , i ) => expect ( m ) . toBe ( before [ i ] ) ) ;
2510+ after . forEach ( ( m , i ) => {
2511+ if ( i <= lastReadIndex ) {
2512+ expect ( m ) . not . toBe ( before [ i ] ) ;
2513+ expect ( m . readBy . map ( ( u ) => u . id ) ) . toEqual ( [ 'jack' ] ) ;
2514+ } else {
2515+ expect ( m ) . toBe ( before [ i ] ) ;
2516+ expect ( m . readBy ) . toEqual ( [ ] ) ;
2517+ }
2518+ } ) ;
2519+ } ) ;
2520+
2521+ it ( `shouldn't emit the message list, if read state changes don't change #readBy` , async ( ) => {
2522+ await init ( ) ;
2523+ const channel = getActiveChannel ( ) ;
2524+ const spy = jasmine . createSpy ( ) ;
2525+ service . activeChannelMessages$ . subscribe ( spy ) ;
2526+ spy . calls . reset ( ) ;
2527+ const read = { } as Channel [ 'state' ] [ 'read' ] ;
2528+ channel . state . read = read ;
2529+ ( channel . state as unknown as MockStateStore < { read : unknown } > ) . partialNext (
2530+ { read }
2531+ ) ;
2532+
2533+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
24952534 } ) ;
24962535
24972536 it ( 'should recalculate #readBy, if a channel is activated again (read events received while the channel was inactive)' , async ( ) => {
@@ -2516,38 +2555,45 @@ describe('ChannelService', () => {
25162555 ( channel . state as unknown as MockStateStore < { read : unknown } > ) . partialNext (
25172556 { read }
25182557 ) ;
2558+ const spy = jasmine . createSpy ( ) ;
2559+ service . activeChannelMessages$ . subscribe ( spy ) ;
2560+ spy . calls . reset ( ) ;
25192561 service . setAsActiveChannel ( channel ) ;
25202562 const after = getLatestValue ( service . activeChannelMessages$ ) ;
25212563
25222564 expect ( after [ after . length - 1 ] . readBy . map ( ( u ) => u . id ) ) . toEqual ( [ 'jack' ] ) ;
2565+ // The message list is emitted once, with the up-to-date readBy lists
2566+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
25232567 } ) ;
25242568
25252569 it ( 'should update #readBy of every message, if a user is updated' , async ( ) => {
25262570 await init ( ) ;
25272571 const channel = getActiveChannel ( ) ;
25282572 const user = { id : 'jack' , name : 'Jack' } as UserResponse ;
2529- const read = {
2573+ const setRead = ( read : Channel [ 'state' ] [ 'read' ] ) => {
2574+ channel . state . read = read ;
2575+ (
2576+ channel . state as unknown as MockStateStore < { read : unknown } >
2577+ ) . partialNext ( { read } ) ;
2578+ } ;
2579+ setRead ( {
25302580 [ user . id ] : {
25312581 last_read : dateToNs ( new Date ( ) ) ,
25322582 user,
25332583 unread_messages : 0 ,
25342584 } ,
2535- } as unknown as Channel [ 'state' ] [ 'read' ] ;
2536- channel . state . read = read ;
2537- // Simulate a channel switch, so readBy is calculated for every message
2538- service . deselectActiveChannel ( ) ;
2539- service . setAsActiveChannel ( channel ) ;
2585+ } as unknown as Channel [ 'state' ] [ 'read' ] ) ;
25402586 const before = getLatestValue ( service . activeChannelMessages$ ) ;
25412587
25422588 expect ( before . every ( ( m ) => m . readBy [ 0 ] ?. name === 'Jack' ) ) . toBeTrue ( ) ;
25432589
2544- channel . state . read = {
2545- [ user . id ] : { ... read [ user . id ] , user : { ... user , name : 'Jack Updated' } } ,
2546- } as unknown as Channel [ 'state' ] [ 'read' ] ;
2547- events$ . next ( {
2548- eventType : ' user.updated' ,
2549- event : { type : 'user.updated' , user } ,
2550- } as unknown as ClientEvent ) ;
2590+ // stream-chat updates the users of the read state on user.updated events
2591+ setRead ( {
2592+ [ user . id ] : {
2593+ ... channel . state . read [ user . id ] ,
2594+ user : { ... user , name : 'Jack Updated' } ,
2595+ } ,
2596+ } as unknown as Channel [ 'state' ] [ 'read' ] ) ;
25512597 const after = getLatestValue ( service . activeChannelMessages$ ) ;
25522598
25532599 expect ( after . every ( ( m ) => m . readBy [ 0 ] ?. name === 'Jack Updated' ) ) . toBeTrue ( ) ;
0 commit comments