-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Filter Conversation Notification Badge
- Loading branch information
Showing
3 changed files
with
157 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import React from 'react'; | ||
import { renderHook, waitFor } from '@testing-library/react-native'; | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { mockGraphQLResponse } from '../common/testHelpers/mockGraphQLResponse'; | ||
import { useHasUnread } from './useConversations'; | ||
import { useProfilesForTile } from './useMessagingProfiles'; | ||
import { useUser } from './useUser'; | ||
import { GraphQLClientContextProvider } from './useGraphQLClient'; | ||
|
||
const baseURL = 'http://localhost:8080/unit-test'; | ||
const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
retry: false, | ||
}, | ||
}, | ||
}); | ||
|
||
jest.mock('./useActiveAccount', () => ({ | ||
useActiveAccount: jest.fn().mockReturnValue({ account: 'account' }), | ||
})); | ||
jest.mock('./useMessagingProfiles', () => ({ | ||
useProfilesForTile: jest.fn(), | ||
})); | ||
jest.mock('./useUser', () => ({ | ||
useUser: jest.fn(), | ||
})); | ||
|
||
const useProfilesForTileMock = useProfilesForTile as jest.Mock; | ||
const useUserMock = useUser as jest.Mock; | ||
|
||
const renderHookInContext = <T extends any>(useHook: () => T) => { | ||
return renderHook(() => useHook(), { | ||
wrapper: ({ children }) => ( | ||
<QueryClientProvider client={queryClient}> | ||
<GraphQLClientContextProvider baseURL={baseURL}> | ||
{children} | ||
</GraphQLClientContextProvider> | ||
</QueryClientProvider> | ||
), | ||
}); | ||
}; | ||
|
||
beforeEach(() => { | ||
useProfilesForTileMock.mockReturnValue({ | ||
data: [{ id: 'profile1' }], | ||
}); | ||
useUserMock.mockReturnValue({ data: { id: 'userId' } }); | ||
}); | ||
|
||
describe('useHasUnread', () => { | ||
test('returns true if there are matching conversations', async () => { | ||
const scope = mockGraphQLResponse( | ||
`${baseURL}/v1/graphql`, | ||
{}, | ||
{ | ||
conversations: { | ||
edges: [ | ||
{ | ||
node: { | ||
conversationId: 'conversation1', | ||
userIds: ['userId', 'profile1'], | ||
hasUnread: true, | ||
}, | ||
}, | ||
], | ||
pageInfo: { hasNextPage: false }, | ||
}, | ||
}, | ||
); | ||
|
||
const { result } = renderHookInContext(() => useHasUnread('')); | ||
|
||
await waitFor(() => scope.done()); | ||
|
||
expect(result.current).toBe(true); | ||
}); | ||
|
||
test('returns false if there are conversations with no matching profiles', async () => { | ||
const scope = mockGraphQLResponse( | ||
`${baseURL}/v1/graphql`, | ||
{}, | ||
{ | ||
conversations: { | ||
edges: [ | ||
{ | ||
node: { | ||
conversationId: 'conversation2', | ||
userIds: ['userId', 'profile2'], | ||
hasUnread: true, | ||
}, | ||
}, | ||
], | ||
pageInfo: { hasNextPage: false }, | ||
}, | ||
}, | ||
); | ||
|
||
const { result } = renderHookInContext(() => useHasUnread('')); | ||
|
||
await waitFor(() => scope.done()); | ||
|
||
expect(result.current).toBe(false); | ||
}); | ||
|
||
test('handles missing user and profile data', async () => { | ||
// Simulate data not loaded | ||
useProfilesForTileMock.mockReturnValue({ data: undefined }); | ||
useUserMock.mockReturnValue({ data: undefined }); | ||
|
||
const scope = mockGraphQLResponse( | ||
`${baseURL}/v1/graphql`, | ||
{}, | ||
{ | ||
conversations: { | ||
edges: [ | ||
{ | ||
node: { | ||
conversationId: 'conversation2', | ||
userIds: ['userId', 'profile1'], | ||
hasUnread: true, | ||
}, | ||
}, | ||
], | ||
pageInfo: { hasNextPage: false }, | ||
}, | ||
}, | ||
); | ||
|
||
const { result } = renderHookInContext(() => useHasUnread('')); | ||
|
||
await waitFor(() => scope.done()); | ||
|
||
expect(result.current).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters