|
| 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 | +}); |
0 commit comments