|
| 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