From 16461e161aba6513d05f9cbf733fcb579edc008f Mon Sep 17 00:00:00 2001 From: Willian Viana Date: Tue, 21 Jan 2025 11:47:47 -0300 Subject: [PATCH 1/5] feat(gadm): add source provider and source version to v2/area endpoint --- services/areas.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/services/areas.js b/services/areas.js index 576120f460..901368b34d 100644 --- a/services/areas.js +++ b/services/areas.js @@ -2,10 +2,11 @@ import { apiAuthRequest } from 'utils/request'; import { trackEvent } from 'utils/analytics'; const REQUEST_URL = '/v2/area'; +const SOURCE = `?source[provider]=gadm&source[version]=4.1`; export const getArea = (id, userToken = null) => apiAuthRequest - .get(`${REQUEST_URL}/${id}`, { + .get(`${REQUEST_URL}/${id}${SOURCE}`, { headers: { ...(userToken && { Authorization: `Bearer ${userToken}`, @@ -36,7 +37,7 @@ export const getArea = (id, userToken = null) => }); export const getAreas = () => - apiAuthRequest.get(REQUEST_URL).then((areasResponse) => { + apiAuthRequest.get(`${REQUEST_URL}${SOURCE}`).then((areasResponse) => { const { data: areas } = areasResponse.data; return areas.map((area) => { From cb897329d0e28b707792b12cdd5356ec65ee0853 Mon Sep 17 00:00:00 2001 From: Willian Viana Date: Tue, 21 Jan 2025 11:47:58 -0300 Subject: [PATCH 2/5] feat(tests): add area service tests for getArea and getAreas methods --- services/__tests__/areas.spec.js | 57 ++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 services/__tests__/areas.spec.js diff --git a/services/__tests__/areas.spec.js b/services/__tests__/areas.spec.js new file mode 100644 index 0000000000..4e59f336d0 --- /dev/null +++ b/services/__tests__/areas.spec.js @@ -0,0 +1,57 @@ +import { jest } from '@jest/globals'; + +import { getArea, getAreas } from '../areas'; +import { apiAuthRequest } from '../../utils/request'; + +jest.mock('../../utils/request', () => ({ + apiAuthRequest: { + get: jest.fn(), + }, +})); + +describe('Areas Service', () => { + afterEach(() => { + jest.clearAllMocks(); // Limpa os mocks após cada teste + }); + + describe('Getting a Single Area', () => { + describe('The Request to the API', () => { + it('should add source params for gadm 4.1', async () => { + // arrange + apiAuthRequest.get.mockResolvedValueOnce({ + data: { data: { attributes: {} } }, + }); + + // act + await getArea('abcdef123456789'); + + // assert + expect(apiAuthRequest.get).toHaveBeenCalledWith( + '/v2/area/abcdef123456789?source[provider]=gadm&source[version]=4.1', + { + headers: {}, + } + ); + }); + }); + }); + + describe('Getting Multiple Areas', () => { + describe('The Request to the API', () => { + it('should add source params for gadm 4.1', async () => { + // arrange + apiAuthRequest.get.mockResolvedValueOnce({ + data: { data: [{ attributes: {} }] }, + }); + + // act + await getAreas(); + + // assert + expect(apiAuthRequest.get).toHaveBeenCalledWith( + '/v2/area?source[provider]=gadm&source[version]=4.1' + ); + }); + }); + }); +}); From 04745860398e5ce85f94d88f7814d863d67aa998 Mon Sep 17 00:00:00 2001 From: Willian Viana Date: Tue, 21 Jan 2025 12:49:32 -0300 Subject: [PATCH 3/5] feat(tests): add tests to saveArea and deleteArea change apiAuthRequest mock to cover saveArea implementation --- services/__tests__/areas.spec.js | 162 +++++++++++++++++++++++++++++-- 1 file changed, 156 insertions(+), 6 deletions(-) diff --git a/services/__tests__/areas.spec.js b/services/__tests__/areas.spec.js index 4e59f336d0..8448ea591e 100644 --- a/services/__tests__/areas.spec.js +++ b/services/__tests__/areas.spec.js @@ -1,17 +1,29 @@ import { jest } from '@jest/globals'; -import { getArea, getAreas } from '../areas'; +import { getArea, getAreas, saveArea, deleteArea } from '../areas'; import { apiAuthRequest } from '../../utils/request'; +import { trackEvent } from '../../utils/analytics'; -jest.mock('../../utils/request', () => ({ - apiAuthRequest: { - get: jest.fn(), - }, +jest.mock('../../utils/request', () => { + const mockApiAuthRequest = jest.fn(() => { + return Promise.resolve({}); + }); + + mockApiAuthRequest.get = jest.fn(() => Promise.resolve({})); + mockApiAuthRequest.delete = jest.fn(() => Promise.resolve({})); + + return { + apiAuthRequest: mockApiAuthRequest, + }; +}); + +jest.mock('../../utils/analytics', () => ({ + trackEvent: jest.fn(), })); describe('Areas Service', () => { afterEach(() => { - jest.clearAllMocks(); // Limpa os mocks após cada teste + jest.clearAllMocks(); }); describe('Getting a Single Area', () => { @@ -52,6 +64,144 @@ describe('Areas Service', () => { '/v2/area?source[provider]=gadm&source[version]=4.1' ); }); + + it('should return a valid array containing area item', async () => { + // arrange + apiAuthRequest.get.mockResolvedValueOnce({ + data: { + data: [ + { + type: 'area', + id: '67892867738cd20016c88173', + attributes: { + name: 'Brazil', + admin: { + adm0: 'BRA', + source: { + provider: 'gadm', + version: '3.6', + }, + }, + iso: {}, + }, + }, + ], + }, + }); + + // act + const result = await getAreas(); + + // assert + expect(result).toEqual([ + { + id: '67892867738cd20016c88173', + name: 'Brazil', + admin: { + adm0: 'BRA', + source: { + provider: 'gadm', + version: '3.6', + }, + }, + iso: {}, + use: {}, + userArea: true, + }, + ]); + }); + }); + }); + + describe('Saving an Area', () => { + describe('When creating a new area', () => { + it('should send a POST request and track the event', async () => { + // arrange + const mockData = { name: 'New Area' }; + apiAuthRequest.mockResolvedValueOnce({ + data: { + data: { id: 'newarea123', attributes: { name: 'New Area' } }, + }, + }); + + // act + const result = await saveArea(mockData); + + // assert + expect(apiAuthRequest).toHaveBeenCalledWith( + expect.objectContaining({ + method: 'POST', + url: '/v2/area', + data: mockData, + }) + ); + + expect(trackEvent).toHaveBeenCalledWith({ + category: 'User AOIs', + action: 'User saves aoi', + label: 'newarea123', + }); + expect(result).toEqual({ + id: 'newarea123', + name: 'New Area', + userArea: true, + }); + }); + }); + + describe('When updating an existing area', () => { + it('should send a PATCH request and track the event', async () => { + // arrange + const mockData = { id: 'updatearea456', name: 'Updated Area' }; + apiAuthRequest.mockResolvedValueOnce({ + data: { + data: { id: 'updatearea456', attributes: { name: 'Updated Area' } }, + }, + }); + + // act + const result = await saveArea(mockData); + + // assert + expect(apiAuthRequest).toHaveBeenCalledWith( + expect.objectContaining({ + method: 'PATCH', + url: '/v2/area/updatearea456', + data: mockData, + }) + ); + expect(trackEvent).toHaveBeenCalledWith({ + category: 'User AOIs', + action: 'User edits aoi', + label: 'updatearea456', + }); + expect(result).toEqual({ + id: 'updatearea456', + name: 'Updated Area', + userArea: true, + }); + }); + }); + }); + + describe('Deleting an Area', () => { + it('should send a DELETE request and track the event', async () => { + // arrange + const areaId = 'deletearea789'; + apiAuthRequest.delete.mockResolvedValueOnce(); + + // act + await deleteArea(areaId); + + // assert + expect(apiAuthRequest.delete).toHaveBeenCalledWith( + '/v2/area/deletearea789' + ); + expect(trackEvent).toHaveBeenCalledWith({ + category: 'User AOIs', + action: 'User deletes aoi', + label: 'deletearea789', + }); }); }); }); From d4d62a0e24c3af11b5a4d85441057385775b984a Mon Sep 17 00:00:00 2001 From: Willian Viana Date: Tue, 21 Jan 2025 12:56:59 -0300 Subject: [PATCH 4/5] chore(gadm): set source[version] to 3.6 to enable production deployment --- services/__tests__/areas.spec.js | 8 ++++---- services/areas.js | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/services/__tests__/areas.spec.js b/services/__tests__/areas.spec.js index 8448ea591e..d709de71b7 100644 --- a/services/__tests__/areas.spec.js +++ b/services/__tests__/areas.spec.js @@ -28,7 +28,7 @@ describe('Areas Service', () => { describe('Getting a Single Area', () => { describe('The Request to the API', () => { - it('should add source params for gadm 4.1', async () => { + it('should add source params for gadm 3.6', async () => { // arrange apiAuthRequest.get.mockResolvedValueOnce({ data: { data: { attributes: {} } }, @@ -39,7 +39,7 @@ describe('Areas Service', () => { // assert expect(apiAuthRequest.get).toHaveBeenCalledWith( - '/v2/area/abcdef123456789?source[provider]=gadm&source[version]=4.1', + '/v2/area/abcdef123456789?source[provider]=gadm&source[version]=3.6', { headers: {}, } @@ -50,7 +50,7 @@ describe('Areas Service', () => { describe('Getting Multiple Areas', () => { describe('The Request to the API', () => { - it('should add source params for gadm 4.1', async () => { + it('should add source params for gadm 3.6', async () => { // arrange apiAuthRequest.get.mockResolvedValueOnce({ data: { data: [{ attributes: {} }] }, @@ -61,7 +61,7 @@ describe('Areas Service', () => { // assert expect(apiAuthRequest.get).toHaveBeenCalledWith( - '/v2/area?source[provider]=gadm&source[version]=4.1' + '/v2/area?source[provider]=gadm&source[version]=3.6' ); }); diff --git a/services/areas.js b/services/areas.js index 901368b34d..d9df4c4215 100644 --- a/services/areas.js +++ b/services/areas.js @@ -2,7 +2,7 @@ import { apiAuthRequest } from 'utils/request'; import { trackEvent } from 'utils/analytics'; const REQUEST_URL = '/v2/area'; -const SOURCE = `?source[provider]=gadm&source[version]=4.1`; +const SOURCE = `?source[provider]=gadm&source[version]=3.6`; export const getArea = (id, userToken = null) => apiAuthRequest From 26f9c99f84e9df43ee8dbafac087420d97f7b5a9 Mon Sep 17 00:00:00 2001 From: Willian Viana Date: Tue, 21 Jan 2025 13:13:31 -0300 Subject: [PATCH 5/5] chore(tests): improve area expected mock --- services/__tests__/areas.spec.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/services/__tests__/areas.spec.js b/services/__tests__/areas.spec.js index d709de71b7..dee5f894c9 100644 --- a/services/__tests__/areas.spec.js +++ b/services/__tests__/areas.spec.js @@ -82,7 +82,13 @@ describe('Areas Service', () => { version: '3.6', }, }, - iso: {}, + iso: { + country: 'BRA', + source: { + provider: 'gadm', + version: '3.6', + }, + }, }, }, ], @@ -104,7 +110,13 @@ describe('Areas Service', () => { version: '3.6', }, }, - iso: {}, + iso: { + country: 'BRA', + source: { + provider: 'gadm', + version: '3.6', + }, + }, use: {}, userArea: true, },