1
- import { fakeAsync , TestBed , tick } from '@angular/core/testing' ;
1
+ import { fakeAsync , flush , TestBed , tick } from '@angular/core/testing' ;
2
2
import { Subject } from 'rxjs' ;
3
3
import { first , take } from 'rxjs/operators' ;
4
4
import {
@@ -208,6 +208,7 @@ describe('ChannelService', () => {
208
208
events$ . next ( { eventType : 'connection.recovered' } as ClientEvent ) ;
209
209
210
210
tick ( ) ;
211
+ flush ( ) ;
211
212
212
213
expect ( spy ) . toHaveBeenCalledWith ( channels ) ;
213
214
expect ( activeChannelSpy ) . toHaveBeenCalledWith ( channels [ 0 ] ) ;
@@ -577,6 +578,10 @@ describe('ChannelService', () => {
577
578
578
579
it ( 'should watch for new message events' , async ( ) => {
579
580
await init ( ) ;
581
+ // wait for mark read throttle time
582
+ await new Promise ( ( resolve ) => {
583
+ setTimeout ( resolve , service [ 'markReadThrottleTime' ] ) ;
584
+ } ) ;
580
585
const spy = jasmine . createSpy ( ) ;
581
586
service . activeChannelMessages$ . subscribe ( spy ) ;
582
587
const prevCount = ( spy . calls . mostRecent ( ) . args [ 0 ] as Channel [ ] ) . length ;
@@ -991,6 +996,7 @@ describe('ChannelService', () => {
991
996
992
997
it ( 'should add the new channel to the top of the list, and start watching it, if user is added to a channel' , fakeAsync ( async ( ) => {
993
998
await init ( ) ;
999
+ flush ( ) ;
994
1000
const newChannel = generateMockChannels ( ) [ 0 ] ;
995
1001
newChannel . cid = 'newchannel' ;
996
1002
newChannel . id = 'newchannel' ;
@@ -1030,6 +1036,7 @@ describe('ChannelService', () => {
1030
1036
event : { channel : channel } as any as Event < DefaultStreamChatGenerics > ,
1031
1037
} ) ;
1032
1038
tick ( ) ;
1039
+ flush ( ) ;
1033
1040
1034
1041
const channels = spy . calls . mostRecent ( ) . args [ 0 ] as Channel [ ] ;
1035
1042
const firstChannel = channels [ 0 ] ;
@@ -1059,6 +1066,7 @@ describe('ChannelService', () => {
1059
1066
event : { channel : channel } as any as Event < DefaultStreamChatGenerics > ,
1060
1067
} ) ;
1061
1068
tick ( ) ;
1069
+ flush ( ) ;
1062
1070
1063
1071
const channels = spy . calls . mostRecent ( ) . args [ 0 ] as Channel [ ] ;
1064
1072
@@ -2242,6 +2250,7 @@ describe('ChannelService', () => {
2242
2250
2243
2251
it ( 'should relaod active channel if active channel is not present after state reconnect' , fakeAsync ( async ( ) => {
2244
2252
await init ( ) ;
2253
+ flush ( ) ;
2245
2254
let activeChannel ! : Channel < DefaultStreamChatGenerics > ;
2246
2255
service . activeChannel$ . subscribe ( ( c ) => ( activeChannel = c ! ) ) ;
2247
2256
let channels ! : Channel < DefaultStreamChatGenerics > [ ] ;
@@ -2251,6 +2260,7 @@ describe('ChannelService', () => {
2251
2260
mockChatClient . queryChannels . and . resolveTo ( channels ) ;
2252
2261
events$ . next ( { eventType : 'connection.recovered' } as ClientEvent ) ;
2253
2262
tick ( ) ;
2263
+ flush ( ) ;
2254
2264
const spy = jasmine . createSpy ( ) ;
2255
2265
service . activeChannel$ . subscribe ( spy ) ;
2256
2266
@@ -2276,6 +2286,7 @@ describe('ChannelService', () => {
2276
2286
activeChannel . state . messages . push ( newMessage ) ;
2277
2287
events$ . next ( { eventType : 'connection.recovered' } as ClientEvent ) ;
2278
2288
tick ( ) ;
2289
+ flush ( ) ;
2279
2290
2280
2291
expect ( spy ) . not . toHaveBeenCalled ( ) ;
2281
2292
expect ( service . deselectActiveChannel ) . not . toHaveBeenCalled ( ) ;
@@ -2354,6 +2365,22 @@ describe('ChannelService', () => {
2354
2365
expect ( service . activeChannelUnreadCount ) . toBe ( 0 ) ;
2355
2366
} ) ;
2356
2367
2368
+ it ( `should set last read message id to undefined if unread count is 0` , ( ) => {
2369
+ const activeChannel = generateMockChannels ( ) [ 0 ] ;
2370
+ activeChannel . id = 'next-active-channel' ;
2371
+ activeChannel . state . read [ user . id ] = {
2372
+ last_read : new Date ( ) ,
2373
+ last_read_message_id : 'last-read-message-id' ,
2374
+ unread_messages : 0 ,
2375
+ user : user ,
2376
+ } ;
2377
+
2378
+ service . setAsActiveChannel ( activeChannel ) ;
2379
+
2380
+ expect ( service . activeChannelLastReadMessageId ) . toBe ( undefined ) ;
2381
+ expect ( service . activeChannelUnreadCount ) . toBe ( 0 ) ;
2382
+ } ) ;
2383
+
2357
2384
it ( 'should be able to select empty channel as active channel' , ( ) => {
2358
2385
const channel = generateMockChannels ( ) [ 0 ] ;
2359
2386
channel . id = 'new-empty-channel' ;
@@ -2558,6 +2585,18 @@ describe('ChannelService', () => {
2558
2585
2559
2586
expect ( service . activeChannelLastReadMessageId ) . toBe ( 'last-read-message' ) ;
2560
2587
expect ( service . activeChannelUnreadCount ) . toBe ( 12 ) ;
2588
+
2589
+ events$ . next ( {
2590
+ eventType : 'notification.mark_unread' ,
2591
+ event : {
2592
+ channel_id : service . activeChannel ?. id ,
2593
+ unread_messages : 0 ,
2594
+ last_read_message_id : 'last-read-message' ,
2595
+ } as Event < DefaultStreamChatGenerics > ,
2596
+ } ) ;
2597
+
2598
+ expect ( service . activeChannelLastReadMessageId ) . toBe ( undefined ) ;
2599
+ expect ( service . activeChannelUnreadCount ) . toBe ( 0 ) ;
2561
2600
} ) ;
2562
2601
2563
2602
it ( 'should halt marking the channel as read if an unread call was made in that session' , async ( ) => {
@@ -2639,4 +2678,76 @@ describe('ChannelService', () => {
2639
2678
expect ( customQuery ) . toHaveBeenCalledWith ( 'next-page' ) ;
2640
2679
expect ( hasMoreSpy ) . toHaveBeenCalledWith ( false ) ;
2641
2680
} ) ;
2681
+
2682
+ it ( 'should throttle mark read API calls' , async ( ) => {
2683
+ await init ( ) ;
2684
+ // wait for mark read throttle time
2685
+ await new Promise ( ( resolve ) => {
2686
+ setTimeout ( resolve , service [ 'markReadThrottleTime' ] ) ;
2687
+ } ) ;
2688
+
2689
+ const activeChannel = service . activeChannel ! ;
2690
+ spyOn ( activeChannel , 'markRead' ) ;
2691
+
2692
+ ( activeChannel as MockChannel ) . handleEvent ( 'message.new' , mockMessage ( ) ) ;
2693
+
2694
+ expect ( activeChannel . markRead ) . toHaveBeenCalledTimes ( 1 ) ;
2695
+
2696
+ ( activeChannel as MockChannel ) . handleEvent ( 'message.new' , mockMessage ( ) ) ;
2697
+
2698
+ expect ( activeChannel . markRead ) . toHaveBeenCalledTimes ( 1 ) ;
2699
+
2700
+ // wait for mark read throttle time
2701
+ await new Promise ( ( resolve ) => {
2702
+ setTimeout ( resolve , service [ 'markReadThrottleTime' ] ) ;
2703
+ } ) ;
2704
+
2705
+ expect ( activeChannel . markRead ) . toHaveBeenCalledTimes ( 2 ) ;
2706
+ } ) ;
2707
+
2708
+ it ( 'should throttle mark read API calls - channel change' , async ( ) => {
2709
+ await init ( ) ;
2710
+ // wait for mark read throttle time
2711
+ await new Promise ( ( resolve ) => {
2712
+ setTimeout ( resolve , service [ 'markReadThrottleTime' ] ) ;
2713
+ } ) ;
2714
+
2715
+ const activeChannel = service . activeChannel ! ;
2716
+ spyOn ( activeChannel , 'markRead' ) ;
2717
+
2718
+ ( activeChannel as MockChannel ) . handleEvent ( 'message.new' , mockMessage ( ) ) ;
2719
+
2720
+ expect ( activeChannel . markRead ) . toHaveBeenCalledTimes ( 1 ) ;
2721
+
2722
+ ( activeChannel as MockChannel ) . handleEvent ( 'message.new' , mockMessage ( ) ) ;
2723
+
2724
+ expect ( activeChannel . markRead ) . toHaveBeenCalledTimes ( 1 ) ;
2725
+
2726
+ service . setAsActiveChannel ( service . channels [ 1 ] ) ;
2727
+
2728
+ expect ( activeChannel . markRead ) . toHaveBeenCalledTimes ( 2 ) ;
2729
+ } ) ;
2730
+
2731
+ it ( 'should throttle mark read API calls - reset' , async ( ) => {
2732
+ await init ( ) ;
2733
+ // wait for mark read throttle time
2734
+ await new Promise ( ( resolve ) => {
2735
+ setTimeout ( resolve , service [ 'markReadThrottleTime' ] ) ;
2736
+ } ) ;
2737
+
2738
+ const activeChannel = service . activeChannel ! ;
2739
+ spyOn ( activeChannel , 'markRead' ) ;
2740
+
2741
+ ( activeChannel as MockChannel ) . handleEvent ( 'message.new' , mockMessage ( ) ) ;
2742
+
2743
+ expect ( activeChannel . markRead ) . toHaveBeenCalledTimes ( 1 ) ;
2744
+
2745
+ ( activeChannel as MockChannel ) . handleEvent ( 'message.new' , mockMessage ( ) ) ;
2746
+
2747
+ expect ( activeChannel . markRead ) . toHaveBeenCalledTimes ( 1 ) ;
2748
+
2749
+ service . reset ( ) ;
2750
+
2751
+ expect ( activeChannel . markRead ) . toHaveBeenCalledTimes ( 2 ) ;
2752
+ } ) ;
2642
2753
} ) ;
0 commit comments