Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
fix(remove second address): make sure we remove the second coordinate…
Browse files Browse the repository at this point in the history
…s if we remove the second address (#284)
  • Loading branch information
carolineBda authored Mar 7, 2023
1 parent 0307a11 commit aca0c63
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
37 changes: 33 additions & 4 deletions src/services/__tests__/format-psychologist.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ jest.mock("axios");
describe("formatPsychologist", () => {
beforeEach(() => {
(axios.get as jest.MockedFunction<typeof axios.get>).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",
Expand All @@ -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,
Expand All @@ -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"
);
Expand All @@ -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}
Expand Down
10 changes: 6 additions & 4 deletions src/services/format-psychologists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down

0 comments on commit aca0c63

Please sign in to comment.