diff --git a/__tests__/requests/insee.spec.js b/__tests__/requests/insee.spec.js index ce5f6e3090fd085dfd8aefe3c4d9a809b442ece1..057c0bd3e2dd109972e361ecaab600fd5b7f3368 100644 --- a/__tests__/requests/insee.spec.js +++ b/__tests__/requests/insee.spec.js @@ -2,11 +2,11 @@ const { errors } = require('cozy-konnector-libs') const { getInseeCode } = require('../../src/requests/insee') describe('getInseeCode', () => { - it('should return a valid insee code for Lyon 7', async () => { + it('should return insee code for: Lyon 7', async () => { expect(await getInseeCode(69007)).toEqual('69387') }) - it('should throw USER_ACTION_NEEDED for a unexisting post code', async () => { + it('should throw USER_ACTION_NEEDED for a inexistant post code', async () => { try { await getInseeCode(69069) } catch (error) { @@ -23,11 +23,11 @@ describe('getInseeCode', () => { }) describe('should handle communes with multiple insee code', () => { - it('should return Craponne insee code for post code 69290', async () => { + it('should return insee code for: Craponne', async () => { expect(await getInseeCode(69290, 'CRAPONNE')).toEqual('69069') }) - it('should return Pollionnay insee code for post code 69290', async () => { + it('should return insee code for: Pollionnay', async () => { expect(await getInseeCode(69290, 'POLLIONNAY')).toEqual('69154') }) @@ -35,9 +35,30 @@ describe('getInseeCode', () => { expect(await getInseeCode('01600', 'SAINT-BERNARD')).toEqual('01339') }) - it('should return insee code for: Neuville sur Saône', async () => { - expect(await getInseeCode(69250, 'Neuville sur Saône')).toEqual('69143') + describe('should handle partial input omitting "sur Saône" or "au mont d\'or"', () => { + it('should return insee code for: Neuville sur Saône', async () => { + expect(await getInseeCode(69250, 'Neuville sur Saône')).toEqual('69143') + }) + it('should return insee code for: Neuville', async () => { + expect(await getInseeCode(69250, 'Neuville')).toEqual('69143') + }) + it('should return insee code for: Poleymieux', async () => { + expect(await getInseeCode(69250, 'Poleymieux')).toEqual('69153') + }) + it('should return insee code for: Poleymieux au Mont d Or', async () => { + expect(await getInseeCode(69250, 'Poleymieux au Mont d Or')).toEqual( + '69153' + ) + }) + it('should throw USER_ACTION_NEEDED when city is not precise enough', async () => { + try { + await getInseeCode(26600, 'e') + } catch (error) { + expect(error).toEqual(errors.USER_ACTION_NEEDED) + } + }) }) + it("should return insee code for: L'isle d'abeau", async () => { expect(await getInseeCode(38080, "L'isle d'abeau")).toEqual('38193') }) diff --git a/src/requests/insee.js b/src/requests/insee.js index 4ebf988248aa1bd19de788cfffcc9a6001591b84..04f4f99e589c0234f14d62bf8f0b29afd2b726e8 100644 --- a/src/requests/insee.js +++ b/src/requests/insee.js @@ -22,12 +22,17 @@ async function getInseeCode(postalCode, city) { if (!city) throw new Error('No city') const parsedCity = sanitizeCity(city) - console.log(parsedCity) - const filteredResponse = response.data.filter( - town => sanitizeCity(town.nomCommune) === parsedCity + const filteredResponse = response.data.filter(commune => + sanitizeCity(commune.nomCommune).includes(parsedCity) ) + if (filteredResponse.length > 1) { + throw new Error( + 'Input city is not precise enough, more than one city was found' + ) + } + return filteredResponse[0].codeCommune } } catch (error) {