acupedia-store-ui/tests/unit/constants/countries.test.ts

53 lines
2.0 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import {
COUNTRY_CODES,
DEFAULT_COUNTRY,
SORTED_COUNTRY_CODES,
getCountryRuleByDialCode,
getCountryRuleByIso,
getCountryRuleByName,
} from '../../../src/constants/countries';
describe('countries constants & helpers', () => {
it('exports sorted country codes and India as default', () => {
expect(DEFAULT_COUNTRY.country).toBe('India');
expect(DEFAULT_COUNTRY.code).toBe('+91');
expect(SORTED_COUNTRY_CODES.length).toBe(COUNTRY_CODES.length);
});
it('resolves country rule by ISO2 code with default fallback', () => {
expect(getCountryRuleByIso(null)).toEqual(DEFAULT_COUNTRY);
expect(getCountryRuleByIso(' de ')).toMatchObject({ country: 'Germany', code: '+49' });
expect(getCountryRuleByIso('ZZ')).toEqual(DEFAULT_COUNTRY);
});
it('resolves country rule by name, ISO, partial match, or custom fallback', () => {
expect(getCountryRuleByName('')).toEqual(DEFAULT_COUNTRY);
expect(getCountryRuleByName('Germany')).toMatchObject({ iso2: 'DE', code: '+49' });
expect(getCountryRuleByName('gb')).toMatchObject({ country: 'United Kingdom' });
expect(getCountryRuleByName('Republic of Singapore')).toMatchObject({ country: 'Singapore' });
const unknown = getCountryRuleByName('Atlantis');
expect(unknown).toMatchObject({
country: 'Atlantis',
currency: 'USD',
minLength: 7,
maxLength: 15,
});
});
it('resolves country rule by dial code with country hint and fallback', () => {
expect(getCountryRuleByDialCode(null)).toEqual(DEFAULT_COUNTRY);
expect(getCountryRuleByDialCode('+1', 'Canada')).toMatchObject({ country: 'Canada', iso2: 'CA' });
expect(getCountryRuleByDialCode('+44')).toMatchObject({ country: 'United Kingdom' });
expect(getCountryRuleByDialCode('+999', 'CustomLand')).toMatchObject({
code: '+999',
country: 'CustomLand',
});
expect(getCountryRuleByDialCode('+999')).toMatchObject({
code: '+999',
country: 'Other',
});
});
});