Skip to content

Commit 3b06a05

Browse files
committed
chore: add test for own messages and read
1 parent e4eedf9 commit 3b06a05

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

package/src/__tests__/offline-support/offline-feature.js

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

package/src/mock-builders/event/messageNew.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ export default (client, newMessage, channel = {}) => {
66
cid: channel.cid,
77
message: newMessage,
88
type: 'message.new',
9+
...(newMessage.user ? { user: newMessage.user } : {}),
910
});
1011
};

0 commit comments

Comments
 (0)