Skip to content

Commit

Permalink
✅ [open-formulieren/open-forms#4929] Update form start tests
Browse files Browse the repository at this point in the history
Updated the existing tests for the new component structure and
injection of dependencies, and some tests were moved from Form
to FormStart tests because the behaviour being tested was also moved.

Less mocks are now used and the tests should generally be a bit more
robust.
  • Loading branch information
sergei-maertens committed Jan 10, 2025
1 parent ca56b96 commit c38fe5b
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 176 deletions.
70 changes: 1 addition & 69 deletions src/components/Form.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {render, screen, waitFor, waitForElementToBeRemoved} from '@testing-library/react';
import {render, screen} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import messagesEN from 'i18n/compiled/en.json';
import {IntlProvider} from 'react-intl';
Expand All @@ -10,8 +10,6 @@ import mswServer from 'api-mocks/msw-server';
import {mockSubmissionPost, mockSubmissionStepGet} from 'api-mocks/submissions';
import {routes} from 'components/App';

import {START_FORM_QUERY_PARAM} from './constants';

window.scrollTo = vi.fn();

beforeEach(() => {
Expand Down Expand Up @@ -51,72 +49,6 @@ const Wrapper = ({form = buildForm(), initialEntry = '/startpagina'}) => {
);
};

// TODO: move to/merge with FormStart tests
test('Start form anonymously', async () => {
const user = userEvent.setup();
mswServer.use(mockSubmissionPost(), mockAnalyticsToolConfigGet(), mockSubmissionStepGet());
let startSubmissionRequest;
mswServer.events.on('request:match', async ({request}) => {
const url = new URL(request.url);
if (request.method === 'POST' && url.pathname.endsWith('/api/v2/submissions')) {
startSubmissionRequest = request;
}
});
// form with only anonymous login option
const form = buildForm({loginOptions: [], loginRequired: false});

render(<Wrapper form={form} />);

await waitForElementToBeRemoved(() => screen.getByRole('status'));

const startButton = screen.getByRole('button', {name: 'Begin'});
await user.click(startButton);

expect(startSubmissionRequest).not.toBeUndefined();
const requestBody = await startSubmissionRequest.json();
expect(requestBody.anonymous).toBe(true);
});

// TODO: move to/merge with FormStart tests
test('Start form as if authenticated from the backend', async () => {
mswServer.use(mockAnalyticsToolConfigGet(), mockSubmissionPost(), mockSubmissionStepGet());
let startSubmissionRequest;
mswServer.events.on('request:match', async ({request}) => {
const url = new URL(request.url);
if (request.method === 'POST' && url.pathname.endsWith('/api/v2/submissions')) {
startSubmissionRequest = request;
}
});
render(<Wrapper initialEntry={`/startpagina?${START_FORM_QUERY_PARAM}=1`} />);

await waitFor(() => {
expect(startSubmissionRequest).not.toBeUndefined();
});
const requestBody = await startSubmissionRequest.json();
expect(requestBody.anonymous).toBe(false);
});

// TODO: move to/merge with FormStart tests
test('Start form with object reference query param', async () => {
mswServer.use(mockAnalyticsToolConfigGet(), mockSubmissionPost(), mockSubmissionStepGet());
let startSubmissionRequest;
mswServer.events.on('request:match', async ({request}) => {
const url = new URL(request.url);
if (request.method === 'POST' && url.pathname.endsWith('/api/v2/submissions')) {
startSubmissionRequest = request;
}
});
render(
<Wrapper initialEntry={`/startpagina?${START_FORM_QUERY_PARAM}=1&initial_data_reference=foo`} />
);

await waitFor(() => {
expect(startSubmissionRequest).not.toBeUndefined();
});
const requestBody = await startSubmissionRequest.json();
expect(requestBody.initialDataReference).toBe('foo');
});

// Regression test for https://github.com/open-formulieren/open-forms/issues/4918
test.each([
{
Expand Down
Loading

0 comments on commit c38fe5b

Please sign in to comment.