Skip to content

Commit

Permalink
fixed failing tests, added tests for rtv-us-state-information
Browse files Browse the repository at this point in the history
  • Loading branch information
dvorakjt committed Sep 21, 2024
1 parent 6d990a3 commit 27dffea
Show file tree
Hide file tree
Showing 7 changed files with 331 additions and 336 deletions.
17 changes: 8 additions & 9 deletions src/__tests__/unit/app/register/addresses/addresses.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen, cleanup } from '@testing-library/react';
import { render, screen, cleanup, act } from '@testing-library/react';
import userEvent, { type UserEvent } from '@testing-library/user-event';
import { clearAllPersistentFormElements } from 'fully-formed';
import { Addresses } from '@/app/register/addresses/addresses';
Expand Down Expand Up @@ -84,12 +84,11 @@ describe('Addresses', () => {
clearAllPersistentFormElements();
});

it(`prefetches the next page when the state or zip fields of the home address
form are updated while both valid.`, async () => {
const zip = document.getElementById(homeAddressForm.fields.zip.id)!;
await user.type(zip, '94043');
expect(router.prefetch).toHaveBeenCalledWith(
VoterRegistrationPathnames.OTHER_DETAILS + '?state=CA&zip=94043',
it(`prefetches the next page when the state field of the home address form is
updated and is valid.`, async () => {
act(() => homeAddressForm.fields.state.setValue('CA'));
expect(router.prefetch).toHaveBeenLastCalledWith(
VoterRegistrationPathnames.OTHER_DETAILS + '?state=CA',
);
});

Expand Down Expand Up @@ -358,7 +357,7 @@ describe('Addresses', () => {
await user.click(continueAnyway);

expect(router.push).toHaveBeenCalledWith(
VoterRegistrationPathnames.OTHER_DETAILS + '?state=CO&zip=80301',
VoterRegistrationPathnames.OTHER_DETAILS + '?state=CO',
);
validateAddressesSpy.mockRestore();
});
Expand Down Expand Up @@ -421,7 +420,7 @@ describe('Addresses', () => {
const submitBtn = screen.getByText('Next');
await user.click(submitBtn);
expect(router.push).toHaveBeenCalledWith(
VoterRegistrationPathnames.OTHER_DETAILS + '?state=CA&zip=94043',
VoterRegistrationPathnames.OTHER_DETAILS + '?state=CA',
);
validateAddressesSpy.mockRestore();
});
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { render, cleanup, act } from '@testing-library/react';
import navigation from 'next/navigation';
import { usePrefetchOtherDetailsWithState } from '@/app/register/addresses/hooks/use-prefetch-other-details-with-state';
import { HomeAddressForm } from '@/app/register/addresses/home-address/home-address-form';
import { VoterRegistrationPathnames } from '@/app/register/constants/voter-registration-pathnames';
import { clearAllPersistentFormElements } from 'fully-formed';
import { Builder } from 'builder-pattern';
import { Field } from 'fully-formed';
import type { AppRouterInstance } from 'next/dist/shared/lib/app-router-context.shared-runtime';
import zipState from 'zip-state';

jest.mock('next/navigation', () => ({
useRouter: jest.fn(),
}));

describe('usePrefetchOtherDetailsWithState', () => {
let router: AppRouterInstance;

interface TestComponentProps {
form: InstanceType<typeof HomeAddressForm>;
}

function TestComponent({ form }: TestComponentProps) {
usePrefetchOtherDetailsWithState(form);

return null;
}

beforeEach(() => {
router = Builder<AppRouterInstance>().prefetch(jest.fn()).build();
jest.spyOn(navigation, 'useRouter').mockImplementation(() => router);
});

afterEach(() => {
cleanup();
clearAllPersistentFormElements();
});

it(`prefetches the other details page when the page loads.`, () => {
const zip = '94043';
const state = zipState(zip);
expect(state).toEqual(expect.stringMatching(/[A-Z]{2}/));

const form = new HomeAddressForm(
new Field({ name: 'zip', defaultValue: zip }),
);

render(<TestComponent form={form} />);
expect(router.prefetch).toHaveBeenCalledWith(
VoterRegistrationPathnames.OTHER_DETAILS + `?state=${state}`,
);
});

it(`prefetches the other details page when the state field changes.`, () => {
const form = new HomeAddressForm(
new Field({ name: 'zip', defaultValue: '94043' }),
);

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

act(() => form.fields.state.setValue('PA'));

expect(router.prefetch).toHaveBeenCalledTimes(2);
expect(router.prefetch).toHaveBeenLastCalledWith(
VoterRegistrationPathnames.OTHER_DETAILS +
`?state=${form.state.value.state}`,
);
});
});
Loading

0 comments on commit 27dffea

Please sign in to comment.