Skip to content

Commit dc669fd

Browse files
test: fix MessageList tests
1 parent f4136f0 commit dc669fd

File tree

4 files changed

+71
-85
lines changed

4 files changed

+71
-85
lines changed

src/components/MessageList/__tests__/MessageList.test.js

Lines changed: 56 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ jest.mock('../../EmptyStateIndicator', () => ({
3535

3636
const UNREAD_MESSAGES_SEPARATOR_TEST_ID = 'unread-messages-separator';
3737

38-
let chatClient;
39-
let channel;
4038
const user1 = generateUser();
4139
const user2 = generateUser();
4240
const message1 = generateMessage({ text: 'message1', user: user1 });
@@ -59,29 +57,34 @@ const renderComponent = ({ channelProps, chatClient, msgListProps }) =>
5957
);
6058

6159
describe('MessageList', () => {
60+
let chatClient;
61+
let channel;
62+
let markReadMock;
63+
6264
beforeEach(async () => {
6365
chatClient = await getTestClientWithUser({ id: 'vishal' });
6466
useMockedApis(chatClient, [getOrCreateChannelApi(mockedChannelData)]);
6567
channel = chatClient.channel('messaging', mockedChannelData.id);
6668
await channel.watch();
69+
70+
markReadMock = jest
71+
.spyOn(channel, 'markRead')
72+
.mockResolvedValue(markReadApi(channel));
6773
});
6874

6975
afterEach(() => {
7076
cleanup();
7177
jest.clearAllMocks();
78+
markReadMock.mockRestore();
7279
});
7380

7481
it('should add new message at the bottom of the list', async () => {
75-
const { getByTestId, getByText } = renderComponent({
82+
const { findByTestId, getByText } = renderComponent({
7683
channelProps: { channel },
7784
chatClient,
7885
});
79-
const markReadMock = jest
80-
.spyOn(channel, 'markRead')
81-
.mockReturnValueOnce(markReadApi(channel));
82-
await waitFor(() => {
83-
expect(getByTestId('reverse-infinite-scroll')).toBeInTheDocument();
84-
});
86+
87+
expect(await findByTestId('reverse-infinite-scroll')).toBeInTheDocument();
8588

8689
const newMessage = generateMessage({ user: user2 });
8790
act(() => dispatchMessageNewEvent(chatClient, newMessage, mockedChannelData.channel));
@@ -92,7 +95,6 @@ describe('MessageList', () => {
9295
// MessageErrorIcon has path with id "background" - that is not permitted from the a11i standpoint
9396
// const results = await axe(container);
9497
// expect(results).toHaveNoViolations();
95-
markReadMock.mockRestore();
9698
});
9799

98100
it('should render the thread head if provided', async () => {
@@ -134,12 +136,10 @@ describe('MessageList', () => {
134136
});
135137

136138
it('should render EmptyStateIndicator with corresponding list type in main message list', async () => {
137-
await act(() => {
138-
renderComponent({
139-
channelProps: { channel },
140-
chatClient,
141-
msgListProps: { messages: [] },
142-
});
139+
renderComponent({
140+
channelProps: { channel },
141+
chatClient,
142+
msgListProps: { messages: [] },
143143
});
144144

145145
await waitFor(() => {
@@ -151,12 +151,10 @@ describe('MessageList', () => {
151151
});
152152

153153
it('should not render EmptyStateIndicator with corresponding list type in thread', async () => {
154-
await act(() => {
155-
renderComponent({
156-
channelProps: { channel },
157-
chatClient,
158-
msgListProps: { messages: [], threadList: true },
159-
});
154+
renderComponent({
155+
channelProps: { channel },
156+
chatClient,
157+
msgListProps: { messages: [], threadList: true },
160158
});
161159

162160
await waitFor(() => {
@@ -165,15 +163,12 @@ describe('MessageList', () => {
165163
});
166164

167165
it('Message UI components should render `Avatar` when the custom prop is provided', async () => {
168-
let renderResult;
169-
await act(() => {
170-
renderResult = renderComponent({
171-
channelProps: {
172-
Avatar,
173-
channel,
174-
},
175-
chatClient,
176-
});
166+
const renderResult = renderComponent({
167+
channelProps: {
168+
Avatar,
169+
channel,
170+
},
171+
chatClient,
177172
});
178173

179174
await waitFor(() => {
@@ -186,19 +181,14 @@ describe('MessageList', () => {
186181

187182
it('should accept a custom group style function', async () => {
188183
const classNameSuffix = 'msg-list-test';
189-
const markReadMock = jest
190-
.spyOn(channel, 'markRead')
191-
.mockReturnValueOnce(markReadApi(channel));
192184

193-
await act(() => {
194-
renderComponent({
195-
channelProps: {
196-
Avatar,
197-
channel,
198-
},
199-
chatClient,
200-
msgListProps: { groupStyles: () => classNameSuffix },
201-
});
185+
renderComponent({
186+
channelProps: {
187+
Avatar,
188+
channel,
189+
},
190+
chatClient,
191+
msgListProps: { groupStyles: () => classNameSuffix },
202192
});
203193

204194
await waitFor(() => {
@@ -220,17 +210,12 @@ describe('MessageList', () => {
220210
// MessageErrorIcon has path with id "background" - that is not permitted from the a11i standpoint
221211
// const results = await axe(renderResult.container);
222212
// expect(results).toHaveNoViolations();
223-
markReadMock.mockRestore();
224213
});
225214

226215
it('should render DateSeparator by default', async () => {
227-
let container;
228-
await act(() => {
229-
const result = renderComponent({
230-
channelProps: { channel },
231-
chatClient,
232-
});
233-
container = result.container;
216+
const { container } = renderComponent({
217+
channelProps: { channel },
218+
chatClient,
234219
});
235220

236221
await waitFor(() => {
@@ -242,14 +227,10 @@ describe('MessageList', () => {
242227
});
243228

244229
it('should not render DateSeparator if disableDateSeparator is true', async () => {
245-
let container;
246-
await act(() => {
247-
const result = renderComponent({
248-
channelProps: { channel },
249-
chatClient,
250-
msgListProps: { disableDateSeparator: true },
251-
});
252-
container = result.container;
230+
const { container } = renderComponent({
231+
channelProps: { channel },
232+
chatClient,
233+
msgListProps: { disableDateSeparator: true },
253234
});
254235

255236
await waitFor(() => {
@@ -265,14 +246,12 @@ describe('MessageList', () => {
265246
const headerText = 'header is rendered';
266247
const Header = () => <div>{headerText}</div>;
267248

268-
await act(() => {
269-
renderComponent({
270-
channelProps: { channel, HeaderComponent: Header },
271-
chatClient,
272-
msgListProps: {
273-
messages: [intro],
274-
},
275-
});
249+
renderComponent({
250+
channelProps: { channel, HeaderComponent: Header },
251+
chatClient,
252+
msgListProps: {
253+
messages: [intro],
254+
},
276255
});
277256

278257
await waitFor(() => {
@@ -286,14 +265,12 @@ describe('MessageList', () => {
286265
type: 'system',
287266
});
288267

289-
await act(() => {
290-
renderComponent({
291-
channelProps: { channel },
292-
chatClient,
293-
msgListProps: {
294-
messages: [system],
295-
},
296-
});
268+
renderComponent({
269+
channelProps: { channel },
270+
chatClient,
271+
msgListProps: {
272+
messages: [system],
273+
},
297274
});
298275

299276
await waitFor(() => {
@@ -305,12 +282,10 @@ describe('MessageList', () => {
305282
const customRenderMessages = ({ messages }) =>
306283
messages.map((msg) => <li key={msg.id}>prefixed {msg.text}</li>);
307284

308-
await act(() => {
309-
renderComponent({
310-
channelProps: { channel },
311-
chatClient,
312-
msgListProps: { renderMessages: customRenderMessages },
313-
});
285+
renderComponent({
286+
channelProps: { channel },
287+
chatClient,
288+
msgListProps: { renderMessages: customRenderMessages },
314289
});
315290

316291
await waitFor(() => {

src/components/MessageList/__tests__/utils.test.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,11 +486,20 @@ describe('getGroupStyles', () => {
486486

487487
it('is date message', () => {
488488
if (position === 'bottom') {
489-
nextMessage = { ...nextMessage, customType: CUSTOM_MESSAGE_TYPE.date };
489+
nextMessage = {
490+
...nextMessage,
491+
customType: CUSTOM_MESSAGE_TYPE.date,
492+
date: new Date(),
493+
};
490494
}
491495
if (position === 'top') {
492-
previousMessage = { ...previousMessage, customType: CUSTOM_MESSAGE_TYPE.date };
496+
previousMessage = {
497+
...previousMessage,
498+
customType: CUSTOM_MESSAGE_TYPE.date,
499+
date: new Date(),
500+
};
493501
}
502+
494503
expect(getGroupStyles(message, previousMessage, nextMessage, noGroupByUser)).toBe(
495504
position,
496505
);

src/components/MessageList/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,10 @@ export function isIntroMessage(message: unknown): message is IntroMessage {
373373
export function isDateSeparatorMessage(
374374
message: unknown,
375375
): message is DateSeparatorMessage {
376+
if (typeof message === 'undefined') return false;
377+
376378
return (
377379
(message as DateSeparatorMessage).customType === CUSTOM_MESSAGE_TYPE.date &&
378-
!!(message as DateSeparatorMessage).date &&
379380
isDate((message as DateSeparatorMessage).date)
380381
);
381382
}

src/i18n/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export const isDayOrMoment = (
2424
output: TDateTimeParserOutput,
2525
): output is Dayjs.Dayjs | Moment => !!(output as Dayjs.Dayjs | Moment)?.isSame;
2626

27-
export const isDate = (output: unknown): output is Date => !!(output as Date)?.getMonth;
27+
export const isDate = (output: unknown): output is Date =>
28+
typeof (output as Date).getTime === 'function';
2829

2930
export function getDateString({
3031
calendar,

0 commit comments

Comments
 (0)