2019-07-31 17:19:49 -04:00
|
|
|
const {
|
2019-08-05 23:33:05 -04:00
|
|
|
countryInfo,
|
2019-07-31 17:19:49 -04:00
|
|
|
countryOptions,
|
2019-08-05 23:33:05 -04:00
|
|
|
lookupCountryInfo,
|
|
|
|
dupeCommonCountries,
|
2019-11-05 18:31:13 -05:00
|
|
|
registrationCountryCodeOptions,
|
|
|
|
registrationCountryNameOptions,
|
2019-07-31 17:19:49 -04:00
|
|
|
subdivisionOptions
|
|
|
|
} = require('../../../src/lib/country-data');
|
|
|
|
|
|
|
|
describe('unit test lib/country-data.js', () => {
|
|
|
|
|
2019-08-05 23:33:05 -04:00
|
|
|
test('countryInfo has the ballpark number of countries we expect', () => {
|
|
|
|
expect(typeof countryInfo).toBe('object');
|
|
|
|
expect(countryInfo.length > 200).toEqual(true);
|
|
|
|
expect(countryInfo.length < 300).toEqual(true);
|
2019-07-31 17:19:49 -04:00
|
|
|
});
|
|
|
|
|
2019-08-05 23:33:05 -04:00
|
|
|
test('countryOptions() maintains number of items', () => {
|
|
|
|
expect(typeof countryOptions).toBe('function');
|
|
|
|
const myCountryOptions = countryOptions(countryInfo, 'name');
|
|
|
|
const numCountries = countryInfo.length;
|
|
|
|
expect(myCountryOptions.length).toEqual(numCountries);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('countryOptions() called with value=name will use correct display strings and country name', () => {
|
|
|
|
expect(typeof countryOptions).toBe('function');
|
|
|
|
const myCountryOptions = countryOptions(countryInfo, 'name');
|
|
|
|
|
|
|
|
const eswatiniInfo = myCountryOptions.find(country => country.value === 'Swaziland');
|
|
|
|
expect(eswatiniInfo).toBeTruthy();
|
|
|
|
expect(eswatiniInfo.label).toEqual('Eswatini');
|
|
|
|
|
|
|
|
const swedenInfo = myCountryOptions.find(country => country.value === 'Sweden');
|
|
|
|
expect(swedenInfo).toBeTruthy();
|
|
|
|
expect(swedenInfo.label).toEqual('Sweden');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('countryOptions() called with value==code will use correct display strings and country code', () => {
|
|
|
|
expect(typeof countryOptions).toBe('function');
|
|
|
|
const myCountryOptions = countryOptions(countryInfo, 'code');
|
|
|
|
const szInfo = myCountryOptions
|
|
|
|
.find(country => country.value === 'sz');
|
|
|
|
expect(szInfo).toBeTruthy();
|
|
|
|
expect(szInfo.label).toEqual('Eswatini');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('lookupCountryInfo() will find country info', () => {
|
|
|
|
expect(typeof lookupCountryInfo).toBe('function');
|
|
|
|
const eswatiniInfo = lookupCountryInfo('sz');
|
|
|
|
expect(eswatiniInfo.name).toEqual('Swaziland');
|
|
|
|
expect(eswatiniInfo.display).toEqual('Eswatini');
|
|
|
|
expect(eswatiniInfo.code).toEqual('sz');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('calling dupeCommonCountries() will duplicate the requested country info at start of array', () => {
|
|
|
|
expect(typeof dupeCommonCountries).toBe('function');
|
|
|
|
const countryInfoWithCommon = dupeCommonCountries(countryInfo, ['ca', 'gb']);
|
|
|
|
|
|
|
|
// test that the two entries have been added to the start of the array
|
|
|
|
const numCountries = countryInfo.length;
|
|
|
|
expect(countryInfoWithCommon.length).toEqual(numCountries + 2);
|
|
|
|
expect(countryInfoWithCommon[0]).toEqual({code: 'ca', name: 'Canada'});
|
|
|
|
expect(countryInfoWithCommon[1]).toEqual({code: 'gb', name: 'United Kingdom'});
|
|
|
|
|
|
|
|
// test that there are now two entries for Canada
|
|
|
|
const canadaItems = countryInfoWithCommon.reduce((acc, thisCountry) => (
|
|
|
|
thisCountry.code === 'ca' ? [...acc, thisCountry] : acc
|
|
|
|
), []);
|
|
|
|
expect(canadaItems.length).toEqual(2);
|
|
|
|
// test that there are now two entries for UK
|
|
|
|
const ukItems = countryInfoWithCommon.reduce((acc, thisCountry) => (
|
|
|
|
thisCountry.code === 'gb' ? [...acc, thisCountry] : acc
|
|
|
|
), []);
|
|
|
|
expect(ukItems.length).toEqual(2);
|
|
|
|
});
|
|
|
|
|
2019-11-05 18:31:13 -05:00
|
|
|
test('registrationCountryNameOptions object uses country names for both option label and option value', () => {
|
|
|
|
expect(typeof registrationCountryNameOptions).toBe('object');
|
2019-11-05 18:07:31 -05:00
|
|
|
// test that there is one option with label and value === 'Brazil'
|
2019-11-05 18:31:13 -05:00
|
|
|
const brazilOptions = registrationCountryNameOptions.reduce((acc, thisCountry) => (
|
2019-11-05 18:07:31 -05:00
|
|
|
(thisCountry.value === 'Brazil' && thisCountry.label === 'Brazil') ? [...acc, thisCountry] : acc
|
|
|
|
), []);
|
|
|
|
expect(brazilOptions.length).toEqual(1);
|
|
|
|
});
|
|
|
|
|
2019-11-05 18:31:13 -05:00
|
|
|
test('registrationCountryCodeOptions object uses country codes for option value', () => {
|
|
|
|
expect(typeof registrationCountryCodeOptions).toBe('object');
|
|
|
|
// test that there is one option with label and value === 'Brazil'
|
|
|
|
const brazilOptions = registrationCountryCodeOptions.reduce((acc, thisCountry) => (
|
|
|
|
(thisCountry.value === 'br' && thisCountry.label === 'Brazil') ? [...acc, thisCountry] : acc
|
|
|
|
), []);
|
|
|
|
expect(brazilOptions.length).toEqual(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('registrationCountryNameOptions object places USA and UK at start, with display name versions', () => {
|
|
|
|
expect(typeof registrationCountryNameOptions).toBe('object');
|
2019-08-05 23:33:05 -04:00
|
|
|
const numCountries = countryInfo.length;
|
|
|
|
|
|
|
|
// test that the two entries have been added to the start of the array, and that
|
|
|
|
// the name of the USA includes "America"
|
2019-11-05 18:31:13 -05:00
|
|
|
expect(registrationCountryNameOptions.length).toEqual(numCountries + 2);
|
|
|
|
expect(registrationCountryNameOptions[0]).toEqual({value: 'United States', label: 'United States of America'});
|
|
|
|
expect(registrationCountryNameOptions[1]).toEqual({value: 'United Kingdom', label: 'United Kingdom'});
|
2019-08-05 23:33:05 -04:00
|
|
|
|
|
|
|
// test that there are now two entries for USA
|
2019-11-05 18:31:13 -05:00
|
|
|
const usaOptions = registrationCountryNameOptions.reduce((acc, thisCountry) => (
|
2019-11-05 18:07:31 -05:00
|
|
|
thisCountry.value === 'United States' ? [...acc, thisCountry] : acc
|
2019-08-05 23:33:05 -04:00
|
|
|
), []);
|
|
|
|
expect(usaOptions.length).toEqual(2);
|
|
|
|
// test that there are now two entries for UK
|
2019-11-05 18:31:13 -05:00
|
|
|
const ukOptions = registrationCountryNameOptions.reduce((acc, thisCountry) => (
|
2019-11-05 18:07:31 -05:00
|
|
|
thisCountry.value === 'United Kingdom' ? [...acc, thisCountry] : acc
|
2019-08-05 23:33:05 -04:00
|
|
|
), []);
|
|
|
|
expect(ukOptions.length).toEqual(2);
|
2019-07-31 17:19:49 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('subdivisionOptions object should include correct info for sample country', () => {
|
|
|
|
expect(typeof subdivisionOptions).toBe('object');
|
|
|
|
// 71 subdivisions in Bangladesh
|
|
|
|
expect(subdivisionOptions.bd.length > 50).toEqual(true);
|
|
|
|
expect(subdivisionOptions.bd.length < 100).toEqual(true);
|
|
|
|
const nilphamari = subdivisionOptions.bd.find(item => item.label === 'Nilphamari');
|
|
|
|
expect(nilphamari).toEqual({
|
|
|
|
label: 'Nilphamari',
|
|
|
|
value: 'bd-46',
|
|
|
|
type: 'District'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|