Skip to content

Commit 3f58b10

Browse files
committed
fix unit test
1 parent 097bf52 commit 3f58b10

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

common/constants/testIDs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ export const MODAL_CONTENT = 'MODAL_CONTENT';
4343
export const MODAL_OVERLAY = 'MODAL_OVERLAY';
4444
export const LOGIN_FORM = 'LOGIN_FORM';
4545
export const SUCCESS_PAGE_MESSAGE = 'SUCCESS_PAGE_MESSAGE';
46+
export const REGISTRATION_FORM_INITIAL_SUBMIT_BUTTON = 'REGISTRATION_FORM_INITIAL_SUBMIT_BUTTON';

components/Forms/RegistrationForm/RegistrationForm.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import OutboundLink from 'components/OutboundLink/OutboundLink';
1414
import type { AxiosError } from 'axios';
1515
import axios from 'axios';
1616
import { InlineLoadingSpinner } from 'components/InlineLoadingSpinner';
17+
import { REGISTRATION_FORM_INITIAL_SUBMIT_BUTTON } from 'common/constants/testIDs';
1718

1819
export interface RegistrationFormValues {
1920
email: string;
@@ -222,7 +223,12 @@ export function RegistrationForm({
222223
{errorMessage && <Alert type="error">{errorMessage}</Alert>}
223224

224225
<div className="max-w-md px-3 mt-5">
225-
<Button type="submit" theme="secondary" disabled={isSubmitting}>
226+
<Button
227+
type="submit"
228+
theme="secondary"
229+
disabled={isSubmitting}
230+
data-testid={REGISTRATION_FORM_INITIAL_SUBMIT_BUTTON}
231+
>
226232
<span className="flex items-center justify-center gap-x-2">
227233
{isSubmitting && <InlineLoadingSpinner />}
228234
<span className="mt-[0.325rem]">Submit ✓</span>

components/Forms/RegistrationForm/__tests__/RegistrationForm.test.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { mockUser } from 'test-utils/mockGenerators/mockUser';
55
import { describe, expect, it } from 'vitest';
66
import MockAdapter from 'axios-mock-adapter';
77
import axios from 'axios';
8+
import { REGISTRATION_FORM_INITIAL_SUBMIT_BUTTON } from 'common/constants/testIDs';
89
import { RegistrationForm } from '../RegistrationForm';
910

1011
const axiosMock = new MockAdapter(axios);
@@ -22,11 +23,11 @@ describe('RegistrationForm', () => {
2223
const successSpy = vi.fn();
2324
const submitSpy = vi.fn();
2425

25-
const { getByText } = render(
26+
const { getByTestId } = render(
2627
<RegistrationForm onSubmit={submitSpy} onSuccess={successSpy} initialValues={user} />,
2728
);
2829

29-
fireEvent.click(getByText('Submit'));
30+
fireEvent.click(getByTestId(REGISTRATION_FORM_INITIAL_SUBMIT_BUTTON));
3031

3132
await waitFor(() => {
3233
expect(submitSpy).toHaveBeenCalledTimes(1);
@@ -40,13 +41,13 @@ describe('RegistrationForm', () => {
4041
axiosMock.onPost('/api/registration/new', user).reply(200);
4142

4243
const successSpy = vi.fn(() => Promise.resolve(true));
43-
const { container, getByText, findByText } = render(
44+
const { container, getByTestId, findByTestId } = render(
4445
<RegistrationForm onSuccess={successSpy} initialValues={user} />,
4546
);
4647

47-
fireEvent.click(getByText('Submit'));
48+
fireEvent.click(getByTestId(REGISTRATION_FORM_INITIAL_SUBMIT_BUTTON));
4849

49-
const submit = await findByText('Submit');
50+
const submit = await findByTestId(REGISTRATION_FORM_INITIAL_SUBMIT_BUTTON);
5051
expect(submit).not.toBeDisabled();
5152
container.querySelectorAll('input').forEach(input => {
5253
expect(input.textContent).toBeFalsy();
@@ -64,7 +65,7 @@ describe('RegistrationForm', () => {
6465

6566
// No Alert on mount
6667
expect(screen.queryByRole('alert')).not.toBeInTheDocument();
67-
fireEvent.click(screen.getByText('Submit'));
68+
fireEvent.click(screen.getByTestId(REGISTRATION_FORM_INITIAL_SUBMIT_BUTTON));
6869

6970
// Alert shown after submitting
7071
await waitFor(() => expect(submitSpy).toHaveBeenCalledTimes(1));

components/Forms/RegistrationForm/__tests__/__snapshots__/RegistrationForm.test.tsx.snap

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,21 @@ exports[`RegistrationForm > should render with required props 1`] = `
327327
>
328328
<button
329329
className="Button secondary"
330-
data-testid="BUTTON"
330+
data-testid="REGISTRATION_FORM_INITIAL_SUBMIT_BUTTON"
331331
disabled={false}
332332
onClick={[Function]}
333333
tabIndex={0}
334334
type="submit"
335335
>
336-
Submit
336+
<span
337+
className="flex items-center justify-center gap-x-2"
338+
>
339+
<span
340+
className="mt-[0.325rem]"
341+
>
342+
Submit ✓
343+
</span>
344+
</span>
337345
</button>
338346
<p
339347
className="pt-5 text-xs"

0 commit comments

Comments
 (0)