// FE-JRN-SUBSCRIBERS-HOOK-001 to FE-JRN-SUBSCRIBERS-HOOK-003 // // #1669: the addresses following a journey link, as the owner's share section reads them. import { beforeEach, describe, expect, it, vi } from 'vitest'; import { act, renderHook, waitFor } from '../../../tests/helpers/render'; import { useJourneySubscribers } from './useJourneySubscribers'; const repo = vi.hoisted(() => ({ subscribers: vi.fn(), removeSubscriber: vi.fn() })); vi.mock('../../repo/journeySocialRepo', () => ({ journeySocialRepo: repo })); const LIST = { count: 1, pending: 1, subscribers: [ { id: 1, email: 'ana@example.com', confirmed: true, created_at: null, confirmed_at: null, last_notified_at: null, }, { id: 2, email: 'bo@example.com', confirmed: false, created_at: null, confirmed_at: null, last_notified_at: null }, ], }; beforeEach(() => { repo.subscribers.mockReset(); repo.removeSubscriber.mockReset(); }); describe('useJourneySubscribers', () => { it('FE-JRN-SUBSCRIBERS-HOOK-001: reads nothing until enabled, then the list', async () => { repo.subscribers.mockResolvedValue(LIST); const { result, rerender } = renderHook(({ on }) => useJourneySubscribers(7, on), { initialProps: { on: false } }); expect(result.current.status).toBe('idle'); expect(repo.subscribers).not.toHaveBeenCalled(); rerender({ on: true }); await waitFor(() => expect(result.current.status).toBe('ready')); expect(result.current.count).toBe(1); expect(result.current.pending).toBe(1); expect(result.current.subscribers).toHaveLength(2); }); it('FE-JRN-SUBSCRIBERS-HOOK-002: a removal takes the address and its count off', async () => { repo.subscribers.mockResolvedValue(LIST); repo.removeSubscriber.mockResolvedValue(undefined); const { result } = renderHook(() => useJourneySubscribers(7, true)); await waitFor(() => expect(result.current.status).toBe('ready')); await act(async () => { expect(await result.current.remove(1)).toBe(true); }); expect(repo.removeSubscriber).toHaveBeenCalledWith(7, 1); expect(result.current).toMatchObject({ count: 0, pending: 1 }); await act(async () => { await result.current.remove(2); }); expect(result.current).toMatchObject({ count: 0, pending: 0, subscribers: [] }); }); it('FE-JRN-SUBSCRIBERS-HOOK-003: a failed read says so, a refused removal keeps the list', async () => { repo.subscribers.mockRejectedValueOnce(new Error('403')); const failing = renderHook(() => useJourneySubscribers(7, true)); await waitFor(() => expect(failing.result.current.status).toBe('failed')); repo.subscribers.mockResolvedValue(LIST); repo.removeSubscriber.mockRejectedValue(new Error('404')); const { result } = renderHook(() => useJourneySubscribers(8, true)); await waitFor(() => expect(result.current.status).toBe('ready')); await act(async () => { expect(await result.current.remove(1)).toBe(false); }); expect(result.current.subscribers).toHaveLength(2); }); });