Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/registration/steps.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ class DemographicsStep extends React.Component {
// look up country name using user's country code selection ('us' -> 'United States')
getCountryName (values) {
if (values.countryCode) {
const countryInfo = countryData.lookupCountryInfo(values.countryCode);
const countryInfo = countryData.lookupCountryByCode(values.countryCode);
if (countryInfo) {
return countryInfo.name;
}
Expand All @@ -466,7 +466,7 @@ class DemographicsStep extends React.Component {
// look up country code from country label ('United States' -> 'us')
// if `countryName` is not found, including if it's null or undefined, then this function will return undefined.
getCountryCode (countryName) {
const country = countryData.countryInfo.find(countryItem => countryItem.name === countryName);
const country = countryData.lookupCountryByName(countryName);
return country && country.code;
}
handleValidSubmit (formData) {
Expand Down
6 changes: 5 additions & 1 deletion src/lib/country-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -1036,10 +1036,14 @@ const countryOptions = module.exports.countryOptions = (startingCountryInfo, val
))
);

module.exports.lookupCountryInfo = countryCode => (
module.exports.lookupCountryByCode = countryCode => (
countryInfo.find(country => country.code === countryCode)
);

module.exports.lookupCountryByName = countryName => (
countryInfo.find(country => country.name === countryName)
);

/**
* Function dupeCommonCountries():
* takes startingCountryInfo, and duplicates any number of its country labels
Expand Down
17 changes: 13 additions & 4 deletions test/unit/lib/country-data.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const {
countryInfo,
countryOptions,
lookupCountryInfo,
lookupCountryByCode,
lookupCountryByName,
dupeCommonCountries,
registrationCountryCodeOptions,
registrationCountryNameOptions,
Expand Down Expand Up @@ -45,9 +46,17 @@ describe('unit test lib/country-data.js', () => {
expect(szInfo.label).toEqual('Eswatini');
});

test('lookupCountryInfo() will find country info', () => {
expect(typeof lookupCountryInfo).toBe('function');
const eswatiniInfo = lookupCountryInfo('sz');
test('lookupCountryByCode() will find country info', () => {
expect(typeof lookupCountryByCode).toBe('function');
const eswatiniInfo = lookupCountryByCode('sz');
expect(eswatiniInfo.name).toEqual('Swaziland');
expect(eswatiniInfo.display).toEqual('Eswatini');
expect(eswatiniInfo.code).toEqual('sz');
});

test('lookupCountryByName() will find country info', () => {
expect(typeof lookupCountryByName).toBe('function');
const eswatiniInfo = lookupCountryByName('Swaziland');
expect(eswatiniInfo.name).toEqual('Swaziland');
expect(eswatiniInfo.display).toEqual('Eswatini');
expect(eswatiniInfo.code).toEqual('sz');
Expand Down