Skip to content

Commit

Permalink
feat(location-service): add location tests
Browse files Browse the repository at this point in the history
  • Loading branch information
willian-viana committed Jan 23, 2025
1 parent 010305b commit 0b2fdf7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
82 changes: 82 additions & 0 deletions services/__tests__/location.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { jest } from '@jest/globals';

import {
getCountriesProvider,
getRegionsProvider,
getSubRegionsProvider,
} from 'services/country';

import { countryConfig } from '../location';

jest.mock('services/country', () => ({
getCountriesProvider: jest.fn(),
getRegionsProvider: jest.fn(),
getSubRegionsProvider: jest.fn(),
}));

describe('countryConfig', () => {
afterEach(() => {
jest.clearAllMocks();
});

describe('adm0', () => {
it('should return the correct country data', async () => {
const mockCountries = [{ iso: 'BRA', name: 'Brazil' }];
getCountriesProvider.mockResolvedValue({ data: mockCountries });

const result = await countryConfig.adm0({ adm0: 'BRA' });

expect(result).toEqual({
locationName: 'Brazil',
iso: 'BRA',
});
expect(getCountriesProvider).toHaveBeenCalledTimes(1);
});
});

describe('adm1', () => {
it('should return the correct region and country data', async () => {
const mockCountries = [{ iso: 'BRA', name: 'Brazil' }];
const mockRegions = [{ id: 'BRA.25_', name: 'São Paulo', iso: 'BRA' }];

getCountriesProvider.mockResolvedValue({ data: mockCountries });
getRegionsProvider.mockResolvedValue({ data: mockRegions });

const result = await countryConfig.adm1({ adm0: 'BRA', adm1: '25' });

expect(result).toEqual({
locationName: 'São Paulo, Brazil',
id: 'BRA.25_',
iso: 'BRA',
});
expect(getCountriesProvider).toHaveBeenCalledTimes(1);
expect(getRegionsProvider).toHaveBeenCalledTimes(1);
});
});

describe('adm2', () => {
it('should return the correct sub-region, region, and country data', async () => {
const mockCountries = [{ iso: 'BRA', name: 'Brazil' }];
const mockRegions = [{ id: 'BRA.25_', name: 'São Paulo' }];
const mockSubRegions = [{ id: 'BRA.25.390_', name: 'Osasco' }];

getCountriesProvider.mockResolvedValue({ data: mockCountries });
getRegionsProvider.mockResolvedValue({ data: mockRegions });
getSubRegionsProvider.mockResolvedValue({ data: mockSubRegions });

const result = await countryConfig.adm2({
adm0: 'BRA',
adm1: '25',
adm2: '390',
});

expect(result).toEqual({
locationName: 'Osasco, Brazil, São Paulo',
id: 'BRA.25.390_',
});
expect(getCountriesProvider).toHaveBeenCalledTimes(1);
expect(getRegionsProvider).toHaveBeenCalledTimes(1);
expect(getSubRegionsProvider).toHaveBeenCalledTimes(1);
});
});
});
4 changes: 2 additions & 2 deletions services/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const countryConfig = {
const country = findByIso(countries, adm0);
const region = findById(regions, `${adm0}.${adm1}_`);

const { name, ...props } = region;
const { name, ...props } = region || {};

return {
locationName: `${name}, ${country?.name}`,
Expand All @@ -59,7 +59,7 @@ export const countryConfig = {
const region = findById(regions, `${adm0}.${adm1}_`);
const subRegion = findById(subRegions, `${adm0}.${adm1}.${adm2}_`);

const { name, ...props } = subRegion;
const { name, ...props } = subRegion || {};

return {
locationName: `${name}, ${country?.name}, ${region?.name}`,
Expand Down

0 comments on commit 0b2fdf7

Please sign in to comment.