PPPAIMT-Crest/frontend/src/pages/AgreementPage.test.jsx

122 lines
3.9 KiB
JavaScript

import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { MemoryRouter } from 'react-router-dom';
import AgreementPage from './AgreementPage.jsx';
describe('agreement page', () => {
beforeEach(() => {
sessionStorage.setItem('resumeToken', 'token');
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
email: 'physician@example.com',
agreement_date: null,
full_name: '',
age: null,
accepted: true,
}),
}),
);
});
afterEach(() => {
cleanup();
vi.unstubAllGlobals();
sessionStorage.clear();
});
it('shows the form only after the agreement is accepted', async () => {
sessionStorage.removeItem('resumeToken');
render(
<MemoryRouter>
<AgreementPage />
</MemoryRouter>,
);
expect(await screen.findByLabelText('Consultation agreement')).toBeInTheDocument();
expect(screen.getByRole('listitem', { current: 'step' })).toHaveTextContent('Read agreement');
expect(screen.queryByLabelText('Email')).not.toBeInTheDocument();
fireEvent.click(screen.getByRole('checkbox', { name: /read and accept the agreement terms/i }));
expect(screen.queryByLabelText('Consultation agreement')).not.toBeInTheDocument();
expect(screen.getByRole('listitem', { current: 'step' })).toHaveTextContent('Your details');
expect(screen.getByLabelText('Email')).toBeInTheDocument();
expect(screen.getByLabelText('Full name')).toBeInTheDocument();
expect(screen.queryByRole('button', { name: 'Save draft' })).not.toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Continue to review' })).toBeInTheDocument();
});
it('uppercases PAN as it is typed', async () => {
render(
<MemoryRouter>
<AgreementPage />
</MemoryRouter>,
);
const pan = await screen.findByLabelText('PAN');
fireEvent.change(pan, { target: { value: 'abcde1234f' } });
await waitFor(() => {
expect(pan).toHaveValue('ABCDE1234F');
});
});
it('shows registration conflicts under the registration field', async () => {
const fetchMock = vi.fn(async (path, options = {}) => {
if (
path === '/api/applications/me' &&
(!options.method || options.method === 'GET')
) {
return {
ok: true,
status: 200,
json: async () => ({
email: 'physician@example.com',
agreement_date: '2026-10-17',
full_name: 'Asha Rao',
age: 42,
address: 'Pune',
registration_no: 'MMC-1',
medical_council: 'Maharashtra Medical Council',
pan: 'ABCDE1234F',
bank_name: 'SBI',
account_holder: 'Asha Rao',
account_number: '123456789012',
ifsc: 'SBIN0001234',
accepted: true,
}),
};
}
if (String(path).startsWith('/api/applications/me') && options.method === 'PUT') {
return {
ok: false,
status: 409,
json: async () => ({
error: { message: 'This registration number is already in use' },
}),
};
}
return { ok: false, status: 404, json: async () => ({}) };
});
vi.stubGlobal('fetch', fetchMock);
render(
<MemoryRouter>
<AgreementPage />
</MemoryRouter>,
);
const registration = await screen.findByLabelText('Registration number');
fireEvent.change(registration, { target: { value: 'MMC-999' } });
fireEvent.click(screen.getAllByRole('button', { name: 'Continue to review' })[0]);
const message = await screen.findByText('* This registration number is already in use');
expect(message).toBeInTheDocument();
expect(message.className).toContain('field-error');
});
});