@@ -408,8 +408,6 @@ export const Generic = () => {
408408 ( r ) => targetChannel . cid === r . cid && chatClient . userID === r . userId ,
409409 ) ;
410410
411- console . log ( 'READROWS: ' , readRows ) ;
412-
413411 expect ( matchingReadRows . length ) . toBe ( 1 ) ;
414412 expect ( matchingReadRows [ 0 ] . unreadMessages ) . toBe ( 0 ) ;
415413 } ) ;
@@ -456,6 +454,68 @@ export const Generic = () => {
456454 } ) ;
457455 } ) ;
458456
457+ it ( 'should correctly handle multiple new messages from our own user' , async ( ) => {
458+ useMockedApis ( chatClient , [ queryChannelsApi ( channels ) ] ) ;
459+
460+ renderComponent ( ) ;
461+ act ( ( ) => dispatchConnectionChangedEvent ( chatClient ) ) ;
462+ await act ( async ( ) => await chatClient . offlineDb . syncManager . invokeSyncStatusListeners ( true ) ) ;
463+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'channel-list' ) ) . toBeTruthy ( ) ) ;
464+ const targetChannel = channels [ 0 ] . channel ;
465+
466+ // check if the reads state is correct first
467+ await waitFor ( async ( ) => {
468+ const readRows = await BetterSqlite . selectFromTable ( 'reads' ) ;
469+ const matchingReadRows = readRows . filter (
470+ ( r ) => targetChannel . cid === r . cid && chatClient . userID === r . userId ,
471+ ) ;
472+
473+ expect ( matchingReadRows . length ) . toBe ( 1 ) ;
474+ expect ( matchingReadRows [ 0 ] . unreadMessages ) . toBe ( 0 ) ;
475+ } ) ;
476+
477+ const newMessages = [
478+ generateMessage ( {
479+ cid : targetChannel . cid ,
480+ user : chatClient . user ,
481+ } ) ,
482+ generateMessage ( {
483+ cid : targetChannel . cid ,
484+ user : chatClient . user ,
485+ } ) ,
486+ generateMessage ( {
487+ cid : targetChannel . cid ,
488+ user : chatClient . user ,
489+ } ) ,
490+ ] ;
491+
492+ newMessages . forEach ( ( newMessage ) => {
493+ act ( ( ) => dispatchMessageNewEvent ( chatClient , newMessage , targetChannel ) ) ;
494+ } ) ;
495+
496+ await waitFor ( async ( ) => {
497+ const messagesRows = await BetterSqlite . selectFromTable ( 'messages' ) ;
498+ const readRows = await BetterSqlite . selectFromTable ( 'reads' ) ;
499+ const matchingMessageRows = messagesRows . filter ( ( m ) =>
500+ newMessages . some ( ( newMessage ) => newMessage . id === m . id ) ,
501+ ) ;
502+ const matchingReadRows = readRows . filter (
503+ ( r ) => targetChannel . cid === r . cid && chatClient . userID === r . userId ,
504+ ) ;
505+
506+ expect ( matchingMessageRows . length ) . toBe ( 3 ) ;
507+ newMessages . forEach ( ( newMessage ) => {
508+ expect (
509+ matchingMessageRows . some (
510+ ( matchingMessageRow ) => matchingMessageRow . id === newMessage . id ,
511+ ) ,
512+ ) . toBe ( true ) ;
513+ } ) ;
514+ expect ( matchingReadRows . length ) . toBe ( 1 ) ;
515+ expect ( matchingReadRows [ 0 ] . unreadMessages ) . toBe ( 0 ) ;
516+ } ) ;
517+ } ) ;
518+
459519 it ( 'should add a new channel and a new message to database from notification event' , async ( ) => {
460520 useMockedApis ( chatClient , [ queryChannelsApi ( channels ) ] ) ;
461521
0 commit comments