Admin: Berichten-scherm met formulierschakelaar, lijst, detail en opnieuw mailen (CMS-U6)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-10 22:42:33 +01:00
co-authored by Claude Sonnet 5.5
parent 0d729b6413
commit fd13647139
22 changed files with 1675 additions and 10 deletions
@@ -0,0 +1,418 @@
import { beforeEach, describe, expect, it } from 'vitest';
import { screen, waitFor, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { http, HttpResponse } from 'msw';
import { renderApp, mockAuthenticated } from '@/test/utils';
import { server } from '@/mocks/server';
import { API_BASE } from '@/features/auth/mocks/fixtures';
import {
MSG_NEW,
MSG_READ,
MSG_SPAM,
getMockContactMessages,
resetMockContact,
} from '@/features/contact/mocks/handlers';
import { _resetSetupStatusCache } from '@/router';
const LONG = { timeout: 10000 };
function capabilities(modules: string[], emailEnabled = true) {
server.use(
http.get(`${API_BASE}/api/v1/System/capabilities`, () =>
HttpResponse.json({ modules, emailEnabled, defaultLanguage: 'nl' }),
),
);
}
const WITH_CONTACT = ['Availability', 'Identity', 'Master', 'Offerings', 'Contact'];
beforeEach(() => {
_resetSetupStatusCache();
resetMockContact();
mockAuthenticated();
});
async function openPage() {
capabilities(WITH_CONTACT);
renderApp('/messages');
await screen.findByTestId('messages-title', {}, LONG);
}
describe('Berichten navigation', () => {
it('shows the sidebar entry when the module is loaded and the API answers', async () => {
capabilities(WITH_CONTACT);
renderApp('/dashboard');
expect(await screen.findByTestId('nav-messages', {}, LONG)).toBeInTheDocument();
});
it('hides the entry when the module is not loaded', async () => {
capabilities(['Availability', 'Identity', 'Offerings']);
renderApp('/dashboard');
await screen.findByTestId('nav-offerings', {}, LONG);
expect(screen.queryByTestId('nav-messages')).not.toBeInTheDocument();
});
it('hides the entry when the settings answer 404 (host switch off)', async () => {
capabilities(WITH_CONTACT);
server.use(
http.get(
`${API_BASE}/api/v1/contact-settings`,
() => new HttpResponse(null, { status: 404 }),
),
);
renderApp('/dashboard');
await screen.findByTestId('nav-offerings', {}, LONG);
await new Promise((r) => setTimeout(r, 200));
expect(screen.queryByTestId('nav-messages')).not.toBeInTheDocument();
});
it('shows the not-found page on a direct visit while unavailable', async () => {
capabilities(WITH_CONTACT);
server.use(
http.get(
`${API_BASE}/api/v1/contact-settings`,
() => new HttpResponse(null, { status: 404 }),
),
);
renderApp('/messages');
expect(await screen.findByTestId('not-found-title', {}, LONG)).toBeInTheDocument();
});
it('shows the feature-unavailable message when the module is missing', async () => {
capabilities(['Availability', 'Identity']);
renderApp('/messages');
expect(
await screen.findByTestId('feature-unavailable-message', {}, LONG),
).toBeInTheDocument();
});
});
describe('Form switch', () => {
it('shows the first-time hint while off and switches on and off with the server answer', async () => {
await openPage();
expect(await screen.findByTestId('contact-switch-hint')).toBeInTheDocument();
const toggle = screen.getByTestId('contact-switch-toggle');
expect(toggle).toHaveAttribute('aria-checked', 'false');
await userEvent.click(toggle);
await waitFor(() =>
expect(screen.getByTestId('contact-switch-toggle')).toHaveAttribute(
'aria-checked',
'true',
),
);
expect(screen.getByTestId('contact-switch-state')).toHaveTextContent('The form is on');
expect(screen.queryByTestId('contact-switch-hint')).not.toBeInTheDocument();
await userEvent.click(screen.getByTestId('contact-switch-toggle'));
await waitFor(() =>
expect(screen.getByTestId('contact-switch-toggle')).toHaveAttribute(
'aria-checked',
'false',
),
);
});
it('shows a translated error and keeps the state when the switch fails', async () => {
await openPage();
server.use(
http.put(`${API_BASE}/api/v1/contact-settings`, () =>
HttpResponse.json({ status: 500, title: 'Internal Server Error' }, { status: 500 }),
),
);
await userEvent.click(await screen.findByTestId('contact-switch-toggle'));
const error = await screen.findByTestId('contact-switch-error');
expect(error).toHaveTextContent('Something went wrong');
expect(error).not.toHaveTextContent('Internal Server Error');
expect(screen.getByTestId('contact-switch-toggle')).toHaveAttribute(
'aria-checked',
'false',
);
});
});
describe('Message list', () => {
it('shows the inbox without spam and the unread counter', async () => {
await openPage();
expect(await screen.findByTestId(`contact-row-${MSG_NEW}`)).toBeInTheDocument();
expect(screen.getByTestId(`contact-row-${MSG_READ}`)).toBeInTheDocument();
expect(screen.queryByTestId(`contact-row-${MSG_SPAM}`)).not.toBeInTheDocument();
expect(screen.getByTestId('contact-unread-count')).toHaveTextContent('(1)');
expect(screen.getByTestId('messages-range')).toHaveTextContent('1–2 of 2');
});
it('sends the right status for each chip and shows spam apart', async () => {
await openPage();
await screen.findByTestId(`contact-row-${MSG_NEW}`);
await userEvent.click(screen.getByTestId('contact-filter-spam'));
expect(await screen.findByTestId(`contact-row-${MSG_SPAM}`)).toBeInTheDocument();
expect(screen.queryByTestId(`contact-row-${MSG_NEW}`)).not.toBeInTheDocument();
await userEvent.click(screen.getByTestId('contact-filter-new'));
expect(await screen.findByTestId(`contact-row-${MSG_NEW}`)).toBeInTheDocument();
expect(screen.queryByTestId(`contact-row-${MSG_READ}`)).not.toBeInTheDocument();
await userEvent.click(screen.getByTestId('contact-filter-archived'));
expect(await screen.findByTestId('messages-empty-state')).toHaveTextContent(
'No archived messages',
);
});
it('requests the status query parameter only for a specific chip, 25 per page', async () => {
const urls: string[] = [];
server.use(
http.get(`${API_BASE}/api/v1/contact-messages`, ({ request }) => {
urls.push(new URL(request.url).search);
return HttpResponse.json({
items: [],
page: 1,
pageSize: 25,
totalCount: 0,
unreadCount: 0,
});
}),
);
await openPage();
await screen.findByTestId('messages-empty-state');
await userEvent.click(screen.getByTestId('contact-filter-read'));
await waitFor(() => expect(urls.some((u) => u.includes('status=read'))).toBe(true));
expect(urls[0]).not.toContain('status=');
expect(urls[0]).toContain('pageSize=25');
});
it('pages through more than 25 messages', async () => {
const many = Array.from({ length: 30 }, (_, i) => ({
id: `00000000-0000-0000-0000-${String(i).padStart(12, '0')}`,
receivedAt: '2026-11-09T10:00:00Z',
name: `Naam ${i}`,
email: 'x@example.nl',
subject: 's',
status: 'read',
mailStatus: 'sent',
}));
server.use(
http.get(`${API_BASE}/api/v1/contact-messages`, ({ request }) => {
const page = Number(new URL(request.url).searchParams.get('page'));
return HttpResponse.json({
items: many.slice((page - 1) * 25, page * 25),
page,
pageSize: 25,
totalCount: 30,
unreadCount: 0,
});
}),
);
await openPage();
expect(await screen.findByTestId('messages-range')).toHaveTextContent('1–25 of 30');
expect(screen.getByTestId('messages-prev')).toBeDisabled();
await userEvent.click(screen.getByTestId('messages-next'));
await waitFor(() =>
expect(screen.getByTestId('messages-range')).toHaveTextContent('26–30 of 30'),
);
expect(screen.getByTestId('messages-next')).toBeDisabled();
});
it('shows an empty state for the inbox', async () => {
server.use(
http.get(`${API_BASE}/api/v1/contact-messages`, () =>
HttpResponse.json({
items: [],
page: 1,
pageSize: 25,
totalCount: 0,
unreadCount: 0,
}),
),
);
await openPage();
expect(await screen.findByTestId('messages-empty-state')).toHaveTextContent(
'No messages yet',
);
});
it('gives the mail icons a text alternative', async () => {
await openPage();
await screen.findByTestId(`contact-row-${MSG_NEW}`);
expect(
within(screen.getByTestId(`contact-row-${MSG_NEW}`)).getByRole('img'),
).toHaveAccessibleName('Mail sent');
expect(
within(screen.getByTestId(`contact-row-${MSG_READ}`)).getByRole('img'),
).toHaveAccessibleName('Mail failed');
});
});
describe('Message detail', () => {
it('opens, keeps line breaks, links the address and marks an unread message as read', async () => {
await openPage();
await userEvent.click(await screen.findByTestId(`contact-open-${MSG_NEW}`));
const body = await screen.findByTestId('contact-message-body');
expect(body.textContent).toBe('Eerste regel\nTweede regel');
expect(body.className).toContain('whitespace-pre-wrap');
expect(screen.getByRole('link', { name: 'jan@example.nl' })).toHaveAttribute(
'href',
'mailto:jan@example.nl',
);
await waitFor(() =>
expect(getMockContactMessages().find((m) => m.id === MSG_NEW)?.status).toBe('read'),
);
await waitFor(() =>
expect(screen.getByTestId('contact-unread-count')).toHaveTextContent('(0)'),
);
});
it('does not change an already read message when it is opened', async () => {
await openPage();
await userEvent.click(await screen.findByTestId(`contact-open-${MSG_READ}`));
await screen.findByTestId('contact-message-body');
expect(getMockContactMessages().find((m) => m.id === MSG_READ)?.status).toBe('read');
});
it('changes the status through the select', async () => {
await openPage();
await userEvent.click(await screen.findByTestId(`contact-open-${MSG_READ}`));
await userEvent.selectOptions(
await screen.findByTestId('contact-status-select'),
'archived',
);
await waitFor(() =>
expect(getMockContactMessages().find((m) => m.id === MSG_READ)?.status).toBe(
'archived',
),
);
});
it('shows the translated mail error and the next attempt, never the raw code', async () => {
await openPage();
await userEvent.click(await screen.findByTestId(`contact-open-${MSG_READ}`));
expect(await screen.findByTestId('contact-mail-error')).toHaveTextContent(
'Logging in to the mail server failed.',
);
expect(screen.getByTestId('contact-mail-error')).not.toHaveTextContent('auth_failed');
expect(screen.getByTestId('contact-next-attempt')).toBeInTheDocument();
});
it('moves a spam message to the inbox with "Geen spam"', async () => {
await openPage();
await userEvent.click(await screen.findByTestId('contact-filter-spam'));
await userEvent.click(await screen.findByTestId(`contact-open-${MSG_SPAM}`));
await userEvent.click(await screen.findByTestId('contact-not-spam'));
await waitFor(() =>
expect(getMockContactMessages().find((m) => m.id === MSG_SPAM)?.status).toBe('read'),
);
await waitFor(() =>
expect(screen.queryByTestId(`contact-row-${MSG_SPAM}`)).not.toBeInTheDocument(),
);
await userEvent.click(screen.getByTestId('contact-filter-inbox'));
expect(await screen.findByTestId(`contact-row-${MSG_SPAM}`)).toBeInTheDocument();
});
it('does not offer resend on a spam message', async () => {
await openPage();
await userEvent.click(await screen.findByTestId('contact-filter-spam'));
await userEvent.click(await screen.findByTestId(`contact-open-${MSG_SPAM}`));
await screen.findByTestId('contact-not-spam');
expect(screen.queryByTestId('contact-resend-button')).not.toBeInTheDocument();
});
});
describe('Delete', () => {
it('deletes after confirmation', async () => {
await openPage();
await userEvent.click(await screen.findByTestId(`contact-open-${MSG_READ}`));
await userEvent.click(await screen.findByTestId('contact-delete-button'));
await userEvent.click(await screen.findByTestId('delete-message-confirm'));
await waitFor(() =>
expect(screen.queryByTestId(`contact-row-${MSG_READ}`)).not.toBeInTheDocument(),
);
expect(getMockContactMessages().some((m) => m.id === MSG_READ)).toBe(false);
});
it('keeps the message when the confirmation is cancelled', async () => {
await openPage();
await userEvent.click(await screen.findByTestId(`contact-open-${MSG_READ}`));
await userEvent.click(await screen.findByTestId('contact-delete-button'));
await userEvent.click(await screen.findByTestId('delete-message-cancel'));
expect(getMockContactMessages().some((m) => m.id === MSG_READ)).toBe(true);
});
});
describe('Resend', () => {
async function openRead() {
await openPage();
await userEvent.click(await screen.findByTestId(`contact-open-${MSG_READ}`));
return screen.findByTestId('contact-resend-button');
}
it('says "Verstuurd" when the mail went out', async () => {
await userEvent.click(await openRead());
expect(await screen.findByTestId('contact-resend-note')).toHaveTextContent('Sent.');
});
it('says "Niet verstuurd" when the server could not send', async () => {
const button = await openRead();
server.use(
http.post(`${API_BASE}/api/v1/contact-messages/:id/resend`, () =>
HttpResponse.json({
mailStatus: 'pending',
mailAttempts: 0,
mailed: false,
mailError: 'disabled',
}),
),
);
await userEvent.click(button);
expect(await screen.findByTestId('contact-resend-note')).toHaveTextContent(
'Not sent (mail is off).',
);
});
it('shows the retry delay from Retry-After on 429', async () => {
const button = await openRead();
server.use(
http.post(`${API_BASE}/api/v1/contact-messages/:id/resend`, () =>
HttpResponse.json(
{ status: 429, title: 'Too Many Requests' },
{ status: 429, headers: { 'Retry-After': '42' } },
),
),
);
await userEvent.click(button);
const note = await screen.findByTestId('contact-resend-note');
expect(note).toHaveTextContent('Try again in 42 seconds.');
expect(note).not.toHaveTextContent('Too Many Requests');
});
it('explains a 409 on a spam message', async () => {
const button = await openRead();
server.use(
http.post(`${API_BASE}/api/v1/contact-messages/:id/resend`, () =>
HttpResponse.json({ status: 409, title: 'Conflict' }, { status: 409 }),
),
);
await userEvent.click(button);
expect(await screen.findByTestId('contact-resend-note')).toHaveTextContent(
'Mark the message as not spam first.',
);
});
it('disables resend with a hint when e-mail is off for the site', async () => {
capabilities(WITH_CONTACT, false);
renderApp('/messages');
await userEvent.click(await screen.findByTestId(`contact-open-${MSG_READ}`, {}, LONG));
expect(await screen.findByTestId('contact-resend-button')).toBeDisabled();
expect(screen.getByTestId('contact-resend-hint')).toBeInTheDocument();
});
});