From 04745860398e5ce85f94d88f7814d863d67aa998 Mon Sep 17 00:00:00 2001 From: Willian Viana Date: Tue, 21 Jan 2025 12:49:32 -0300 Subject: [PATCH] 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', + }); }); }); });