This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCountryUtils.test.ts
72 lines (58 loc) · 2.84 KB
/
CountryUtils.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) 2023. Heusala Group Oy <info@hg.fi>. All rights reserved.
import { CountryUtils } from "./CountryUtils";
import { CountryCode } from "./types/CountryCode";
import { isArray } from "./types/Array";
describe('CountryUtils', () => {
const mockTranslationFunction = (key: string) => `lang.${key}`; // simple mock translation function for testing
describe('getCountryList', () => {
it('should return a list of Country objects', () => {
const countries = CountryUtils.getCountryList();
expect(countries).toBeDefined();
expect(isArray(countries)).toBe(true);
});
});
describe('getCountryCodeList', () => {
it('should return a list of CountryCodes', () => {
const countryCodes = CountryUtils.getCountryCodeList();
expect(countryCodes).toBeDefined();
expect(isArray(countryCodes)).toBe(true);
});
});
describe('createCountryByCode', () => {
it('should create Country object for valid CountryCode', () => {
const country = CountryUtils.createCountryByCode(CountryCode.AF);
expect(country).toBeDefined();
expect(country.iso2).toEqual(CountryCode.AF);
});
it('should throw an error for unknown CountryCode', () => {
expect(() => CountryUtils.createCountryByCode('UnknownCode' as CountryCode)).toThrowError();
});
});
describe('createCountryAutoCompleteValues', () => {
it('should create an array of autocomplete mappings', () => {
const countryCodes = CountryUtils.getCountryCodeList();
const autoCompleteMappings = CountryUtils.createCountryAutoCompleteValues(countryCodes, mockTranslationFunction);
expect(autoCompleteMappings).toBeDefined();
expect(isArray(autoCompleteMappings)).toBe(true);
});
});
describe('parseCountry', () => {
it('should return Country object for a valid country name', () => {
const countryName = 'af';
const country = CountryUtils.parseCountry(countryName, mockTranslationFunction);
expect(country).toBeDefined();
expect(country?.iso2).toEqual(CountryCode.AF);
});
it('should return Country object for a valid translated country name', () => {
const countryName = 'lang.countryCode.af.name';
const country = CountryUtils.parseCountry(countryName, mockTranslationFunction);
expect(country).toBeDefined();
expect(country?.iso2).toEqual(CountryCode.AF);
});
it('should return undefined for an unknown country name', () => {
const countryName = 'UnknownCountry';
const country = CountryUtils.parseCountry(countryName, mockTranslationFunction);
expect(country).toBeUndefined();
});
});
});