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 { useDirectMessageAffiliations } from '../../hooks/direct_messages/use_direct_message_affiliations' import { ApiClient, useApiClient } from '../../hooks/use_api_client' jest.mock('../../hooks/use_api_client') const mockedUseApiClient = useApiClient as jest.MockedFunction let get: jest.Mock const renderUseDirectMessageAffiliations = () => { const wrapper = ({ children }: { children: React.ReactNode }) => ( {children} ) return renderHook(() => useDirectMessageAffiliations(), { wrapper }) } beforeEach(() => { get = jest.fn().mockResolvedValue({ data: { id: '1', type: 'DirectMessageAffiliations', groups: { '10': 'Youth' }, teams: { '20': 'Band' }, resultsDescription: 'Who you can message', }, }) mockedUseApiClient.mockReturnValue({ chat: { get } } as unknown as ApiClient) }) afterEach(() => { jest.clearAllMocks() }) describe('useDirectMessageAffiliations', () => { it('requests the results description alongside groups and teams', async () => { renderUseDirectMessageAffiliations() await waitFor(() => expect(get).toHaveBeenCalled()) expect(get).toHaveBeenCalledWith( expect.objectContaining({ url: '/me/direct_message_affiliations', data: { fields: { DirectMessageAffiliations: ['groups', 'teams', 'results_description'] } }, }) ) }) it('exposes the server rendered results description', async () => { const { result } = renderUseDirectMessageAffiliations() await waitFor(() => expect(result.current.resultsDescription).toBe('Who you can message')) expect(result.current.groups.get(10)).toBe('Youth') expect(result.current.teams.get(20)).toBe('Band') }) it('returns null when the server sends no description', async () => { get.mockResolvedValue({ data: { id: '1', type: 'DirectMessageAffiliations', groups: {}, teams: {} }, }) const { result } = renderUseDirectMessageAffiliations() await waitFor(() => expect(get).toHaveBeenCalled()) await waitFor(() => expect(result.current.groups.size).toBe(0)) expect(result.current.resultsDescription).toBeNull() }) })