Files
TREK/client/src/components/Journey/useJourneySubscribers.test.ts
T
Maurice f1e64ef686 feat(journey): read and write comments and followers in the client hooks
The data side of journey comments and following a link by email (#1702,
#1669), for the views to draw on, online only like the journey itself:

- api/journeySocial and repo/journeySocialRepo, parsed against the shared
  contracts.
- useJourneyComments: a journey's comments grouped by entry, add, hide and
  delete, read again on journey:entry:commented.
- useJourneySubscribers: the owner's list of followers and their removal.
- useJourneyPublicSocial: the comments a share link shows, a reader's
  comment and follow request, each answering what it came to.
- useJourneyFollow: the confirm and unsubscribe pages a follower's mail
  links open, posting the token once.
- useJourneyShareLink switches allow_comments and allow_subscriptions on
  their own, like the map extras.
2026-11-30 16:56:15 +01:00

78 lines
3.0 KiB
TypeScript

// 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);
});
});