Skip to content

Commit dc79eb5

Browse files
authored
Merge pull request #3974 from benjiwheeler/move-to-country-data-lib
move country name lookup to library
2 parents 7b76c19 + d388eef commit dc79eb5

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

src/components/registration/steps.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ class DemographicsStep extends React.Component {
456456
// look up country name using user's country code selection ('us' -> 'United States')
457457
getCountryName (values) {
458458
if (values.countryCode) {
459-
const countryInfo = countryData.lookupCountryInfo(values.countryCode);
459+
const countryInfo = countryData.lookupCountryByCode(values.countryCode);
460460
if (countryInfo) {
461461
return countryInfo.name;
462462
}
@@ -466,7 +466,7 @@ class DemographicsStep extends React.Component {
466466
// look up country code from country label ('United States' -> 'us')
467467
// if `countryName` is not found, including if it's null or undefined, then this function will return undefined.
468468
getCountryCode (countryName) {
469-
const country = countryData.countryInfo.find(countryItem => countryItem.name === countryName);
469+
const country = countryData.lookupCountryByName(countryName);
470470
return country && country.code;
471471
}
472472
handleValidSubmit (formData) {

src/lib/country-data.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,10 +1036,14 @@ const countryOptions = module.exports.countryOptions = (startingCountryInfo, val
10361036
))
10371037
);
10381038

1039-
module.exports.lookupCountryInfo = countryCode => (
1039+
module.exports.lookupCountryByCode = countryCode => (
10401040
countryInfo.find(country => country.code === countryCode)
10411041
);
10421042

1043+
module.exports.lookupCountryByName = countryName => (
1044+
countryInfo.find(country => country.name === countryName)
1045+
);
1046+
10431047
/**
10441048
* Function dupeCommonCountries():
10451049
* takes startingCountryInfo, and duplicates any number of its country labels

test/unit/lib/country-data.test.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const {
22
countryInfo,
33
countryOptions,
4-
lookupCountryInfo,
4+
lookupCountryByCode,
5+
lookupCountryByName,
56
dupeCommonCountries,
67
registrationCountryCodeOptions,
78
registrationCountryNameOptions,
@@ -45,9 +46,17 @@ describe('unit test lib/country-data.js', () => {
4546
expect(szInfo.label).toEqual('Eswatini');
4647
});
4748

48-
test('lookupCountryInfo() will find country info', () => {
49-
expect(typeof lookupCountryInfo).toBe('function');
50-
const eswatiniInfo = lookupCountryInfo('sz');
49+
test('lookupCountryByCode() will find country info', () => {
50+
expect(typeof lookupCountryByCode).toBe('function');
51+
const eswatiniInfo = lookupCountryByCode('sz');
52+
expect(eswatiniInfo.name).toEqual('Swaziland');
53+
expect(eswatiniInfo.display).toEqual('Eswatini');
54+
expect(eswatiniInfo.code).toEqual('sz');
55+
});
56+
57+
test('lookupCountryByName() will find country info', () => {
58+
expect(typeof lookupCountryByName).toBe('function');
59+
const eswatiniInfo = lookupCountryByName('Swaziland');
5160
expect(eswatiniInfo.name).toEqual('Swaziland');
5261
expect(eswatiniInfo.display).toEqual('Eswatini');
5362
expect(eswatiniInfo.code).toEqual('sz');

0 commit comments

Comments
 (0)