Skip to content

Commit

Permalink
add rule min 3 caractères pour le search
Browse files Browse the repository at this point in the history
  • Loading branch information
Ornella452 committed Nov 25, 2024
1 parent 7d74a35 commit 36986a5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
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 searchByNameMock = vi.fn();
vi.spyOn(useGeoApi, "useGeoApi").mockImplementation(() => ({

Check warning on line 732 in src/views/candidature-conseiller/CandidatureConseiller.test.jsx

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote

Check failure on line 732 in src/views/candidature-conseiller/CandidatureConseiller.test.jsx

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
searchByName: searchByNameMock,
}));

// 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(searchByNameMock).toBeCalledTimes(0);

Check warning on line 743 in src/views/candidature-conseiller/CandidatureConseiller.test.jsx

View workflow job for this annotation

GitHub Actions / lint

Replace toBeCalledTimes() with its canonical name toHaveBeenCalledTimes()
fireEvent.change(adresse, { target: { value: '93' } });
vi.advanceTimersByTime(300);
expect(searchByNameMock).toBeCalledTimes(0);

Check warning on line 746 in src/views/candidature-conseiller/CandidatureConseiller.test.jsx

View workflow job for this annotation

GitHub Actions / lint

Replace toBeCalledTimes() with its canonical name toHaveBeenCalledTimes()
fireEvent.change(adresse, { target: { value: '931' } });
vi.advanceTimersByTime(300);
expect(searchByNameMock).toBeCalledTimes(1);

Check warning on line 749 in src/views/candidature-conseiller/CandidatureConseiller.test.jsx

View workflow job for this annotation

GitHub Actions / lint

Replace toBeCalledTimes() with its canonical name toHaveBeenCalledTimes()
fireEvent.change(adresse, { target: { value: '93100' } });
vi.advanceTimersByTime(300);
expect(searchByNameMock).toBeCalledTimes(2);

Check warning on line 752 in src/views/candidature-conseiller/CandidatureConseiller.test.jsx

View workflow job for this annotation

GitHub Actions / lint

Replace toBeCalledTimes() with its canonical name toHaveBeenCalledTimes()

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

0 comments on commit 36986a5

Please sign in to comment.