Skip to content

Commit 8cb975a

Browse files
committed
tests: add a few tests
1 parent a1dadf3 commit 8cb975a

File tree

2 files changed

+108
-62
lines changed

2 files changed

+108
-62
lines changed

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

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ import dispatchMessageNewEvent from '../../../mock-builders/event/messageNew';
2525
import dispatchNotificationAddedToChannelEvent from '../../../mock-builders/event/notificationAddedToChannel';
2626
import dispatchNotificationMessageNewEvent from '../../../mock-builders/event/notificationMessageNew';
2727
import dispatchNotificationRemovedFromChannel from '../../../mock-builders/event/notificationRemovedFromChannel';
28-
import dispatchUserPresenceEvent from '../../../mock-builders/event/userPresence';
29-
import dispatchUserUpdatedEvent from '../../../mock-builders/event/userUpdated';
3028
import { generateChannel, generateChannelResponse } from '../../../mock-builders/generator/channel';
3129
import { generateMessage } from '../../../mock-builders/generator/message';
3230
import { generateUser } from '../../../mock-builders/generator/user';
@@ -691,65 +689,5 @@ describe('ChannelList', () => {
691689
});
692690
});
693691
});
694-
695-
describe('user.updated', () => {
696-
it('should call handleEvent in the custom hook if the user updates', async () => {
697-
useMockedApis(chatClient, [queryChannelsApi([testChannel1])]);
698-
const updateSpy = jest.spyOn(chatClient, 'on');
699-
const offlineUser = generateUser();
700-
701-
render(
702-
<Chat client={chatClient}>
703-
<ChannelList {...props} />
704-
</Chat>,
705-
);
706-
707-
await waitFor(() => {
708-
expect(screen.getByTestId('channel-list')).toBeTruthy();
709-
});
710-
711-
act(() =>
712-
dispatchUserUpdatedEvent(
713-
chatClient,
714-
{ ...offlineUser, name: 'dan' },
715-
testChannel1.channel,
716-
),
717-
);
718-
719-
await waitFor(() => {
720-
expect(updateSpy).toHaveBeenCalledWith('user.updated', expect.any(Function));
721-
});
722-
});
723-
});
724-
725-
describe('user.presence.changed', () => {
726-
it('should call handleEvent in the custom hook if user presence changes', async () => {
727-
useMockedApis(chatClient, [queryChannelsApi([testChannel1])]);
728-
const updateSpy = jest.spyOn(chatClient, 'on');
729-
const offlineUser = generateUser();
730-
731-
render(
732-
<Chat client={chatClient}>
733-
<ChannelList {...props} />
734-
</Chat>,
735-
);
736-
737-
await waitFor(() => {
738-
expect(screen.getByTestId('channel-list')).toBeTruthy();
739-
});
740-
741-
act(() =>
742-
dispatchUserPresenceEvent(
743-
chatClient,
744-
{ ...offlineUser, online: true },
745-
testChannel1.channel,
746-
),
747-
);
748-
749-
await waitFor(() => {
750-
expect(updateSpy).toHaveBeenCalledWith('user.presence.changed', expect.any(Function));
751-
});
752-
});
753-
});
754692
});
755693
});
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { renderHook } from '@testing-library/react-native';
2+
import type { Channel, StreamChat, UserResponse } from 'stream-chat';
3+
4+
import type { ChatContextValue } from '../../../../contexts/chatContext/ChatContext';
5+
import * as ChatContext from '../../../../contexts/chatContext/ChatContext';
6+
import { getTestClientWithUser } from '../../../../mock-builders/mock';
7+
import { DefaultStreamChatGenerics } from '../../../../types/types';
8+
import { useChannelPreviewDisplayPresence } from '../useChannelPreviewDisplayPresence';
9+
10+
describe('useChannelPreviewDisplayPresence', () => {
11+
// Mock user data
12+
const currentUserId = 'current-user';
13+
const otherUserId = 'other-user';
14+
let chatClient: StreamChat<DefaultStreamChatGenerics>;
15+
16+
let mockChannel: Channel<DefaultStreamChatGenerics>;
17+
18+
beforeEach(async () => {
19+
jest.clearAllMocks();
20+
chatClient = await getTestClientWithUser({
21+
id: currentUserId,
22+
userID: currentUserId,
23+
});
24+
25+
// Create mock channel
26+
mockChannel = {
27+
state: {
28+
members: {
29+
[currentUserId]: {
30+
user: { id: currentUserId, online: true } as UserResponse<DefaultStreamChatGenerics>,
31+
},
32+
[otherUserId]: {
33+
user: { id: otherUserId, online: false } as UserResponse<DefaultStreamChatGenerics>,
34+
},
35+
},
36+
},
37+
} as unknown as Channel<DefaultStreamChatGenerics>;
38+
39+
// Mock the useChatContext hook
40+
jest
41+
.spyOn(ChatContext, 'useChatContext')
42+
.mockImplementation(() => ({ client: chatClient }) as unknown as ChatContextValue);
43+
});
44+
45+
afterEach(() => {
46+
jest.clearAllMocks();
47+
});
48+
49+
it('should return false for channels with more than 2 members', () => {
50+
// Create a channel with 3 members
51+
const thirdUserId = 'third-user';
52+
const channelWithThreeMembers = {
53+
state: {
54+
members: {
55+
[currentUserId]: {
56+
user: { id: currentUserId } as UserResponse<DefaultStreamChatGenerics>,
57+
},
58+
[otherUserId]: {
59+
user: { id: otherUserId } as UserResponse<DefaultStreamChatGenerics>,
60+
},
61+
[thirdUserId]: {
62+
user: { id: thirdUserId } as UserResponse<DefaultStreamChatGenerics>,
63+
},
64+
},
65+
},
66+
} as unknown as Channel<DefaultStreamChatGenerics>;
67+
68+
const { result } = renderHook(() => useChannelPreviewDisplayPresence(channelWithThreeMembers));
69+
expect(result.current).toBe(false);
70+
});
71+
72+
it('should return false when the other user is offline', () => {
73+
const { result } = renderHook(() => useChannelPreviewDisplayPresence(mockChannel));
74+
expect(result.current).toBe(false);
75+
});
76+
77+
it('should return true when the other user is online', () => {
78+
// Update the other user to be online
79+
const onlineUser = {
80+
...mockChannel.state.members[otherUserId].user,
81+
online: true,
82+
} as UserResponse<DefaultStreamChatGenerics>;
83+
84+
mockChannel.state.members[otherUserId].user = onlineUser;
85+
86+
const { result } = renderHook(() => useChannelPreviewDisplayPresence(mockChannel));
87+
expect(result.current).toBe(true);
88+
});
89+
90+
it('should handle null user gracefully', () => {
91+
// Create a channel with a member that has no user
92+
const channelWithNullUser = {
93+
state: {
94+
members: {
95+
[currentUserId]: {
96+
user: { id: currentUserId } as UserResponse<DefaultStreamChatGenerics>,
97+
},
98+
'null-user': {
99+
user: null,
100+
},
101+
},
102+
},
103+
} as unknown as Channel<DefaultStreamChatGenerics>;
104+
105+
const { result } = renderHook(() => useChannelPreviewDisplayPresence(channelWithNullUser));
106+
expect(result.current).toBe(false);
107+
});
108+
});

0 commit comments

Comments
 (0)