mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Add localizeCurrency and specs
This commit is contained in:
63
spec/javascripts/services/localize_currency_test.js
Normal file
63
spec/javascripts/services/localize_currency_test.js
Normal file
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
*/
|
||||
|
||||
import localizeCurrency from "js/services/localize_currency";
|
||||
|
||||
describe("convert number to localised currency", function () {
|
||||
beforeAll(() => {
|
||||
const mockedToCurrency = jest.fn();
|
||||
mockedToCurrency.mockImplementation((amount, options) => {
|
||||
if (options.format == "%n %u") {
|
||||
return `${amount.toFixed(options.precision)}${options.unit}`;
|
||||
} else {
|
||||
return `${options.unit}${amount.toFixed(options.precision)}`;
|
||||
}
|
||||
});
|
||||
|
||||
global.I18n = { toCurrency: mockedToCurrency };
|
||||
|
||||
// Requires global var from page
|
||||
global.ofn_currency_config = {
|
||||
symbol: "$",
|
||||
symbol_position: "before",
|
||||
currency: "D",
|
||||
hide_cents: "false",
|
||||
};
|
||||
});
|
||||
// (jest still doesn't have aroundEach https://github.com/jestjs/jest/issues/4543 )
|
||||
afterAll(() => {
|
||||
delete global.I18n;
|
||||
});
|
||||
|
||||
it("adds decimal fraction to an amount", function () {
|
||||
expect(localizeCurrency(10)).toEqual("$10.00");
|
||||
});
|
||||
|
||||
it("handles an existing fraction", function () {
|
||||
expect(localizeCurrency(9.9)).toEqual("$9.90");
|
||||
});
|
||||
|
||||
it("can use any currency symbol", function () {
|
||||
global.ofn_currency_config.symbol = "£";
|
||||
expect(localizeCurrency(404.04)).toEqual("£404.04");
|
||||
});
|
||||
|
||||
it("can place symbols after the amount", function () {
|
||||
global.ofn_currency_config.symbol = "$";
|
||||
global.ofn_currency_config.symbol_position = "after";
|
||||
expect(localizeCurrency(333.3)).toEqual("333.30$");
|
||||
});
|
||||
|
||||
it("can add a currency string", function () {
|
||||
global.ofn_currency_config.display_currency = true;
|
||||
global.ofn_currency_config.symbol_position = "before";
|
||||
expect(localizeCurrency(5)).toEqual("$5.00 D");
|
||||
});
|
||||
|
||||
it("can hide cents", function () {
|
||||
global.ofn_currency_config.display_currency = false;
|
||||
global.ofn_currency_config.hide_cents = "true";
|
||||
expect(localizeCurrency(5)).toEqual("$5");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user