import { act, renderHook } from '@testing-library/react-native' import type { ConversationResource } from '../../types' import { availableFeatures } from '../use_features' import { useUnarchiveConversationAction } from '../use_unarchive_conversation_action' const mockSetArchived = jest.fn() jest.mock('../use_conversation', () => ({ useConversationArchive: () => ({ setArchived: mockSetArchived, isPending: false }), })) let mockEnabledFeatures: string[] = [] jest.mock('../use_features', () => ({ ...jest.requireActual('../use_features'), useFeatures: () => ({ features: [], featureEnabled: (name: string) => mockEnabledFeatures.includes(name), }), })) const conversation = ({ disabledReason = 'archived', canUnarchive = true, }: { disabledReason?: string | null canUnarchive?: boolean } = {}) => ({ type: 'Conversation', id: 7, disabled: !!disabledReason, disabledReason, memberAbility: { type: 'MemberAbility', canUnarchive }, }) as unknown as ConversationResource const renderAction = (resource: ConversationResource) => renderHook(() => useUnarchiveConversationAction(resource)).result.current describe('useUnarchiveConversationAction', () => { beforeEach(() => { mockEnabledFeatures = [availableFeatures.manual_archiving_rollout] jest.clearAllMocks() }) it('unarchives an archived conversation the person can unarchive', () => { const action = renderAction(conversation()) expect(action?.label).toBe('Unarchive') act(() => action?.onPress()) expect(mockSetArchived).toHaveBeenCalledWith(false) }) it.each([ ['neither mobile flag is on', () => (mockEnabledFeatures = []), conversation()], ['the person cannot unarchive', () => {}, conversation({ canUnarchive: false })], [ 'the conversation is disabled for another reason', () => {}, conversation({ disabledReason: 'safety_check' }), ], ])('offers nothing when %s', (_case, setup, resource) => { setup() expect(renderAction(resource)).toBeUndefined() }) })