mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Add PriceParser and UnitPrices and specs
This is in preparation for removing angular from the variant update page. Converted using https://www.codeconvert.ai/coffeescript-to-javascript-converter
This commit is contained in:
150
spec/javascripts/services/price_parser_test.js
Normal file
150
spec/javascripts/services/price_parser_test.js
Normal file
@@ -0,0 +1,150 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
*/
|
||||
|
||||
import PriceParse from "js/services/price_parser";
|
||||
|
||||
describe("PriceParser service", function () {
|
||||
let priceParser = null;
|
||||
|
||||
beforeEach(() => {
|
||||
priceParser = new PriceParse();
|
||||
});
|
||||
|
||||
describe("test internal method with Regexp", function () {
|
||||
describe("test replaceCommaByFinalPoint() method", function () {
|
||||
it("handle the default case (with two numbers after comma)", function () {
|
||||
expect(priceParser.replaceCommaByFinalPoint("1,00")).toEqual("1.00");
|
||||
});
|
||||
it("doesn't confuse with thousands separator", function () {
|
||||
expect(priceParser.replaceCommaByFinalPoint("1,000")).toEqual("1,000");
|
||||
});
|
||||
it("handle also when there is only one number after the decimal separator", function () {
|
||||
expect(priceParser.replaceCommaByFinalPoint("1,0")).toEqual("1.0");
|
||||
});
|
||||
});
|
||||
|
||||
describe("test removeThousandsSeparator() method", function () {
|
||||
it("handle the default case", function () {
|
||||
expect(priceParser.removeThousandsSeparator("1,000", ",")).toEqual("1000");
|
||||
expect(priceParser.removeThousandsSeparator("1,000,000", ",")).toEqual("1000000");
|
||||
});
|
||||
it("handle the case with decimal separator", function () {
|
||||
expect(priceParser.removeThousandsSeparator("1,000,000.00", ",")).toEqual("1000000.00");
|
||||
});
|
||||
it("handle the case when it is actually a decimal separator (and not a thousands one)", function () {
|
||||
expect(priceParser.removeThousandsSeparator("1,00", ",")).toEqual("1,00");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("with point as decimal separator and comma as thousands separator for I18n service", function () {
|
||||
beforeAll(() => {
|
||||
const mockedToCurrency = jest.fn();
|
||||
mockedToCurrency.mockImplementation((arg) => {
|
||||
if (arg == 0.1) {
|
||||
return "0.1";
|
||||
} else if (arg == 1000) {
|
||||
return "1,000";
|
||||
}
|
||||
});
|
||||
|
||||
global.I18n = { toCurrency: mockedToCurrency };
|
||||
});
|
||||
// (jest still doesn't have aroundEach https://github.com/jestjs/jest/issues/4543 )
|
||||
afterAll(() => {
|
||||
delete global.I18n;
|
||||
});
|
||||
|
||||
it("handle point as decimal separator", function () {
|
||||
expect(priceParser.parse("1.00")).toEqual(1.0);
|
||||
});
|
||||
|
||||
it("handle point as decimal separator", function () {
|
||||
expect(priceParser.parse("1.000")).toEqual(1.0);
|
||||
});
|
||||
|
||||
it("also handle comma as decimal separator", function () {
|
||||
expect(priceParser.parse("1,0")).toEqual(1.0);
|
||||
});
|
||||
|
||||
it("also handle comma as decimal separator", function () {
|
||||
expect(priceParser.parse("1,00")).toEqual(1.0);
|
||||
});
|
||||
|
||||
it("also handle comma as decimal separator", function () {
|
||||
expect(priceParser.parse("11,00")).toEqual(11.0);
|
||||
});
|
||||
|
||||
it("handle comma as decimal separator but not confusing with thousands separator", function () {
|
||||
expect(priceParser.parse("11,000")).toEqual(11000);
|
||||
});
|
||||
|
||||
it("handle point as decimal separator and comma as thousands separator", function () {
|
||||
expect(priceParser.parse("1,000,000.00")).toEqual(1000000);
|
||||
});
|
||||
|
||||
it("handle integer number", function () {
|
||||
expect(priceParser.parse("10")).toEqual(10);
|
||||
});
|
||||
|
||||
it("handle integer number with comma as thousands separator", function () {
|
||||
expect(priceParser.parse("1,000")).toEqual(1000);
|
||||
});
|
||||
|
||||
it("handle integer number with no thousands separator", function () {
|
||||
expect(priceParser.parse("1000")).toEqual(1000);
|
||||
});
|
||||
});
|
||||
|
||||
describe("with comma as decimal separator and final point as thousands separator for I18n service", function () {
|
||||
beforeAll(() => {
|
||||
const mockedToCurrency = jest.fn();
|
||||
mockedToCurrency.mockImplementation((arg) => {
|
||||
if (arg == 0.1) {
|
||||
return "0,1";
|
||||
} else if (arg == 1000) {
|
||||
return "1.000";
|
||||
}
|
||||
});
|
||||
|
||||
global.I18n = { toCurrency: mockedToCurrency };
|
||||
});
|
||||
// (jest still doesn't have aroundEach https://github.com/jestjs/jest/issues/4543 )
|
||||
afterAll(() => {
|
||||
delete global.I18n;
|
||||
});
|
||||
|
||||
it("handle comma as decimal separator", function () {
|
||||
expect(priceParser.parse("1,00")).toEqual(1.0);
|
||||
});
|
||||
|
||||
it("handle comma as decimal separator with one digit after the comma", function () {
|
||||
expect(priceParser.parse("11,0")).toEqual(11.0);
|
||||
});
|
||||
|
||||
it("handle comma as decimal separator with two digit after the comma", function () {
|
||||
expect(priceParser.parse("11,00")).toEqual(11.0);
|
||||
});
|
||||
|
||||
it("handle comma as decimal separator with three digit after the comma", function () {
|
||||
expect(priceParser.parse("11,000")).toEqual(11.0);
|
||||
});
|
||||
|
||||
it("also handle point as decimal separator", function () {
|
||||
expect(priceParser.parse("1.00")).toEqual(1.0);
|
||||
});
|
||||
|
||||
it("also handle point as decimal separator with integer part with two digits", function () {
|
||||
expect(priceParser.parse("11.00")).toEqual(11.0);
|
||||
});
|
||||
|
||||
it("handle point as decimal separator and final point as thousands separator", function () {
|
||||
expect(priceParser.parse("1.000.000,00")).toEqual(1000000);
|
||||
});
|
||||
|
||||
it("handle integer number", function () {
|
||||
expect(priceParser.parse("10")).toEqual(10);
|
||||
});
|
||||
});
|
||||
});
|
||||
170
spec/javascripts/services/unit_prices_test.js
Normal file
170
spec/javascripts/services/unit_prices_test.js
Normal file
@@ -0,0 +1,170 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
*/
|
||||
|
||||
import UnitPrices from "js/services/unit_prices";
|
||||
|
||||
describe("UnitPrices service", function () {
|
||||
let unitPrices = null;
|
||||
|
||||
beforeAll(() => {
|
||||
// Requires global var from page for VariantUnitManager
|
||||
global.ofn_available_units_sorted = {
|
||||
weight: {
|
||||
"1.0": { name: "g", system: "metric" },
|
||||
28.35: { name: "oz", system: "imperial" },
|
||||
453.6: { name: "lb", system: "imperial" },
|
||||
"1000.0": { name: "kg", system: "metric" },
|
||||
"1000000.0": { name: "T", system: "metric" },
|
||||
},
|
||||
volume: {
|
||||
0.001: { name: "mL", system: "metric" },
|
||||
"1.0": { name: "L", system: "metric" },
|
||||
"1000.0": { name: "kL", system: "metric" },
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
unitPrices = new UnitPrices();
|
||||
});
|
||||
|
||||
describe("get correct unit price duo unit/value for weight", function () {
|
||||
const unit_type = "weight";
|
||||
|
||||
it("with scale: 1", function () {
|
||||
const price = 1;
|
||||
const scale = 1;
|
||||
const unit_value = 1;
|
||||
expect(unitPrices.price(price, scale, unit_type, unit_value)).toEqual(1000);
|
||||
expect(unitPrices.unit(scale, unit_type)).toEqual("kg");
|
||||
});
|
||||
|
||||
it("with scale and unit_value: 1000", function () {
|
||||
const price = 1;
|
||||
const scale = 1000;
|
||||
const unit_value = 1000;
|
||||
expect(unitPrices.price(price, scale, unit_type, unit_value)).toEqual(1);
|
||||
expect(unitPrices.unit(scale, unit_type)).toEqual("kg");
|
||||
});
|
||||
|
||||
it("with scale: 1000 and unit_value: 2000", function () {
|
||||
const price = 1;
|
||||
const scale = 1000;
|
||||
const unit_value = 2000;
|
||||
expect(unitPrices.price(price, scale, unit_type, unit_value)).toEqual(0.5);
|
||||
expect(unitPrices.unit(scale, unit_type)).toEqual("kg");
|
||||
});
|
||||
|
||||
it("with price: 2", function () {
|
||||
const price = 2;
|
||||
const scale = 1;
|
||||
const unit_value = 1;
|
||||
expect(unitPrices.price(price, scale, unit_type, unit_value)).toEqual(2000);
|
||||
expect(unitPrices.unit(scale, unit_type)).toEqual("kg");
|
||||
});
|
||||
|
||||
it("with price: 2, scale and unit_value: 1000", function () {
|
||||
const price = 2;
|
||||
const scale = 1000;
|
||||
const unit_value = 1000;
|
||||
expect(unitPrices.price(price, scale, unit_type, unit_value)).toEqual(2);
|
||||
expect(unitPrices.unit(scale, unit_type)).toEqual("kg");
|
||||
});
|
||||
|
||||
it("with price: 2, scale: 1000 and unit_value: 2000", function () {
|
||||
const price = 2;
|
||||
const scale = 1000;
|
||||
const unit_value = 2000;
|
||||
expect(unitPrices.price(price, scale, unit_type, unit_value)).toEqual(1);
|
||||
expect(unitPrices.unit(scale, unit_type)).toEqual("kg");
|
||||
});
|
||||
|
||||
it("with price: 2, scale: 1000 and unit_value: 500", function () {
|
||||
const price = 2;
|
||||
const scale = 1000;
|
||||
const unit_value = 500;
|
||||
expect(unitPrices.price(price, scale, unit_type, unit_value)).toEqual(4);
|
||||
expect(unitPrices.unit(scale, unit_type)).toEqual("kg");
|
||||
});
|
||||
});
|
||||
|
||||
describe("get correct unit price duo unit/value for volume", function () {
|
||||
const unit_type = "volume";
|
||||
|
||||
it("with scale: 1", function () {
|
||||
const price = 1;
|
||||
const scale = 1;
|
||||
const unit_value = 1;
|
||||
expect(unitPrices.price(price, scale, unit_type, unit_value)).toEqual(1);
|
||||
expect(unitPrices.unit(scale, unit_type)).toEqual("L");
|
||||
});
|
||||
|
||||
it("with price: 2 and unit_value: 0.5", function () {
|
||||
const price = 2;
|
||||
const scale = 1;
|
||||
const unit_value = 0.5;
|
||||
expect(unitPrices.price(price, scale, unit_type, unit_value)).toEqual(4);
|
||||
expect(unitPrices.unit(scale, unit_type)).toEqual("L");
|
||||
});
|
||||
|
||||
it("with price: 2, scale: 0.001 and unit_value: 0.01", function () {
|
||||
const price = 2;
|
||||
const scale = 0.001;
|
||||
const unit_value = 0.01;
|
||||
expect(unitPrices.price(price, scale, unit_type, unit_value)).toEqual(200);
|
||||
expect(unitPrices.unit(scale, unit_type)).toEqual("L");
|
||||
});
|
||||
|
||||
it("with price: 20000, scale: 1000 and unit_value: 10000", function () {
|
||||
const price = 20000;
|
||||
const scale = 1000;
|
||||
const unit_value = 10000;
|
||||
expect(unitPrices.price(price, scale, unit_type, unit_value)).toEqual(2);
|
||||
expect(unitPrices.unit(scale, unit_type)).toEqual("L");
|
||||
});
|
||||
|
||||
it("with price: 2, scale: 1000 and unit_value: 10000 and variant_unit_name: box", function () {
|
||||
const price = 20000;
|
||||
const scale = 1000;
|
||||
const unit_value = 10000;
|
||||
const variant_unit_name = "Box";
|
||||
expect(unitPrices.price(price, scale, unit_type, unit_value, variant_unit_name)).toEqual(2);
|
||||
expect(unitPrices.unit(scale, unit_type, variant_unit_name)).toEqual("L");
|
||||
});
|
||||
});
|
||||
|
||||
describe("get correct unit price duo unit/value for items", function () {
|
||||
const unit_type = "items";
|
||||
const scale = null;
|
||||
|
||||
it("with price: 1 and unit_value: 1", function () {
|
||||
const price = 1;
|
||||
const unit_value = 1;
|
||||
expect(unitPrices.price(price, scale, unit_type, unit_value)).toEqual(1);
|
||||
expect(unitPrices.unit(scale, unit_type)).toEqual("item");
|
||||
});
|
||||
|
||||
it("with price: 1 and unit_value: 10", function () {
|
||||
const price = 1;
|
||||
const unit_value = 10;
|
||||
expect(unitPrices.price(price, scale, unit_type, unit_value)).toEqual(0.1);
|
||||
expect(unitPrices.unit(scale, unit_type)).toEqual("item");
|
||||
});
|
||||
|
||||
it("with price: 10 and unit_value: 1", function () {
|
||||
const price = 10;
|
||||
const unit_value = 1;
|
||||
expect(unitPrices.price(price, scale, unit_type, unit_value)).toEqual(10);
|
||||
expect(unitPrices.unit(scale, unit_type)).toEqual("item");
|
||||
});
|
||||
|
||||
it("with price: 10 and unit_value: 1 and variant_unit_name: box", function () {
|
||||
const price = 10;
|
||||
const unit_value = 1;
|
||||
const variant_unit_name = "Box";
|
||||
expect(unitPrices.price(price, scale, unit_type, unit_value, variant_unit_name)).toEqual(10);
|
||||
expect(unitPrices.unit(scale, unit_type, variant_unit_name)).toEqual("Box");
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user