update local currency code

This commit is contained in:
Matthew Piatetsky
2019-06-03 17:06:23 -04:00
parent ad77392f8d
commit ce5b976f5e
8 changed files with 88 additions and 64 deletions

View File

@@ -1,2 +1,3 @@
<div id="currency_data" value='{"CAN": {"rate": 2.2, "code": "CAD", "symbol": "$"}}'></div>
<input type="submit" name="verified_mode" value="Pursue a Verified Certificate ($100 USD)">
<button type="submit" class="no-discount" name="verified_mode"><span>Pursue a Verified Certificate</span>(<span class="upgrade-price-string">$100 USD</span>)</button>
<button type="submit" class="discount" name="verified_mode"><span>Pursue a Verified Certificate</span>(<span class="upgrade-price-string">$90 USD</span> <del><span class="upgrade-price-string">$100 USD</span></del>)</button>

View File

@@ -4,17 +4,28 @@ import $ from 'jquery'; // eslint-disable-line import/extensions
export class Currency { // eslint-disable-line import/prefer-default-export
setPrice() {
editText(price) {
const l10nCookie = this.countryL10nData;
const lmsregex = /(\$)(\d*)( USD)/g;
const price = $('input[name="verified_mode"]').filter(':visible')[0];
const regexMatch = lmsregex.exec(price.value);
const dollars = parseFloat(regexMatch[2]);
const converted = dollars * l10nCookie.rate;
const string = `${l10nCookie.symbol}${Math.round(converted)} ${l10nCookie.code}`;
// Use regex to change displayed price on track selection
// based on edx-price-l10n cookie currency_data
price.value = price.value.replace(regexMatch[0], string);
const lmsregex = /(\$)([\d|.]*)( USD)/g;
const priceText = price.text();
const regexMatch = lmsregex.exec(priceText);
if (regexMatch) {
const currentPrice = regexMatch[2];
const dollars = parseFloat(currentPrice);
const newPrice = dollars * l10nCookie.rate;
const newPriceString = `${l10nCookie.symbol}${Math.round(newPrice)} ${l10nCookie.code}`;
// Change displayed price based on edx-price-l10n cookie currency_data
price.text(newPriceString);
}
}
setPrice() {
$('.upgrade-price-string').each((i, price) => {
// When the button includes two prices (discounted and previous)
// we call the method twice, since it modifies one price at a time.
// Could also be used to modify all prices on any page
this.editText($(price));
});
}
getCountry() {

View File

@@ -36,12 +36,17 @@ describe('Currency factory', () => {
it('when location is the default (US)', () => {
$.cookie('edx-price-l10n', '{"rate":1,"code":"USD","symbol":"$","countryCode":"US"}', { path: '/' });
currency = new Currency();
expect($('input[name="verified_mode"]').filter(':visible')[0].value).toEqual('Pursue a Verified Certificate ($100 USD)');
expect($('[name="verified_mode"].no-discount').filter(':visible').text()).toEqual('Pursue a Verified Certificate($100 USD)');
});
it('when cookie is set to a different country', () => {
$.cookie('edx-price-l10n', '{"rate":2.2,"code":"CAD","symbol":"$","countryCode":"CAN"}', { expires: 1 });
currency = new Currency();
expect($('input[name="verified_mode"]').filter(':visible')[0].value).toEqual('Pursue a Verified Certificate ($220 CAD)');
expect($('[name="verified_mode"].no-discount').filter(':visible').text()).toEqual('Pursue a Verified Certificate($220 CAD)');
});
it('when cookie is set to a different country with a discount', () => {
$.cookie('edx-price-l10n', '{"rate":2.2,"code":"CAD","symbol":"$","countryCode":"CAN"}', { expires: 1 });
currency = new Currency();
expect($('[name="verified_mode"].discount').filter(':visible').text()).toEqual('Pursue a Verified Certificate($198 CAD $220 CAD)');
});
});
});