-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathVirtualizedMessageList.test.js
More file actions
158 lines (133 loc) Β· 4.78 KB
/
VirtualizedMessageList.test.js
File metadata and controls
158 lines (133 loc) Β· 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import React, { act } from 'react';
import { cleanup, render } from '@testing-library/react';
import * as nanoid from 'nanoid';
import '@testing-library/jest-dom';
import {
generateChannel,
generateMember,
generateMessage,
generateUser,
getOrCreateChannelApi,
getTestClientWithUser,
useMockedApis,
} from '../../../mock-builders';
import { usePrependedMessagesCount } from '../hooks';
import { VirtualizedMessageList } from '../VirtualizedMessageList';
import { Chat } from '../../Chat';
import { Channel } from '../../Channel';
jest.mock('react-virtuoso', () => {
const { Virtuoso } = jest.requireActual('react-virtuoso');
const { forwardRef } = jest.requireActual('react');
return {
Virtuoso: forwardRef((props, ref) => (
<Virtuoso
ref={ref}
{...props}
fixedItemHeight={30}
initialTopMostItemIndex={0}
overscan={0}
/>
)),
};
});
jest.mock('../../Loading', () => ({
LoadingIndicator: jest.fn(() => <div>LoadingIndicator</div>),
}));
jest.mock('../../Message', () => ({
FixedHeightMessage: jest.fn(({ groupedByUser }) => (
<div data-testid='msg'>
FixedHeightMessage groupedByUser:
{groupedByUser ? 'true' : 'false'}
</div>
)),
}));
async function createChannel(empty = false) {
const user1 = generateUser();
const user2 = generateUser();
const mockedChannel = generateChannel({
members: [generateMember({ user: user1 }), generateMember({ user: user2 })],
messages: empty
? []
: ' '
.repeat(20)
.split(' ')
.map((v, i) => generateMessage({ user: i % 3 ? user1 : user2 })),
});
const client = await getTestClientWithUser({ id: 'id' });
useMockedApis(client, [getOrCreateChannelApi(mockedChannel)]); // eslint-disable-line react-hooks/rules-of-hooks
const channel = client.channel('messaging', mockedChannel.id);
await channel.watch();
return { channel, client };
}
// simple test since Virtuoso heavily relies on document height and jsdom doesn't support it
describe('VirtualizedMessageList', () => {
afterEach(cleanup);
beforeEach(jest.clearAllMocks);
it('should render the list without any message', async () => {
const { channel, client } = await createChannel(true);
jest.spyOn(nanoid, 'nanoid').mockReturnValue('mockedId');
let result;
await act(() => {
result = render(
<Chat client={client}>
<Channel channel={channel}>
<VirtualizedMessageList />
</Channel>
</Chat>,
);
});
expect(result.container).toMatchSnapshot();
});
});
describe('usePrependedMessagesCount', () => {
const TestCase = ({ messages }) => {
const prependCount = usePrependedMessagesCount(messages);
return <div data-prepend-count={prependCount} id='prepend-counter' />;
};
const expectPrependCount = (count, container) => {
expect(container.querySelector('#prepend-counter')).toHaveAttribute(
'data-prepend-count',
count.toString(),
);
};
it('determines 0 prepended messages for empty message list', async () => {
const { container } = await render(<TestCase messages={[]} />);
expectPrependCount(0, container);
});
const messageBatch = (ids, status) => ids.map((id) => ({ id, status }));
const firstBatch = (status) => messageBatch(['a'], status);
const secondBatch = (status) => messageBatch(['c', 'b'], status);
const thirdBatch = (status) => messageBatch(['e', 'd'], status);
const testPrependCount = async (status, first, second, third) => {
const firstMessage = firstBatch(status);
const secondMsgBatch = secondBatch(status);
const thirdMsgBatch = thirdBatch(status);
const { container, rerender } = await render(<TestCase messages={[]} />);
await act(async () => {
await rerender(<TestCase messages={[...firstMessage]} />);
});
expectPrependCount(first, container);
await act(async () => {
await rerender(<TestCase messages={[...secondMsgBatch, ...firstMessage]} />);
});
expectPrependCount(second, container);
await act(async () => {
await rerender(
<TestCase messages={[...thirdMsgBatch, ...secondMsgBatch, ...firstMessage]} />,
);
});
expectPrependCount(third, container);
};
it('calculates the prepended count for messages of status "received"', async () => {
await testPrependCount('received', 0, 2, 4);
});
it('ignores the messages of status "sending" from the prepended messages count', async () => {
await testPrependCount('sending', 0, 0, 0);
});
it('ignores the messages of status "failed" from the prepended messages count', async () => {
await testPrependCount('failed', 0, 0, 0);
});
it('calculates the prepended messages count for the messages of status undefined', async () => {
await testPrependCount(undefined, 0, 2, 4);
});
});