Files
slp-modular-cms/frontend/src/features/contact/pages/MessagesPage.test.tsx
T

779 lines
33 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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);
}
async function openSettings() {
await userEvent.click(await screen.findByTestId('contact-tab-settings'));
await screen.findByTestId('contact-panel-settings');
}
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();
await openSettings();
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('is a real switch: named, stateful, 44px, with the state as text, usable by keyboard', async () => {
await openPage();
const toggle = await screen.findByRole('switch', { name: 'Contact form on/off' });
expect(toggle).toHaveAttribute('aria-checked', 'false');
expect(toggle).toHaveTextContent('Off');
expect(toggle.className).toContain('min-h-11');
expect(toggle.className).toContain('min-w-11');
toggle.focus();
await userEvent.keyboard(' ');
await waitFor(() => expect(toggle).toHaveAttribute('aria-checked', 'true'));
expect(toggle).toHaveTextContent('On');
await userEvent.keyboard('{Enter}');
await waitFor(() => expect(toggle).toHaveAttribute('aria-checked', 'false'));
});
it('is disabled while saving', async () => {
await openPage();
server.use(
http.put(`${API_BASE}/api/v1/contact-settings`, async () => {
await new Promise((r) => setTimeout(r, 300));
return HttpResponse.json({ enabled: true, hostEnabled: true });
}),
);
const toggle = await screen.findByTestId('contact-switch-toggle');
await userEvent.click(toggle);
expect(toggle).toBeDisabled();
await waitFor(() => expect(toggle).not.toBeDisabled());
});
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('Recipient address', () => {
it('shows the field, says that the server setting applies, and never an address it was not given', async () => {
await openPage();
await openSettings();
expect(await screen.findByTestId('contact-recipient-card')).toBeInTheDocument();
expect(screen.getByTestId('contact-recipient-input')).toHaveValue('');
expect(screen.getByTestId('contact-recipient-source')).toHaveTextContent(
'server setting applies',
);
expect(screen.queryByTestId('contact-recipient-reset')).not.toBeInTheDocument();
expect(screen.queryByTestId('contact-no-recipient-banner')).not.toBeInTheDocument();
});
it('saves a valid address, shows where it comes from and offers to go back', async () => {
await openPage();
await openSettings();
const input = await screen.findByTestId('contact-recipient-input');
await userEvent.type(input, ' inbox@example.org ');
await userEvent.click(screen.getByTestId('contact-recipient-save'));
await waitFor(() =>
expect(screen.getByTestId('contact-recipient-source')).toHaveTextContent(
'Messages go to this address',
),
);
expect(screen.getByTestId('contact-recipient-input')).toHaveValue('inbox@example.org');
expect(screen.getByTestId('contact-recipient-reset')).toBeInTheDocument();
});
it('does not save an invalid address (client side), and says so', async () => {
await openPage();
await openSettings();
const input = await screen.findByTestId('contact-recipient-input');
await userEvent.type(input, 'not-an-address');
await userEvent.click(screen.getByTestId('contact-recipient-save'));
expect(await screen.findByTestId('contact-recipient-invalid')).toHaveTextContent(
'valid e-mail address',
);
});
it('shows a translated message when the server refuses the address', async () => {
await openPage();
await openSettings();
server.use(
http.put(`${API_BASE}/api/v1/contact-settings/recipient`, () =>
HttpResponse.json(
{
status: 400,
title: 'Validation failed',
errors: { recipientEmail: ['invalid'] },
},
{ status: 400 },
),
),
);
await userEvent.type(
await screen.findByTestId('contact-recipient-input'),
'jan@example.org',
);
await userEvent.click(screen.getByTestId('contact-recipient-save'));
const error = await screen.findByTestId('contact-recipient-error');
expect(error).toHaveTextContent('valid e-mail address');
expect(error).not.toHaveTextContent('Validation failed');
});
it('goes back to the server setting when cleared', async () => {
await openPage();
await openSettings();
await userEvent.type(
await screen.findByTestId('contact-recipient-input'),
'inbox@example.org',
);
await userEvent.click(screen.getByTestId('contact-recipient-save'));
await userEvent.click(await screen.findByTestId('contact-recipient-reset'));
await waitFor(() =>
expect(screen.getByTestId('contact-recipient-source')).toHaveTextContent(
'server setting applies',
),
);
expect(screen.getByTestId('contact-recipient-input')).toHaveValue('');
});
it('warns (but does not block the switch) while there is no address at all', async () => {
server.use(
http.get(`${API_BASE}/api/v1/contact-settings`, () =>
HttpResponse.json({
enabled: false,
hostEnabled: true,
recipientEmail: null,
recipientSource: 'none',
}),
),
);
await openPage();
await openSettings();
expect(await screen.findByTestId('contact-no-recipient-banner')).toHaveTextContent(
'No recipient address is set',
);
expect(screen.getByTestId('contact-recipient-source')).toHaveTextContent(
'no recipient address yet',
);
expect(screen.getByTestId('contact-switch-toggle')).not.toBeDisabled();
});
});
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('shows a subject label with special characters as plain text', async () => {
const label = `Fotoshoot & <b>"bruiloft"</b> 'zomer'`;
server.use(
http.get(`${API_BASE}/api/v1/contact-messages`, () =>
HttpResponse.json({
items: [
{
id: MSG_NEW,
receivedAt: '2026-11-09T10:00:00Z',
name: 'Jan',
email: 'jan@example.nl',
subject: label,
status: 'read',
mailStatus: 'sent',
},
],
page: 1,
pageSize: 25,
totalCount: 1,
unreadCount: 0,
}),
),
);
await openPage();
const row = await screen.findByTestId(`contact-row-${MSG_NEW}`);
expect(row).toHaveTextContent(label);
expect(row.querySelector('b')).toBeNull();
});
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}`)).getByTestId('mail-status-sent'),
).toHaveAccessibleName('Mail sent');
expect(
within(screen.getByTestId(`contact-row-${MSG_READ}`)).getByTestId('mail-status-failed'),
).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('has no status select; the status is an icon with its name as text', async () => {
await openPage();
await userEvent.click(await screen.findByTestId(`contact-open-${MSG_READ}`));
const dialog = await screen.findByTestId('contact-message-dialog');
expect(within(dialog).queryByTestId('contact-status-select')).not.toBeInTheDocument();
expect(within(dialog).queryByRole('combobox')).not.toBeInTheDocument();
const icon = within(dialog).getByTestId('status-icon-read');
expect(icon).toHaveTextContent('Read');
expect(icon).toHaveAttribute('title', 'Read');
expect(icon.querySelector('svg')).toHaveAttribute('aria-hidden', 'true');
});
it('archives a message and restores it to read', async () => {
await openPage();
await userEvent.click(await screen.findByTestId(`contact-open-${MSG_READ}`));
await userEvent.click(await screen.findByTestId('contact-archive-button'));
await waitFor(() =>
expect(getMockContactMessages().find((m) => m.id === MSG_READ)?.status).toBe(
'archived',
),
);
expect(
await within(screen.getByTestId('contact-message-dialog')).findByTestId(
'status-icon-archived',
),
).toHaveTextContent('Archived');
expect(screen.queryByTestId('contact-archive-button')).not.toBeInTheDocument();
await userEvent.click(await screen.findByTestId('contact-restore-button'));
await waitFor(() =>
expect(getMockContactMessages().find((m) => m.id === MSG_READ)?.status).toBe('read'),
);
expect(await screen.findByTestId('contact-archive-button')).toBeInTheDocument();
});
it('shows the same status icon and name in the list', async () => {
await openPage();
const readRow = await screen.findByTestId(`contact-row-${MSG_READ}`);
const newRow = screen.getByTestId(`contact-row-${MSG_NEW}`);
expect(within(readRow).getByTestId('status-icon-read')).toHaveTextContent('Read');
expect(within(newRow).getByTestId('status-icon-new')).toHaveTextContent('New');
await userEvent.click(screen.getByTestId('contact-filter-spam'));
expect(await screen.findByTestId('status-icon-spam')).toHaveTextContent('Spam');
});
it('shows the subject exactly as the site sent it, capitals included', async () => {
await openPage();
const row = await screen.findByTestId(`contact-row-${MSG_NEW}`);
expect(row).toHaveTextContent('Website');
expect(row).not.toHaveTextContent('website'); // case-sensitive: no lower-case slug
});
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();
});
});
describe('Unread indication and status placement', () => {
it('marks only unread messages with a dot that has an accessible name', async () => {
await openPage();
await screen.findByTestId(`contact-row-${MSG_NEW}`);
const dot = screen.getByTestId(`contact-unread-dot-${MSG_NEW}`);
expect(dot).toHaveAccessibleName('Unread');
expect(screen.queryByTestId(`contact-unread-dot-${MSG_READ}`)).not.toBeInTheDocument();
});
it('shows the status above the message text in the detail', async () => {
await openPage();
await userEvent.click(await screen.findByTestId(`contact-open-${MSG_READ}`));
const body = await screen.findByTestId('contact-message-body');
const status = await within(screen.getByTestId('contact-message-dialog')).findByTestId(
'status-icon-read',
);
expect(
status.compareDocumentPosition(body) & Node.DOCUMENT_POSITION_FOLLOWING,
).toBeTruthy();
});
it('marks a read message as unread again and does not re-mark it as read', async () => {
await openPage();
await userEvent.click(await screen.findByTestId(`contact-open-${MSG_READ}`));
await userEvent.click(await screen.findByTestId('contact-mark-unread-button'));
await waitFor(() =>
expect(getMockContactMessages().find((m) => m.id === MSG_READ)?.status).toBe('new'),
);
await waitFor(() =>
expect(screen.getByTestId('contact-unread-count')).toHaveTextContent('(2)'),
);
expect(await screen.findByTestId(`contact-unread-dot-${MSG_READ}`)).toBeInTheDocument();
});
});
describe('Dashboard tile', () => {
it('shows the number of unread messages and links to the list', async () => {
capabilities(WITH_CONTACT);
renderApp('/dashboard');
const tile = await screen.findByTestId('messages-tile', {}, LONG);
await waitFor(() =>
expect(screen.getByTestId('messages-tile-count')).toHaveTextContent('1'),
);
expect(tile).toHaveAttribute('href', '/messages');
// Next to the system status tile in the same grid, not full width.
expect(tile.parentElement).toBe(screen.getByTestId('dashboard-tiles'));
expect(screen.getByTestId('availability-badge').closest('[data-testid="dashboard-tiles"]')).not.toBeNull();
expect(tile).toHaveTextContent('unread message');
});
it('is not shown when the module is not loaded', async () => {
capabilities(['Availability', 'Identity', 'Offerings']);
renderApp('/dashboard');
await screen.findByTestId('dashboard-title', {}, LONG);
await new Promise((r) => setTimeout(r, 200));
expect(screen.queryByTestId('messages-tile')).not.toBeInTheDocument();
});
it('is not shown while the host switch is off (404)', async () => {
capabilities(WITH_CONTACT);
server.use(
http.get(
`${API_BASE}/api/v1/contact-settings`,
() => new HttpResponse(null, { status: 404 }),
),
);
renderApp('/dashboard');
await screen.findByTestId('dashboard-title', {}, LONG);
await new Promise((r) => setTimeout(r, 200));
expect(screen.queryByTestId('messages-tile')).not.toBeInTheDocument();
});
});
describe('Clickable row', () => {
it('opens the message when any cell of the row is clicked', async () => {
await openPage();
const row = await screen.findByTestId(`contact-row-${MSG_READ}`);
await userEvent.click(within(row).getByText('Onderhoudspakket'));
expect(await screen.findByTestId('contact-message-dialog')).toBeInTheDocument();
expect(await screen.findByTestId('contact-message-body')).toHaveTextContent('Hallo');
});
});
describe('Tabs and header switch', () => {
it('opens on the messages tab with the switch level with the title and no settings blocks', async () => {
await openPage();
expect(await screen.findByRole('tab', { name: 'Messages', selected: true })).toBeInTheDocument();
expect(screen.getByRole('tabpanel')).toHaveAttribute('id', 'contact-panel-messages');
expect(await screen.findByTestId(`contact-row-${MSG_NEW}`)).toBeInTheDocument();
expect(screen.queryByTestId('contact-recipient-card')).not.toBeInTheDocument();
expect(screen.queryByTestId('contact-switch-card')).not.toBeInTheDocument();
const header = screen.getByTestId('messages-title').parentElement as HTMLElement;
const toggle = within(header).getByRole('switch', { name: 'Contact form on/off' });
expect(toggle).toHaveAttribute('title', 'Contact form on/off');
expect(toggle).toHaveTextContent('Off');
});
it('moves the explanation, hints and recipient to the Settings tab', async () => {
await openPage();
await openSettings();
expect(screen.getByRole('tab', { name: 'Settings', selected: true })).toBeInTheDocument();
const card = await screen.findByTestId('contact-switch-card');
expect(card).toHaveTextContent('While the form is off');
expect(screen.getByTestId('contact-switch-state')).toBeInTheDocument();
expect(screen.getByTestId('contact-recipient-card')).toBeInTheDocument();
expect(screen.queryByTestId('messages-range')).not.toBeInTheDocument();
// the switch stays in the header
expect(screen.getAllByRole('switch')).toHaveLength(1);
await userEvent.click(screen.getByTestId('contact-tab-messages'));
expect(await screen.findByTestId(`contact-row-${MSG_NEW}`)).toBeInTheDocument();
});
it('opens directly on the Settings tab from the URL', async () => {
capabilities(WITH_CONTACT);
renderApp('/messages?tab=settings');
expect(await screen.findByTestId('contact-recipient-card', {}, LONG)).toBeInTheDocument();
expect(screen.getByRole('tab', { name: 'Settings', selected: true })).toBeInTheDocument();
});
it('supports the arrow keys, Home and End on the tabs', async () => {
await openPage();
const messagesTab = await screen.findByRole('tab', { name: 'Messages' });
messagesTab.focus();
await userEvent.keyboard('{ArrowRight}');
expect(await screen.findByRole('tab', { name: 'Settings', selected: true })).toHaveFocus();
expect(screen.getByRole('tabpanel')).toHaveAttribute('aria-labelledby', 'contact-tab-settings');
await userEvent.keyboard('{ArrowRight}');
expect(await screen.findByRole('tab', { name: 'Messages', selected: true })).toHaveFocus();
await userEvent.keyboard('{End}');
expect(await screen.findByRole('tab', { name: 'Settings', selected: true })).toHaveFocus();
await userEvent.keyboard('{Home}');
expect(await screen.findByRole('tab', { name: 'Messages', selected: true })).toHaveFocus();
expect(screen.getByRole('tab', { name: 'Settings' })).toHaveAttribute('tabindex', '-1');
});
});