Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { I18n } from '@18f/identity-i18n';
import { setupServer } from 'msw/node';
import { rest } from 'msw';
import type { SetupServerApi } from 'msw/node';
import { SWRConfig } from 'swr';
import { I18nContext } from '@18f/identity-react-i18n';
import { ComponentType } from 'react';
import { LOCATIONS_URL } from './in-person-location-step';
import { ADDRESS_SEARCH_URL } from './address-search';
import InPersonContext from '../context/in-person';
Expand All @@ -23,6 +26,31 @@ const DEFAULT_RESPONSE = [
},
];

const MULTI_LOCATION_RESPONSE = [
{
address: '100 Main St E, Bronwood, Georgia, 39826',
location: {
latitude: 31.831686000000005,
longitude: -84.363768,
},
street_address: '100 Main St E',
city: 'Bronwood',
state: 'GA',
zip_code: '39826',
},
{
address: '200 Main St E, Bronwood, Georgia, 39826',
location: {
latitude: 32.831686000000005,
longitude: -83.363768,
},
street_address: '200 Main St E',
city: 'Bronwood',
state: 'GA',
zip_code: '39826',
},
];

const DEFAULT_PROPS = {
toPreviousStep() {},
onChange() {},
Expand All @@ -31,6 +59,12 @@ const DEFAULT_PROPS = {
};

describe('InPersonLocationStep', () => {
const wrapper: ComponentType = ({ children }) => (
<InPersonContext.Provider value={{ arcgisSearchEnabled: true }}>
<SWRConfig value={{ provider: () => new Map() }}>{children}</SWRConfig>
</InPersonContext.Provider>
);

let server: SetupServerApi;

before(() => {
Expand All @@ -56,11 +90,8 @@ describe('InPersonLocationStep', () => {

it('displays a 500 error if the request to the USPS API throws an error', async () => {
const { findByText, findByLabelText } = render(
<SWRConfig value={{ provider: () => new Map() }}>
<InPersonContext.Provider value={{ arcgisSearchEnabled: true }}>
<InPersonLocationPostOfficeSearchStep {...DEFAULT_PROPS} />
</InPersonContext.Provider>
</SWRConfig>,
<InPersonLocationPostOfficeSearchStep {...DEFAULT_PROPS} />,
{ wrapper },
);

await userEvent.type(
Expand All @@ -87,11 +118,8 @@ describe('InPersonLocationStep', () => {

it('allows search by address when enabled', async () => {
const { findAllByText, findByText, findByLabelText, queryAllByText } = render(
<SWRConfig value={{ provider: () => new Map() }}>
<InPersonContext.Provider value={{ arcgisSearchEnabled: true }}>
<InPersonLocationPostOfficeSearchStep {...DEFAULT_PROPS} />
</InPersonContext.Provider>
</SWRConfig>,
<InPersonLocationPostOfficeSearchStep {...DEFAULT_PROPS} />,
{ wrapper },
);

const results = queryAllByText('in_person_proofing.body.location.location_button');
Expand All @@ -109,13 +137,9 @@ describe('InPersonLocationStep', () => {
});

it('validates input and shows inline error', async () => {
const { findByText } = render(
<SWRConfig value={{ provider: () => new Map() }}>
<InPersonContext.Provider value={{ arcgisSearchEnabled: true }}>
<InPersonLocationPostOfficeSearchStep {...DEFAULT_PROPS} />
</InPersonContext.Provider>
</SWRConfig>,
);
const { findByText } = render(<InPersonLocationPostOfficeSearchStep {...DEFAULT_PROPS} />, {
wrapper,
});

await userEvent.click(
await findByText('in_person_proofing.body.location.po_search.search_button'),
Expand All @@ -126,11 +150,8 @@ describe('InPersonLocationStep', () => {

it('displays no post office results if a successful search is followed by an unsuccessful search', async () => {
const { findByText, findByLabelText, queryByRole } = render(
<SWRConfig value={{ provider: () => new Map() }}>
<InPersonContext.Provider value={{ arcgisSearchEnabled: true }}>
<InPersonLocationPostOfficeSearchStep {...DEFAULT_PROPS} />
</InPersonContext.Provider>
</SWRConfig>,
<InPersonLocationPostOfficeSearchStep {...DEFAULT_PROPS} />,
{ wrapper },
);

await userEvent.type(
Expand All @@ -157,11 +178,8 @@ describe('InPersonLocationStep', () => {

it('clicking search again after first results do not clear results', async () => {
const { findAllByText, findByText, findByLabelText } = render(
<SWRConfig value={{ provider: () => new Map() }}>
<InPersonContext.Provider value={{ arcgisSearchEnabled: true }}>
<InPersonLocationPostOfficeSearchStep {...DEFAULT_PROPS} />
</InPersonContext.Provider>
</SWRConfig>,
<InPersonLocationPostOfficeSearchStep {...DEFAULT_PROPS} />,
{ wrapper },
);

await userEvent.type(
Expand All @@ -176,6 +194,77 @@ describe('InPersonLocationStep', () => {
);
await findAllByText('in_person_proofing.body.location.location_button');
});

it('displays correct pluralization for a single location result', async () => {
const { findByLabelText, findByText } = render(
<I18nContext.Provider
value={
new I18n({
strings: {
'in_person_proofing.body.location.po_search.results_description': {
one: 'one location result',
other: '%{count} %{address}',
},
},
})
}
>
<InPersonLocationPostOfficeSearchStep {...DEFAULT_PROPS} />
</I18nContext.Provider>,
{ wrapper },
);
await userEvent.type(
await findByLabelText('in_person_proofing.body.location.po_search.address_search_label'),
'800 main',
);
await userEvent.click(
await findByText('in_person_proofing.body.location.po_search.search_button'),
);
await userEvent.click(
await findByText('in_person_proofing.body.location.po_search.search_button'),
);

await findByText('one location result');
});

it('displays correct pluralization for multiple location results', async () => {
server.resetHandlers();
server.use(
rest.post(ADDRESS_SEARCH_URL, (_req, res, ctx) => res(ctx.json(DEFAULT_RESPONSE))),
rest.post(LOCATIONS_URL, (_req, res, ctx) => res(ctx.json(MULTI_LOCATION_RESPONSE))),
);

const { findByLabelText, findByText } = render(
<I18nContext.Provider
value={
new I18n({
strings: {
'in_person_proofing.body.location.po_search.results_description': {
one: 'one location result',
other: 'more than one result: %{count} %{address}',
},
},
})
}
>
<InPersonLocationPostOfficeSearchStep {...DEFAULT_PROPS} />
</I18nContext.Provider>,
{ wrapper },
);
await userEvent.type(
await findByLabelText('in_person_proofing.body.location.po_search.address_search_label'),
'800 main',
);
await userEvent.click(
await findByText('in_person_proofing.body.location.po_search.search_button'),
);
await userEvent.click(
await findByText('in_person_proofing.body.location.po_search.search_button'),
);
await findByText(
`more than one result: ${MULTI_LOCATION_RESPONSE.length} ${MULTI_LOCATION_RESPONSE[0].address}`,
);
});
});

context('subsequent network failures clear results', () => {
Expand All @@ -188,11 +277,8 @@ describe('InPersonLocationStep', () => {

it('subsequent failure clears previous results', async () => {
const { findAllByText, findByText, findByLabelText, queryAllByText } = render(
<SWRConfig value={{ provider: () => new Map() }}>
<InPersonContext.Provider value={{ arcgisSearchEnabled: true }}>
<InPersonLocationPostOfficeSearchStep {...DEFAULT_PROPS} />
</InPersonContext.Provider>
</SWRConfig>,
<InPersonLocationPostOfficeSearchStep {...DEFAULT_PROPS} />,
{ wrapper },
);

await userEvent.type(
Expand Down
6 changes: 4 additions & 2 deletions config/locales/in_person_proofing/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ en:
ID to try and verify your identity online again.
po_search_about: If you are having trouble adding your ID, you may be able to
verify your identity in person at a local United States Post Office.
results_description: There are %{count} participating Post Offices within 50
miles of %{address}.
results_description:
one: There is one participating Post Office within 50 miles of %{address}.
other: There are %{count} participating Post Offices within 50 miles of
%{address}.
results_instructions: Select a Post Office location below, or search again using
a different address. For facility accessibility, use the contact
information listed for the Post Office location.
Expand Down
7 changes: 5 additions & 2 deletions config/locales/in_person_proofing/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ es:
po_search_about: Si tiene problemas para añadir su documento de identidad, es
posible que pueda verificar su identidad en persona en una oficina
de correos local de los Estados Unidos.
results_description: Hay %{count} de oficinas de correos participantes en un
radio de 50 millas de la %{address}.
results_description:
one: Hay 1 oficina de correos participante en un radio de 50 millas de la
%{address}.
other: Hay %{count} de oficinas de correos participantes en un radio de 50
millas de la %{address}.
results_instructions: Seleccione una ubicación de la Oficina de Correos a
continuación, o busque de nuevo utilizando una dirección diferente.
Para la accesibilidad de las instalaciones, utilice la información
Expand Down
7 changes: 5 additions & 2 deletions config/locales/in_person_proofing/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ fr:
po_search_about: Si vous avez des difficultés à ajouter votre pièce d’identité,
vous pouvez vérifier votre identité en personne dans un bureau de
poste américain proche.
results_description: Il y a %{count} de bureaux de poste participants dans un
rayon de 50 miles autour de %{address}.
results_description:
one: Il y a 1 bureau de poste participant dans un rayon de 50 miles autour de
%{address}.
other: Il y a %{count} de bureaux de poste participants dans un rayon de 50
miles autour de %{address}.
results_instructions: Sélectionnez un emplacement de bureau de poste ci-dessous,
ou effectuez une nouvelle recherche en utilisant une autre adresse.
Pour l’accessibilité des installations, utilisez les informations de
Expand Down