From aca0c63b21fd068e5a2091eeada0add627358aad Mon Sep 17 00:00:00 2001 From: Caroline <4971715+carolineBda@users.noreply.github.com> Date: Tue, 7 Mar 2023 10:58:08 +0100 Subject: [PATCH] fix(remove second address): make sure we remove the second coordinates if we remove the second address (#284) --- .../__tests__/format-psychologist.spec.ts | 37 +++++++++++++++++-- src/services/format-psychologists.ts | 10 +++-- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/services/__tests__/format-psychologist.spec.ts b/src/services/__tests__/format-psychologist.spec.ts index a70381f3..9fc9e378 100644 --- a/src/services/__tests__/format-psychologist.spec.ts +++ b/src/services/__tests__/format-psychologist.spec.ts @@ -6,10 +6,19 @@ jest.mock("axios"); describe("formatPsychologist", () => { beforeEach(() => { (axios.get as jest.MockedFunction).mockImplementation( - () => Promise.resolve({ data: {} }) + () => + Promise.resolve({ + data: { + features: [ + { geometry: { coordinates: [1, 2] }, properties: { score: 10 } }, + ], + }, + }) ); }); - + afterEach(() => { + jest.clearAllMocks(); + }); const partial = { address: "12 Rue Neuve 31000 Toulouse", demarcheSimplifieesId: "1", @@ -22,13 +31,21 @@ describe("formatPsychologist", () => { state: "Accepted", teleconsultation: true, }; - + const DB_COORDINATES = { + coordinates: [1, 2], + crs: { + properties: { name: "EPSG:4326" }, + type: "name", + }, + type: "POINT", + }; it("should format psychologist", async () => { const result = await formatPsychologist(partial); expect(result).toStrictEqual({ address: "12 Rue Neuve 31000 Toulouse", archived: false, - coordinates: null, + coordinates: DB_COORDINATES, + secondAddressCoordinates: null, demarcheSimplifieesId: "1", department: "31", displayEmail: false, @@ -54,6 +71,7 @@ describe("formatPsychologist", () => { secondAddress: "14 Rue Neuve 31000 Toulouse", }); expect(result.secondAddress).toBe("14 Rue Neuve 31000 Toulouse"); + expect(result.secondAddressCoordinates).toStrictEqual(DB_COORDINATES); expect(axios.get).toHaveBeenCalledWith( "https://httpbin.org/apiAdresse/?q=11%20Rue%20Neuve%2031000%20Toulouse&limit=1" ); @@ -62,6 +80,17 @@ describe("formatPsychologist", () => { ); }); + it("should set coordinate to null if second address is erased", async () => { + const result = await formatPsychologist({ + ...partial, + address: "11 Rue Neuve 31000 Toulouse", + // @ts-ignore + secondAddressCoordinates: { type: "my old coordinate" }, + }); + expect(result.secondAddressCoordinates).toBe(null); + expect(axios.get).toHaveBeenCalledTimes(1); + }); + it.each` input | resultValue ${undefined} | ${undefined} diff --git a/src/services/format-psychologists.ts b/src/services/format-psychologists.ts index 0545c910..01f65b84 100644 --- a/src/services/format-psychologists.ts +++ b/src/services/format-psychologists.ts @@ -81,15 +81,17 @@ export const formatPsychologist = async ( ? formatCoordinates(coordinates) : null; + let secondAddressCoordinates; if (psychologist.secondAddress) { - const coordinates = await getAddressCoordinates( + secondAddressCoordinates = await getAddressCoordinates( identifier, psychologist.secondAddress ); - psychologist.secondAddressCoordinates = coordinates - ? formatCoordinates(coordinates) - : null; } + psychologist.secondAddressCoordinates = secondAddressCoordinates + ? formatCoordinates(coordinates) + : null; + return psychologist as Psychologist; };