import { QueryClient, QueryClientProvider } from '@tanstack/react-query' import { renderHook } from '@testing-library/react-native' import React from 'react' import { buildTestQueryClient } from '../../__utils__/query_client' import type { ApiResource, ConversationResource } from '../../types' import { getConversationRequestArgs } from '../use_conversation' import { useConversationJoltEvents } from '../use_conversation_jolt_events' import { getRequestQueryKey } from '../use_suspense_api' const handlers: Record void> = {} jest.mock('../use_jolt', () => ({ useJoltChannel: () => 'channel', useJoltEvent: (_channel: unknown, name: string, handler: (event: unknown) => void) => { handlers[name] = handler }, })) jest.mock('../use_current_person', () => ({ useCurrentPerson: () => ({ id: 1 }) })) jest.mock('../use_api_client', () => ({ useApiClient: () => ({ chat: {} }) })) const conversationId = 7 const queryKey = getRequestQueryKey(getConversationRequestArgs({ conversation_id: conversationId })) let queryClient: QueryClient const wrapper = ({ children }: { children: React.ReactNode }) => ( {children} ) const archivedAttributes = { disabled: true, disabled_reason: 'archived', disabled_message: 'This conversation is archived.', } const activeAttributes = { disabled: false, disabled_reason: null, disabled_message: null } const seed = (conversation: Partial) => queryClient.setQueryData>(queryKey, { data: { type: 'Conversation', id: conversationId, title: 'Youth', ...conversation }, } as ApiResource) const cached = () => queryClient.getQueryData>(queryKey)!.data const conversationUpdated = (attributes: Record) => handlers['conversation.updated']({ event: 'conversation.updated', data: { data: { id: conversationId, ...attributes } }, }) beforeEach(() => { queryClient = buildTestQueryClient() renderHook(() => useConversationJoltEvents({ conversationId }), { wrapper }) }) describe('useConversationJoltEvents on conversation.updated', () => { it('switches the cached conversation into the archived state', () => { seed({ disabled: false, disabledReason: null, disabledMessage: null }) conversationUpdated(archivedAttributes) expect(cached()).toMatchObject({ title: 'Youth', disabled: true, disabledReason: 'archived', disabledMessage: 'This conversation is archived.', }) }) it('switches the cached conversation back out of the archived state', () => { seed({ disabled: true, disabledReason: 'archived', disabledMessage: 'Archived' }) conversationUpdated(activeAttributes) expect(cached()).toMatchObject({ disabled: false, disabledReason: null, disabledMessage: null }) }) })