Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajout règle minimum 3 caractères pour le lieu #252

Merged
merged 2 commits into from
Nov 26, 2024
Merged
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
6 changes: 4 additions & 2 deletions src/views/candidature-conseiller/AddressChooser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ export default function AddressChooser() {
id="lieuHabitation"
list="resultatsRecherche"
onChange={debounce(async event => {
searchByName(event.target.value);
const codeCommune = await villes.find(({ codesPostaux, nom }) =>
if (event.target.value.length >= 3) {
searchByName(event.target.value);
}
const codeCommune = await villes?.find(({ codesPostaux, nom }) =>
(`${codesPostaux[0]} ${nom}`).toUpperCase() === (event.target.value).toUpperCase())?.code;
setCodeCommune(codeCommune);
})}
Expand Down
30 changes: 30 additions & 0 deletions src/views/candidature-conseiller/CandidatureConseiller.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import CandidatureConseiller from './CandidatureConseiller';
import { textMatcher, dateDujour } from '../../../test/test-utils';
import * as ReactRouterDom from 'react-router-dom';
import * as useApiAdmin from './useApiAdmin';
import * as useGeoApi from './useGeoApi';

vi.mock('react-router-dom', () => ({
useLocation: () => ({ hash: '' }),
Expand Down Expand Up @@ -723,4 +724,33 @@ describe('candidature conseiller', () => {

vi.useRealTimers();
});

it('quand je remplis le formulaire et que je saisi mon lieu d’habitation alors la recherche est lancer à partir de 3 caractères', async () => {
// GIVEN
vi.useFakeTimers();
const searchByNameSpy = vi.fn();
vi.spyOn(useGeoApi, 'useGeoApi').mockImplementation(() => ({
searchByName: searchByNameSpy,
}));

// WHEN
render(<CandidatureConseiller />);
const adresse = screen.getByLabelText('Votre lieu d’habitation * Saississez le nom ou le code postal de votre commune.');

// THEN
fireEvent.change(adresse, { target: { value: '9' } });
vi.advanceTimersByTime(300);
expect(searchByNameSpy).toHaveBeenCalledTimes(0);
fireEvent.change(adresse, { target: { value: '93' } });
vi.advanceTimersByTime(300);
expect(searchByNameSpy).toHaveBeenCalledTimes(0);
fireEvent.change(adresse, { target: { value: '931' } });
vi.advanceTimersByTime(300);
expect(searchByNameSpy).toHaveBeenCalledTimes(1);
fireEvent.change(adresse, { target: { value: '93100' } });
vi.advanceTimersByTime(300);
expect(searchByNameSpy).toHaveBeenCalledTimes(2);

vi.useRealTimers();
});
});