Skip to content

Commit

Permalink
Merge pull request #4920 from wri/develop
Browse files Browse the repository at this point in the history
Deploy
  • Loading branch information
willian-viana authored Jan 21, 2025
2 parents 0df95c7 + 86c6217 commit 37d0583
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 2 deletions.
219 changes: 219 additions & 0 deletions services/__tests__/areas.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
import { jest } from '@jest/globals';

import { getArea, getAreas, saveArea, deleteArea } from '../areas';
import { apiAuthRequest } from '../../utils/request';
import { trackEvent } from '../../utils/analytics';

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();
});

describe('Getting a Single Area', () => {
describe('The Request to the API', () => {
it('should add source params for gadm 3.6', 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]=3.6',
{
headers: {},
}
);
});
});
});

describe('Getting Multiple Areas', () => {
describe('The Request to the API', () => {
it('should add source params for gadm 3.6', async () => {
// arrange
apiAuthRequest.get.mockResolvedValueOnce({
data: { data: [{ attributes: {} }] },
});

// act
await getAreas();

// assert
expect(apiAuthRequest.get).toHaveBeenCalledWith(
'/v2/area?source[provider]=gadm&source[version]=3.6'
);
});

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: {
country: 'BRA',
source: {
provider: 'gadm',
version: '3.6',
},
},
},
},
],
},
});

// act
const result = await getAreas();

// assert
expect(result).toEqual([
{
id: '67892867738cd20016c88173',
name: 'Brazil',
admin: {
adm0: 'BRA',
source: {
provider: 'gadm',
version: '3.6',
},
},
iso: {
country: 'BRA',
source: {
provider: 'gadm',
version: '3.6',
},
},
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',
});
});
});
});
5 changes: 3 additions & 2 deletions services/areas.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]=3.6`;

export const getArea = (id, userToken = null) =>
apiAuthRequest
.get(`${REQUEST_URL}/${id}`, {
.get(`${REQUEST_URL}/${id}${SOURCE}`, {
headers: {
...(userToken && {
Authorization: `Bearer ${userToken}`,
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 37d0583

Please sign in to comment.