Skip to content

Commit 0b2fdf7

Browse files
committed
feat(location-service): add location tests
1 parent 010305b commit 0b2fdf7

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

services/__tests__/location.spec.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { jest } from '@jest/globals';
2+
3+
import {
4+
getCountriesProvider,
5+
getRegionsProvider,
6+
getSubRegionsProvider,
7+
} from 'services/country';
8+
9+
import { countryConfig } from '../location';
10+
11+
jest.mock('services/country', () => ({
12+
getCountriesProvider: jest.fn(),
13+
getRegionsProvider: jest.fn(),
14+
getSubRegionsProvider: jest.fn(),
15+
}));
16+
17+
describe('countryConfig', () => {
18+
afterEach(() => {
19+
jest.clearAllMocks();
20+
});
21+
22+
describe('adm0', () => {
23+
it('should return the correct country data', async () => {
24+
const mockCountries = [{ iso: 'BRA', name: 'Brazil' }];
25+
getCountriesProvider.mockResolvedValue({ data: mockCountries });
26+
27+
const result = await countryConfig.adm0({ adm0: 'BRA' });
28+
29+
expect(result).toEqual({
30+
locationName: 'Brazil',
31+
iso: 'BRA',
32+
});
33+
expect(getCountriesProvider).toHaveBeenCalledTimes(1);
34+
});
35+
});
36+
37+
describe('adm1', () => {
38+
it('should return the correct region and country data', async () => {
39+
const mockCountries = [{ iso: 'BRA', name: 'Brazil' }];
40+
const mockRegions = [{ id: 'BRA.25_', name: 'São Paulo', iso: 'BRA' }];
41+
42+
getCountriesProvider.mockResolvedValue({ data: mockCountries });
43+
getRegionsProvider.mockResolvedValue({ data: mockRegions });
44+
45+
const result = await countryConfig.adm1({ adm0: 'BRA', adm1: '25' });
46+
47+
expect(result).toEqual({
48+
locationName: 'São Paulo, Brazil',
49+
id: 'BRA.25_',
50+
iso: 'BRA',
51+
});
52+
expect(getCountriesProvider).toHaveBeenCalledTimes(1);
53+
expect(getRegionsProvider).toHaveBeenCalledTimes(1);
54+
});
55+
});
56+
57+
describe('adm2', () => {
58+
it('should return the correct sub-region, region, and country data', async () => {
59+
const mockCountries = [{ iso: 'BRA', name: 'Brazil' }];
60+
const mockRegions = [{ id: 'BRA.25_', name: 'São Paulo' }];
61+
const mockSubRegions = [{ id: 'BRA.25.390_', name: 'Osasco' }];
62+
63+
getCountriesProvider.mockResolvedValue({ data: mockCountries });
64+
getRegionsProvider.mockResolvedValue({ data: mockRegions });
65+
getSubRegionsProvider.mockResolvedValue({ data: mockSubRegions });
66+
67+
const result = await countryConfig.adm2({
68+
adm0: 'BRA',
69+
adm1: '25',
70+
adm2: '390',
71+
});
72+
73+
expect(result).toEqual({
74+
locationName: 'Osasco, Brazil, São Paulo',
75+
id: 'BRA.25.390_',
76+
});
77+
expect(getCountriesProvider).toHaveBeenCalledTimes(1);
78+
expect(getRegionsProvider).toHaveBeenCalledTimes(1);
79+
expect(getSubRegionsProvider).toHaveBeenCalledTimes(1);
80+
});
81+
});
82+
});

services/location.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const countryConfig = {
3737
const country = findByIso(countries, adm0);
3838
const region = findById(regions, `${adm0}.${adm1}_`);
3939

40-
const { name, ...props } = region;
40+
const { name, ...props } = region || {};
4141

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

62-
const { name, ...props } = subRegion;
62+
const { name, ...props } = subRegion || {};
6363

6464
return {
6565
locationName: `${name}, ${country?.name}, ${region?.name}`,

0 commit comments

Comments
 (0)