|
1 |
| -import {render, screen} from '@testing-library/react'; |
| 1 | +import {render, screen, waitForElementToBeRemoved} from '@testing-library/react'; |
2 | 2 | import userEvent from '@testing-library/user-event';
|
3 | 3 | import messagesEN from 'i18n/compiled/en.json';
|
4 | 4 | import {IntlProvider} from 'react-intl';
|
5 | 5 | import {RouterProvider, createMemoryRouter} from 'react-router-dom';
|
6 | 6 |
|
7 | 7 | import {ConfigContext, FormContext} from 'Context';
|
8 |
| -import {BASE_URL, buildForm, mockAnalyticsToolConfigGet} from 'api-mocks'; |
| 8 | +import {BASE_URL, buildForm, buildSubmission, mockAnalyticsToolConfigGet} from 'api-mocks'; |
9 | 9 | import mswServer from 'api-mocks/msw-server';
|
10 |
| -import {mockSubmissionPost, mockSubmissionStepGet} from 'api-mocks/submissions'; |
| 10 | +import { |
| 11 | + mockSubmissionCompletePost, |
| 12 | + mockSubmissionGet, |
| 13 | + mockSubmissionPost, |
| 14 | + mockSubmissionProcessingStatusErrorGet, |
| 15 | + mockSubmissionStepGet, |
| 16 | + mockSubmissionSummaryGet, |
| 17 | +} from 'api-mocks/submissions'; |
11 | 18 | import {routes} from 'components/App';
|
| 19 | +import {SUBMISSION_ALLOWED} from 'components/constants'; |
12 | 20 |
|
13 | 21 | window.scrollTo = vi.fn();
|
14 | 22 |
|
| 23 | +beforeAll(() => { |
| 24 | + vi.stubGlobal('jest', { |
| 25 | + advanceTimersByTime: vi.advanceTimersByTime.bind(vi), |
| 26 | + }); |
| 27 | +}); |
| 28 | + |
15 | 29 | beforeEach(() => {
|
16 | 30 | localStorage.clear();
|
17 | 31 | });
|
18 | 32 |
|
19 | 33 | afterEach(() => {
|
| 34 | + if (vi.isFakeTimers()) { |
| 35 | + vi.runOnlyPendingTimers(); |
| 36 | + vi.useRealTimers(); |
| 37 | + } |
20 | 38 | localStorage.clear();
|
21 | 39 | });
|
22 | 40 |
|
23 | 41 | afterAll(() => {
|
| 42 | + vi.unstubAllGlobals(); |
24 | 43 | vi.clearAllMocks();
|
25 | 44 | });
|
26 | 45 |
|
@@ -129,3 +148,46 @@ test('Navigation through form without introduction page', async () => {
|
129 | 148 | const formInput = await screen.findByLabelText('Component 1');
|
130 | 149 | expect(formInput).toBeVisible();
|
131 | 150 | });
|
| 151 | + |
| 152 | +test('Submitting the form with failing background processing', async () => { |
| 153 | + const user = userEvent.setup({ |
| 154 | + advanceTimers: vi.advanceTimersByTime, |
| 155 | + }); |
| 156 | + // The summary page submits the form and needs to trigger the appropriate redirects. |
| 157 | + // When the status check reports failure, we need to be redirected back to the summary |
| 158 | + // page for a retry. |
| 159 | + const form = buildForm({loginRequired: false, submissionStatementsConfiguration: []}); |
| 160 | + const submission = buildSubmission({ |
| 161 | + submissionAllowed: SUBMISSION_ALLOWED.yes, |
| 162 | + payment: { |
| 163 | + isRequired: false, |
| 164 | + amount: undefined, |
| 165 | + hasPaid: false, |
| 166 | + }, |
| 167 | + MARKER: true, |
| 168 | + }); |
| 169 | + mswServer.use( |
| 170 | + mockAnalyticsToolConfigGet(), |
| 171 | + mockSubmissionGet(submission), |
| 172 | + mockSubmissionSummaryGet(), |
| 173 | + mockSubmissionCompletePost(), |
| 174 | + mockSubmissionProcessingStatusErrorGet |
| 175 | + ); |
| 176 | + |
| 177 | + render(<Wrapper form={form} initialEntry={`/overzicht?submission_uuid=${submission.id}`} />); |
| 178 | + |
| 179 | + expect(await screen.findByRole('heading', {name: 'Check and confirm'})).toBeVisible(); |
| 180 | + |
| 181 | + // confirm the submission and complete it |
| 182 | + vi.useFakeTimers(); |
| 183 | + await user.click(screen.getByRole('button', {name: 'Confirm'})); |
| 184 | + expect(await screen.findByRole('heading', {name: 'Processing...'})).toBeVisible(); |
| 185 | + const loader = await screen.findByRole('status'); |
| 186 | + vi.runOnlyPendingTimers(); |
| 187 | + vi.useRealTimers(); |
| 188 | + await waitForElementToBeRemoved(loader); |
| 189 | + |
| 190 | + // due to the error we get redirected back to the summary page. |
| 191 | + expect(await screen.findByRole('heading', {name: 'Check and confirm'})).toBeVisible(); |
| 192 | + expect(screen.getByText('Computer says no.')).toBeVisible(); |
| 193 | +}); |
0 commit comments