Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QA item: use site locale for price range as default #949

Merged
merged 4 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 12 additions & 6 deletions static/js/formatters-internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,24 +525,30 @@ export function price(fieldValue = {}, locale) {
}

/**
* Returns a localized price range string for the given price range ($-$$$$) and country code (ISO format)
* Returns a localized price range string for the given price range ($-$$$$) and country code (ISO format).
* If country code is invalid or undefined, use locale of the site to determine the currency symbol.
* If all else fails, use the default priceRange with dollar sign.
* @param {string} defaultPriceRange The price range from LiveAPI entity
* @param {string} countrycode The country code from LiveAPI entity (e.g. profile.address.countryCode)
* @return {string} The price range with correct currency symbol formatting according to country code
*/
export function priceRange(defaultPriceRange, countryCode) {
if (!defaultPriceRange || !countryCode) {
if (!defaultPriceRange) {
console.warn(`No price range or country code given.`);
yen-tt marked this conversation as resolved.
Show resolved Hide resolved
return '';
}
const currencyCode = LocaleCurrency.getCurrency(countryCode);
if (currencyCode) {
const currencySymbol = getSymbolFromCurrency(currencyCode);
if (countryCode) {
const currencySymbol = getSymbolFromCurrency(LocaleCurrency.getCurrency(countryCode));
if (currencySymbol) {
return defaultPriceRange.replace(/\$/g, currencySymbol);
}
}
console.warn(`Unable to determine currency symbol from ISO country code ${countryCode}.`);
const locale = _getDocumentLocale();
yen-tt marked this conversation as resolved.
Show resolved Hide resolved
const currencySymbol = getSymbolFromCurrency(LocaleCurrency.getCurrency(locale));
if (currencySymbol) {
return defaultPriceRange.replace(/\$/g, currencySymbol);
}
console.warn(`Unable to determine currency symbol from ISO country code "${countryCode}" or locale "${locale}".`);
return defaultPriceRange;
}

Expand Down
15 changes: 14 additions & 1 deletion tests/static/js/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,20 @@ describe('Formatters', () => {
expect(price).toEqual('£');
});

it('Formats a price range in invalid input', () => {
it('Formats a price range in invalid country code, use page\'s locale', () => {
document.documentElement.lang = 'jp'
const price = Formatters.priceRange('$$$', 'IDK');
expect(price).toEqual('¥¥¥');
});

it('Formats a price range in undefined country code, use page\'s locale', () => {
document.documentElement.lang = 'jp'
const price = Formatters.priceRange('$$$', undefined);
expect(price).toEqual('¥¥¥');
});

it('Formats a price range in invalid country code and invalid page\'s locale', () => {
document.documentElement.lang = 'IDKK'
const price = Formatters.priceRange('$', 'IDK');
expect(price).toEqual('$');
});
Expand Down