Files
slp-modular-cms/frontend/src/features/settings/components/MailSettingsCard.test.tsx
T

112 lines
4.4 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import { screen } 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, makeAuthResponse, mockUser } from '@/features/auth/mocks/fixtures';
import { _resetSetupStatusCache } from '@/router';
import type { MailSettings } from '@/features/settings/services/types';
beforeEach(() => {
_resetSetupStatusCache();
});
const baseSettings: MailSettings = {
emailEnabled: true,
fromAddress: 'no-reply@slpsoftware.nl',
fromName: 'Andrea Slop Fotografie',
defaultFromAddress: 'no-reply@slpsoftware.nl',
defaultFromName: 'Andrea Slop Fotografie',
allowedFromAddresses: ['no-reply@slpsoftware.nl', 'info@andreaslop.nl'],
isCustomized: false,
};
function mailEnabled(settings: MailSettings = baseSettings, put?: (body: unknown) => void) {
server.use(
http.get('*/System/capabilities', () =>
HttpResponse.json({ modules: ['Availability', 'Identity'], emailEnabled: true, defaultLanguage: 'nl' }),
),
http.get(`${API_BASE}/api/v1/SiteSettings/mail`, () => HttpResponse.json(settings)),
http.put(`${API_BASE}/api/v1/SiteSettings/mail`, async ({ request }) => {
const body = await request.json();
put?.(body);
return HttpResponse.json({ ...settings, isCustomized: true });
}),
);
}
function asAdministrator() {
server.use(
http.post(`${API_BASE}/api/v1/auth/refresh`, () =>
HttpResponse.json(makeAuthResponse({ user: { ...mockUser, role: 'Administrator', name: 'Test Admin' } })),
),
);
}
describe('MailSettingsCard', () => {
it('is not shown when the site cannot send mail', async () => {
mockAuthenticated();
renderApp('/settings');
await screen.findByTestId('settings-title', {}, { timeout: 10000 });
expect(screen.queryByTestId('mail-settings-card')).not.toBeInTheDocument();
});
it('lets the Owner pick an allowed sender and name, and saves them', async () => {
mockAuthenticated();
let saved: unknown;
mailEnabled(baseSettings, (b) => (saved = b));
renderApp('/settings');
const select = await screen.findByTestId('mail-from-address', {}, { timeout: 10000 });
expect(Array.from((select as HTMLSelectElement).options).map((o) => o.value)).toEqual([
'no-reply@slpsoftware.nl',
'info@andreaslop.nl',
]);
await userEvent.selectOptions(select, 'info@andreaslop.nl');
const name = screen.getByTestId('mail-from-name');
await userEvent.clear(name);
await userEvent.type(name, 'Andrea');
await userEvent.click(screen.getByTestId('mail-save-button'));
await vi.waitFor(() => expect(saved).toEqual({ fromAddress: 'info@andreaslop.nl', fromName: 'Andrea' }));
});
it('disables save until something changes, and blocks an empty or too long name', async () => {
mockAuthenticated();
mailEnabled();
renderApp('/settings');
const save = await screen.findByTestId('mail-save-button', {}, { timeout: 10000 });
expect(save).toBeDisabled();
const name = screen.getByTestId('mail-from-name');
await userEvent.clear(name);
expect(save).toBeDisabled();
await userEvent.type(name, 'x'.repeat(81));
expect(screen.getByTestId('mail-from-name-error')).toBeInTheDocument();
expect(save).toBeDisabled();
});
it('offers to restore the defaults only once the sender was customised', async () => {
mockAuthenticated();
mailEnabled({ ...baseSettings, isCustomized: true });
renderApp('/settings');
expect(await screen.findByTestId('mail-reset-button', {}, { timeout: 10000 })).toBeEnabled();
});
it('shows an Administrator the settings read-only', async () => {
asAdministrator();
mailEnabled();
renderApp('/settings');
expect(await screen.findByTestId('mail-from-address', {}, { timeout: 10000 })).toBeDisabled();
expect(screen.getByTestId('mail-from-name')).toBeDisabled();
expect(screen.getByTestId('mail-readonly-note')).toBeInTheDocument();
expect(screen.queryByTestId('mail-save-button')).not.toBeInTheDocument();
});
});