Skip to content

Commit

Permalink
Navigation: ignore "state" part of location when undefined
Browse files Browse the repository at this point in the history
closes #366
  • Loading branch information
incognitojam committed Jul 28, 2023
1 parent 1acbb0d commit 05cbf25
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/components/Navigation/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ export function formatSearchAddress(item, full = true) {
res = houseNumber ? `${houseNumber} ${street}, ${city}`.trimStart() : `${street}, ${city}`;
if (full) {
const { stateCode, postalCode, countryName } = item.address;
res += `, ${stateCode} ${postalCode}, ${countryName}`;
res += ',';
if (stateCode) {
res += ` ${stateCode}`;
}
res += ` ${postalCode}, ${countryName}`;
}
}

Expand Down
35 changes: 32 additions & 3 deletions src/components/Navigation/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ describe('navigation formatting utils', () => {

// expected
name: 'Taco Bell',
address: '2011 Camino del Rio N, San Diego, CA 92108, United States',
fullAddress: '2011 Camino del Rio N, San Diego, CA 92108, United States',
address: '2011 Camino del Rio N, San Diego',
details: '2011 Camino del Rio N, San Diego',
searchList: ', 2011 Camino del Rio N, San Diego (0.8 mi)',
},
Expand All @@ -52,17 +53,45 @@ describe('navigation formatting utils', () => {

// expected
name: '1441 State St',
address: '1441 State St, San Diego, CA 92101-3421, United States',
fullAddress: '1441 State St, San Diego, CA 92101-3421, United States',
address: 'San Diego',
details: 'San Diego',
searchList: ', San Diego (0.8 mi)',
},
{
// from mapbox api
item: {
title: 'Taco Bell',
address: {
label: 'Taco Bell, Irlam Drive, Liverpool, L32 8, United Kingdom',
countryCode: 'GBR',
countryName: 'United Kingdom',
state: 'England',
countyCode: 'MSY',
county: 'Merseyside',
city: 'Liverpool',
district: 'Kirkby',
street: 'Irlam Drive',
postalCode: 'L32 8',
},
distance: 38190,
},

// expected
name: 'Taco Bell',
fullAddress: 'Irlam Drive, Liverpool, L32 8, United Kingdom',
address: 'Irlam Drive, Liverpool',
details: 'Irlam Drive, Liverpool',
searchList: ', Irlam Drive, Liverpool (23.7 mi)',
},
];

testCases.forEach((testCase) => {
const { item } = testCase;

expect(Utils.formatSearchName(item)).toBe(testCase.name);
expect(Utils.formatSearchAddress(item)).toBe(testCase.address);
expect(Utils.formatSearchAddress(item, true)).toBe(testCase.fullAddress);
expect(Utils.formatSearchAddress(item, false)).toBe(testCase.address);
expect(Utils.formatSearchDetails(item)).toBe(testCase.details);
expect(Utils.formatSearchList(item)).toBe(testCase.searchList);
});
Expand Down

0 comments on commit 05cbf25

Please sign in to comment.