Skip to content

Commit e4eedf9

Browse files
committed
chore: add read checks for message.new as well
1 parent 066f977 commit e4eedf9

File tree

2 files changed

+90
-15
lines changed

2 files changed

+90
-15
lines changed

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

Lines changed: 86 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ export const Generic = () => {
139139
user,
140140
}),
141141
);
142+
members.push(generateMember({ cid, user: chatClient.user }));
143+
142144
const messages =
143145
messagesOverride ||
144146
Array(10)
@@ -168,8 +170,9 @@ export const Generic = () => {
168170
});
169171

170172
const reads = members.map((member) => ({
173+
cid,
171174
last_read: new Date(new Date().setDate(new Date().getDate() - getRandomInt(0, 20))),
172-
unread_messages: getRandomInt(0, messages.length),
175+
unread_messages: 0,
173176
user: member.user,
174177
}));
175178

@@ -182,21 +185,23 @@ export const Generic = () => {
182185
id,
183186
members,
184187
messages,
188+
read: reads,
185189
});
186190
};
187191

188192
beforeEach(async () => {
189193
jest.clearAllMocks();
194+
chatClient = await getTestClientWithUser({ id: 'dan' });
190195
allUsers = Array(20).fill(1).map(generateUser);
196+
allUsers.push(chatClient.user);
191197
allMessages = [];
192198
allMembers = [];
193199
allReactions = [];
194200
allReads = [];
201+
195202
channels = Array(10)
196203
.fill(1)
197204
.map(() => createChannel());
198-
199-
chatClient = await getTestClientWithUser({ id: 'dan' });
200205
await BetterSqlite.openDB();
201206
BetterSqlite.dropAllTables();
202207
});
@@ -288,12 +293,7 @@ export const Generic = () => {
288293
);
289294
readsRows.forEach((row) =>
290295
expect(
291-
allReads.filter(
292-
(r) =>
293-
r.last_read === row.lastRead &&
294-
r.user.id === row.userId &&
295-
r.unread_messages === row.unreadMessages,
296-
),
296+
allReads.filter((r) => r.user.id === row.userId && r.cid === row.cid),
297297
).toHaveLength(1),
298298
);
299299
});
@@ -370,17 +370,89 @@ export const Generic = () => {
370370
act(() => dispatchConnectionChangedEvent(chatClient));
371371
await act(async () => await chatClient.offlineDb.syncManager.invokeSyncStatusListeners(true));
372372
await waitFor(() => expect(screen.getByTestId('channel-list')).toBeTruthy());
373+
const targetChannel = channels[0].channel;
373374
const newMessage = generateMessage({
374-
cid: channels[0].channel.cid,
375+
cid: targetChannel.cid,
375376
user: generateUser(),
376377
});
377-
act(() => dispatchMessageNewEvent(chatClient, newMessage, channels[0].channel));
378+
act(() => dispatchMessageNewEvent(chatClient, newMessage, targetChannel));
378379

379380
await waitFor(async () => {
380381
const messagesRows = await BetterSqlite.selectFromTable('messages');
381-
const matchingRows = messagesRows.filter((m) => m.id === newMessage.id);
382-
expect(matchingRows.length).toBe(1);
383-
expect(matchingRows[0].id).toBe(newMessage.id);
382+
const readRows = await BetterSqlite.selectFromTable('reads');
383+
const matchingMessageRows = messagesRows.filter((m) => m.id === newMessage.id);
384+
const matchingReadRows = readRows.filter(
385+
(r) => targetChannel.cid === r.cid && chatClient.userID === r.userId,
386+
);
387+
388+
expect(matchingMessageRows.length).toBe(1);
389+
expect(matchingMessageRows[0].id).toBe(newMessage.id);
390+
expect(matchingReadRows.length).toBe(1);
391+
expect(matchingReadRows[0].unreadMessages).toBe(1);
392+
});
393+
});
394+
395+
it('should correctly handle multiple new messages and add them to the database', async () => {
396+
useMockedApis(chatClient, [queryChannelsApi(channels)]);
397+
398+
renderComponent();
399+
act(() => dispatchConnectionChangedEvent(chatClient));
400+
await act(async () => await chatClient.offlineDb.syncManager.invokeSyncStatusListeners(true));
401+
await waitFor(() => expect(screen.getByTestId('channel-list')).toBeTruthy());
402+
const targetChannel = channels[0].channel;
403+
404+
// check if the reads state is correct first
405+
await waitFor(async () => {
406+
const readRows = await BetterSqlite.selectFromTable('reads');
407+
const matchingReadRows = readRows.filter(
408+
(r) => targetChannel.cid === r.cid && chatClient.userID === r.userId,
409+
);
410+
411+
console.log('READROWS: ', readRows);
412+
413+
expect(matchingReadRows.length).toBe(1);
414+
expect(matchingReadRows[0].unreadMessages).toBe(0);
415+
});
416+
417+
const newMessages = [
418+
generateMessage({
419+
cid: targetChannel.cid,
420+
user: generateUser(),
421+
}),
422+
generateMessage({
423+
cid: targetChannel.cid,
424+
user: generateUser(),
425+
}),
426+
generateMessage({
427+
cid: targetChannel.cid,
428+
user: generateUser(),
429+
}),
430+
];
431+
432+
newMessages.forEach((newMessage) => {
433+
act(() => dispatchMessageNewEvent(chatClient, newMessage, targetChannel));
434+
});
435+
436+
await waitFor(async () => {
437+
const messagesRows = await BetterSqlite.selectFromTable('messages');
438+
const readRows = await BetterSqlite.selectFromTable('reads');
439+
const matchingMessageRows = messagesRows.filter((m) =>
440+
newMessages.some((newMessage) => newMessage.id === m.id),
441+
);
442+
const matchingReadRows = readRows.filter(
443+
(r) => targetChannel.cid === r.cid && chatClient.userID === r.userId,
444+
);
445+
446+
expect(matchingMessageRows.length).toBe(3);
447+
newMessages.forEach((newMessage) => {
448+
expect(
449+
matchingMessageRows.some(
450+
(matchingMessageRow) => matchingMessageRow.id === newMessage.id,
451+
),
452+
).toBe(true);
453+
});
454+
expect(matchingReadRows.length).toBe(1);
455+
expect(matchingReadRows[0].unreadMessages).toBe(3);
384456
});
385457
});
386458

package/src/mock-builders/generator/channel.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,16 @@ export const generateChannelResponse = (
9898
id?: string;
9999
messages?: Record<string, any>[];
100100
members?: Record<string, any>[];
101+
read?: Record<string, any>[];
101102
type?: string;
102-
} = { channel: {}, id: uuidv4(), members: [], messages: [], type: 'messaging' },
103+
} = { channel: {}, id: uuidv4(), members: [], messages: [], read: [], type: 'messaging' },
103104
) => {
104105
const {
105106
channel = {},
106107
id = uuidv4(),
107108
messages = [],
108109
members = [],
110+
read,
109111
type = 'messaging',
110112
...rest
111113
} = customValues;
@@ -125,6 +127,7 @@ export const generateChannelResponse = (
125127
},
126128
members,
127129
messages,
130+
read,
128131
...rest,
129132
};
130133
};

0 commit comments

Comments
 (0)