import { QueryClientProvider } from '@tanstack/react-query' import { act, renderHook, waitFor } from '@testing-library/react-native' import React from 'react' import { buildTestQueryClient } from '../../__utils__/query_client' import { ChatContext, ChatContextValue } from '../../contexts/chat_context' import { ApiClient, useApiClient } from '../../hooks/use_api_client' import { useCanBrowseArchivedConversations } from '../../hooks/use_can_browse_archived_conversations' import { currentPersonQueryKey, currentPersonRequestArgs } from '../../hooks/use_current_person' import { Session } from '../../utils' import { getFeaturesQueryKey } from '../../utils/request/get_features' jest.mock('../../hooks/use_api_client') const mockedUseApiClient = useApiClient as jest.MockedFunction let get: jest.Mock const session = new Session({ token: { access_token: 'a-real-token' } }) const renderWithFeatures = (enabledFlags: string[], cachedPerson?: object) => { const queryClient = buildTestQueryClient() if (cachedPerson) queryClient.setQueryData(currentPersonQueryKey, { data: cachedPerson }) queryClient.setQueryData(getFeaturesQueryKey(), { data: enabledFlags.map((name, index) => ({ type: 'Feature', id: `${index}`, name, enabled: true, })), links: {}, meta: { count: enabledFlags.length, totalCount: enabledFlags.length }, }) const wrapper = ({ children }: { children: React.ReactNode }) => ( {children} ) return renderHook(() => useCanBrowseArchivedConversations(), { wrapper }) } const settle = () => act(() => new Promise(resolve => setTimeout(resolve, 0))) beforeEach(() => { get = jest.fn().mockResolvedValue({ data: { id: 1, canBrowseArchivedConversations: true } }) mockedUseApiClient.mockReturnValue({ chat: { get } } as unknown as ApiClient) }) afterEach(() => { jest.clearAllMocks() }) describe('useCanBrowseArchivedConversations', () => { it.each(['QA_MOBILE_manual_archiving', 'ROLLOUT_MOBILE_manual_archiving'])( 'is true under %s once the person can browse archived conversations', async flag => { const { result } = renderWithFeatures([flag]) await waitFor(() => expect(result.current).toBe(true)) expect(get).toHaveBeenCalledWith(expect.objectContaining(currentPersonRequestArgs)) } ) it('reads the cached current person without another request', async () => { const { result } = renderWithFeatures(['ROLLOUT_MOBILE_manual_archiving'], { id: 1, canBrowseArchivedConversations: true, }) await settle() expect(result.current).toBe(true) expect(get).not.toHaveBeenCalled() }) it('is false when the person has nothing archived to browse', async () => { get.mockResolvedValue({ data: { id: 1, canBrowseArchivedConversations: false } }) const { result } = renderWithFeatures(['ROLLOUT_MOBILE_manual_archiving']) await waitFor(() => expect(get).toHaveBeenCalled()) await settle() expect(result.current).toBe(false) }) it('is false and skips the request when no mobile flag is on', async () => { const { result } = renderWithFeatures(['ROLLOUT_WEB_manual_archiving']) await settle() expect(result.current).toBe(false) expect(get).not.toHaveBeenCalled() }) })