import { QueryClientProvider } from '@tanstack/react-query' import { renderHook, waitFor } from '@testing-library/react-native' import React from 'react' import { buildTestQueryClient } from '../../__utils__/query_client' import { useGroupChatConversationPayload } from '../../hooks/groups/use_group_chat_conversation_payload' import * as useApiClientModule from '../../hooks/use_api_client' const mockGroupsPost = (impl: jest.Mock) => { jest.spyOn(useApiClientModule, 'useApiClient').mockReturnValue({ groups: { post: impl }, } as unknown as ReturnType) return impl } const resolveWith = (value: string) => jest.fn().mockResolvedValue({ data: { type: 'GroupChatConversationPayload', id: '1', value }, links: {}, meta: {}, }) describe('useGroupChatConversationPayload', () => { afterEach(() => { jest.restoreAllMocks() }) it('refetches on mount even when a fresh payload is already cached', async () => { // Prime the cache as if a payload was fetched moments ago. The hook's 25-min staleTime // means the default refetchOnMount would treat this as fresh and skip the request, so a // group composition change on web would stay invisible until the cache is dropped (app // restart). refetchOnMount: 'always' must override that and refetch on every mount. const queryClient = buildTestQueryClient() queryClient.setQueryData(['groups', '/me/groups/1/chat_conversation_payload'], { data: { type: 'GroupChatConversationPayload', id: '1', value: 'stale' }, links: {}, meta: {}, }) const post = mockGroupsPost(resolveWith('fresh')) const wrapper = ({ children }: { children: React.ReactNode }) => ( {children} ) const { result } = renderHook(() => useGroupChatConversationPayload({ groupId: 1 }), { wrapper, }) await waitFor(() => expect(post).toHaveBeenCalledTimes(1)) await waitFor(() => expect(result.current.payload).toBe('fresh')) }) })