Skip to content

Commit 100f39c

Browse files
author
Dan Carbonell
committed
add loading tests and refactor MessageList
1 parent 7c67ba5 commit 100f39c

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

src/components/ChannelList/__tests__/ChannelListMessenger.test.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react';
2-
import { Text, View } from 'react-native';
32
import { cleanup, render, waitFor } from '@testing-library/react-native';
43

54
import { getOrCreateChannelApi } from 'mock-builders/api/getOrCreateChannel';
@@ -20,11 +19,6 @@ const Component = ({ channels, error = false, loadingChannels = false }) => (
2019
channels={channels}
2120
error={error}
2221
loadingChannels={loadingChannels}
23-
LoadingIndicator={() => (
24-
<View testID='loading-indicator'>
25-
<Text>Loading Indicator</Text>
26-
</View>
27-
)}
2822
/>
2923
</Chat>
3024
);
@@ -74,7 +68,7 @@ describe('ChannelListMessenger', () => {
7468
<Component channels={[]} loadingChannels={true} />,
7569
);
7670
await waitFor(() => {
77-
expect(getByTestId('loading-indicator')).toBeTruthy();
71+
expect(getByTestId('loading')).toBeTruthy();
7872
});
7973
});
8074
});

src/components/MessageList/MessageList.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,16 @@ export const MessageList = <
285285
);
286286
const [newMessagesNotification, setNewMessageNotification] = useState(false);
287287

288+
/**
289+
* In order to prevent the LoadingIndicator component from showing up briefly on mount,
290+
* we set the loading state one cycle behind to ensure the messages are set before the
291+
* change to the loading state is registered.
292+
*/
293+
const [messagesLoading, setMessagesLoading] = useState(false);
294+
useEffect(() => {
295+
setMessagesLoading(!!loading);
296+
}, [loading]);
297+
288298
useEffect(() => {
289299
const currentLastMessage = getLastReceivedMessage(messageList);
290300
if (currentLastMessage) {
@@ -378,8 +388,10 @@ export const MessageList = <
378388
// We can't provide ListEmptyComponent to FlatList when inverted flag is set.
379389
// https://github.com/facebook/react-native/issues/21196
380390
if (messageList.length === 0 && !threadList) {
381-
return (
382-
<View style={{ flex: 1 }}>
391+
return messagesLoading ? (
392+
<LoadingIndicator listType='message' />
393+
) : (
394+
<View style={{ flex: 1 }} testID='empty-state'>
383395
<EmptyStateIndicator listType='message' />
384396
</View>
385397
);
@@ -398,9 +410,6 @@ export const MessageList = <
398410
inverted
399411
keyboardShouldPersistTaps='always'
400412
keyExtractor={keyExtractor}
401-
ListEmptyComponent={
402-
loading ? <LoadingIndicator listType='message' /> : null
403-
}
404413
ListFooterComponent={HeaderComponent}
405414
maintainVisibleContentPosition={{
406415
autoscrollToTopThreshold: 10,

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,31 @@ describe('MessageList', () => {
129129
});
130130
});
131131

132+
it('should render the EmptyStateIndicator when no messages loaded', async () => {
133+
const user1 = generateUser();
134+
const mockedChannel = generateChannel({
135+
members: [generateMember({ user: user1 })],
136+
messages: [],
137+
});
138+
139+
const chatClient = await getTestClientWithUser({ id: 'testID' });
140+
useMockedApis(chatClient, [getOrCreateChannelApi(mockedChannel)]);
141+
const channel = chatClient.channel('messaging', mockedChannel.id);
142+
await channel.query();
143+
144+
const { getByTestId } = render(
145+
<Chat client={chatClient}>
146+
<Channel channel={channel}>
147+
<MessageList />
148+
</Channel>
149+
</Chat>,
150+
);
151+
152+
await waitFor(() => {
153+
expect(getByTestId('empty-state')).toBeTruthy();
154+
});
155+
});
156+
132157
it('should render the is offline error', async () => {
133158
const user1 = generateUser();
134159
const mockedChannel = generateChannel({

0 commit comments

Comments
 (0)