From 36986a55fa9e7e3fc48d8d6b297f8c739033826f Mon Sep 17 00:00:00 2001 From: ornella Date: Mon, 25 Nov 2024 22:46:14 +0100 Subject: [PATCH 1/2] =?UTF-8?q?add=20rule=20min=203=20caract=C3=A8res=20po?= =?UTF-8?q?ur=20le=20search?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../candidature-conseiller/AddressChooser.jsx | 6 ++-- .../CandidatureConseiller.test.jsx | 30 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/views/candidature-conseiller/AddressChooser.jsx b/src/views/candidature-conseiller/AddressChooser.jsx index 7f054f1..9594c76 100644 --- a/src/views/candidature-conseiller/AddressChooser.jsx +++ b/src/views/candidature-conseiller/AddressChooser.jsx @@ -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); })} diff --git a/src/views/candidature-conseiller/CandidatureConseiller.test.jsx b/src/views/candidature-conseiller/CandidatureConseiller.test.jsx index 1c1dfdf..4c6b112 100644 --- a/src/views/candidature-conseiller/CandidatureConseiller.test.jsx +++ b/src/views/candidature-conseiller/CandidatureConseiller.test.jsx @@ -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: '' }), @@ -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(() => ({ + searchByName: searchByNameMock, + })); + + // WHEN + render(); + 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); + fireEvent.change(adresse, { target: { value: '93' } }); + vi.advanceTimersByTime(300); + expect(searchByNameMock).toBeCalledTimes(0); + fireEvent.change(adresse, { target: { value: '931' } }); + vi.advanceTimersByTime(300); + expect(searchByNameMock).toBeCalledTimes(1); + fireEvent.change(adresse, { target: { value: '93100' } }); + vi.advanceTimersByTime(300); + expect(searchByNameMock).toBeCalledTimes(2); + + vi.useRealTimers(); + }); }); From f2afd6f5bc41240f5219ac2ee4ecbbbd973f6c66 Mon Sep 17 00:00:00 2001 From: ornella Date: Tue, 26 Nov 2024 10:35:34 +0100 Subject: [PATCH 2/2] retour lint --- .../CandidatureConseiller.test.jsx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/views/candidature-conseiller/CandidatureConseiller.test.jsx b/src/views/candidature-conseiller/CandidatureConseiller.test.jsx index 4c6b112..eb21b5d 100644 --- a/src/views/candidature-conseiller/CandidatureConseiller.test.jsx +++ b/src/views/candidature-conseiller/CandidatureConseiller.test.jsx @@ -728,9 +728,9 @@ describe('candidature conseiller', () => { 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(() => ({ - searchByName: searchByNameMock, + const searchByNameSpy = vi.fn(); + vi.spyOn(useGeoApi, 'useGeoApi').mockImplementation(() => ({ + searchByName: searchByNameSpy, })); // WHEN @@ -740,16 +740,16 @@ describe('candidature conseiller', () => { // THEN fireEvent.change(adresse, { target: { value: '9' } }); vi.advanceTimersByTime(300); - expect(searchByNameMock).toBeCalledTimes(0); + expect(searchByNameSpy).toHaveBeenCalledTimes(0); fireEvent.change(adresse, { target: { value: '93' } }); vi.advanceTimersByTime(300); - expect(searchByNameMock).toBeCalledTimes(0); + expect(searchByNameSpy).toHaveBeenCalledTimes(0); fireEvent.change(adresse, { target: { value: '931' } }); vi.advanceTimersByTime(300); - expect(searchByNameMock).toBeCalledTimes(1); + expect(searchByNameSpy).toHaveBeenCalledTimes(1); fireEvent.change(adresse, { target: { value: '93100' } }); vi.advanceTimersByTime(300); - expect(searchByNameMock).toBeCalledTimes(2); + expect(searchByNameSpy).toHaveBeenCalledTimes(2); vi.useRealTimers(); });