mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-11 18:26:50 +00:00
Prettify javascript
Also update .prettierignore so that spec files get prettified as well
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
*.yaml
|
*.yaml
|
||||||
*.json
|
*.json
|
||||||
*.html
|
*.html
|
||||||
|
**/*.rb
|
||||||
|
|
||||||
# JS
|
# JS
|
||||||
# Enabled: app/webpacker/controllers/*.js and app/webpacker/packs/*.js
|
# Enabled: app/webpacker/controllers/*.js and app/webpacker/packs/*.js
|
||||||
@@ -27,6 +28,5 @@ postcss.config.js
|
|||||||
/coverage/
|
/coverage/
|
||||||
/engines/
|
/engines/
|
||||||
/public/
|
/public/
|
||||||
/spec/
|
|
||||||
/tmp/
|
/tmp/
|
||||||
/vendor/
|
/vendor/
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ export default class VariantController extends Controller {
|
|||||||
this.variantUnitScale = this.element.querySelector('[name$="[variant_unit_scale]"]');
|
this.variantUnitScale = this.element.querySelector('[name$="[variant_unit_scale]"]');
|
||||||
this.variantUnitName = this.element.querySelector('[name$="[variant_unit_name]"]');
|
this.variantUnitName = this.element.querySelector('[name$="[variant_unit_name]"]');
|
||||||
this.variantUnitWithScale = this.element.querySelector('[name$="[variant_unit_with_scale]"]');
|
this.variantUnitWithScale = this.element.querySelector('[name$="[variant_unit_with_scale]"]');
|
||||||
|
|
||||||
// on variant_unit_with_scale changed; update variant_unit and variant_unit_scale
|
// on variant_unit_with_scale changed; update variant_unit and variant_unit_scale
|
||||||
this.variantUnitWithScale.addEventListener("change", this.#updateUnitAndScale.bind(this), {
|
this.variantUnitWithScale.addEventListener("change", this.#updateUnitAndScale.bind(this), {
|
||||||
passive: true,
|
passive: true,
|
||||||
@@ -87,7 +87,7 @@ export default class VariantController extends Controller {
|
|||||||
variant_unit_name: this.variantUnitName.value,
|
variant_unit_name: this.variantUnitName.value,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract variant_unit and variant_unit_scale from dropdown variant_unit_with_scale,
|
// Extract variant_unit and variant_unit_scale from dropdown variant_unit_with_scale,
|
||||||
// and update hidden product fields
|
// and update hidden product fields
|
||||||
#updateUnitAndScale(event) {
|
#updateUnitAndScale(event) {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ export default class OptionValueNamer {
|
|||||||
|
|
||||||
name() {
|
name() {
|
||||||
const [value, unit] = this.option_value_value_unit();
|
const [value, unit] = this.option_value_value_unit();
|
||||||
const separator = this.value_scaled() ? '' : ' ';
|
const separator = this.value_scaled() ? "" : " ";
|
||||||
const name_fields = [];
|
const name_fields = [];
|
||||||
if (value && unit) {
|
if (value && unit) {
|
||||||
name_fields.push(`${value}${separator}${unit}`);
|
name_fields.push(`${value}${separator}${unit}`);
|
||||||
@@ -20,7 +20,7 @@ export default class OptionValueNamer {
|
|||||||
if (this.variant.unit_description) {
|
if (this.variant.unit_description) {
|
||||||
name_fields.push(this.variant.unit_description);
|
name_fields.push(this.variant.unit_description);
|
||||||
}
|
}
|
||||||
return name_fields.join(' ');
|
return name_fields.join(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
value_scaled() {
|
value_scaled() {
|
||||||
@@ -55,7 +55,7 @@ export default class OptionValueNamer {
|
|||||||
}
|
}
|
||||||
return I18n.t(["inflections", unit_key], {
|
return I18n.t(["inflections", unit_key], {
|
||||||
count: count,
|
count: count,
|
||||||
defaultValue: unit_name
|
defaultValue: unit_name,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,17 +84,20 @@ export default class OptionValueNamer {
|
|||||||
// If there is none available where this is true, use the smallest
|
// If there is none available where this is true, use the smallest
|
||||||
// available unit.
|
// available unit.
|
||||||
const scales = this.variantUnitManager.compatibleUnitScales(
|
const scales = this.variantUnitManager.compatibleUnitScales(
|
||||||
this.variant.variant_unit_scale, this.variant.variant_unit
|
this.variant.variant_unit_scale,
|
||||||
|
this.variant.variant_unit,
|
||||||
);
|
);
|
||||||
const variantUnitValue = this.variant.unit_value;
|
const variantUnitValue = this.variant.unit_value;
|
||||||
|
|
||||||
// sets largestScale = last element in filtered scales array
|
// sets largestScale = last element in filtered scales array
|
||||||
const largestScale = scales.filter(s => variantUnitValue / s >= 1).slice(-1)[0];
|
const largestScale = scales.filter((s) => variantUnitValue / s >= 1).slice(-1)[0];
|
||||||
if (largestScale) {
|
if (largestScale) {
|
||||||
return [largestScale, this.variantUnitManager.getUnitName(largestScale, this.variant.variant_unit)];
|
return [
|
||||||
|
largestScale,
|
||||||
|
this.variantUnitManager.getUnitName(largestScale, this.variant.variant_unit),
|
||||||
|
];
|
||||||
} else {
|
} else {
|
||||||
return [scales[0], this.variantUnitManager.getUnitName(scales[0], this.variant.variant_unit)];
|
return [scales[0], this.variantUnitManager.getUnitName(scales[0], this.variant.variant_unit)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,133 +7,153 @@ import OptionValueNamer from "js/services/option_value_namer";
|
|||||||
describe("OptionValueNamer", () => {
|
describe("OptionValueNamer", () => {
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
// Requires global var from page
|
// Requires global var from page
|
||||||
global.ofn_available_units_sorted = {"weight":{"1.0":{"name":"g","system":"metric"},"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"},"4.54609":{"name":"gal","system":"imperial"},"1000.0":{"name":"kL","system":"metric"}}};
|
global.ofn_available_units_sorted = {
|
||||||
})
|
weight: {
|
||||||
|
"1.0": { name: "g", system: "metric" },
|
||||||
|
"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" },
|
||||||
|
4.54609: { name: "gal", system: "imperial" },
|
||||||
|
"1000.0": { name: "kL", system: "metric" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
describe("generating option value name", function() {
|
describe("generating option value name", function () {
|
||||||
var v, namer;
|
var v, namer;
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
v = {};
|
v = {};
|
||||||
var ofn_available_units_sorted = ofn_available_units_sorted;
|
var ofn_available_units_sorted = ofn_available_units_sorted;
|
||||||
namer = new OptionValueNamer(v);
|
namer = new OptionValueNamer(v);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("when unit is blank (empty items name)", function() {
|
it("when unit is blank (empty items name)", function () {
|
||||||
jest.spyOn(namer, "value_scaled").mockImplementation(() => true);
|
jest.spyOn(namer, "value_scaled").mockImplementation(() => true);
|
||||||
jest.spyOn(namer, "option_value_value_unit").mockImplementation(() => ["value", ""]);
|
jest.spyOn(namer, "option_value_value_unit").mockImplementation(() => ["value", ""]);
|
||||||
expect(namer.name()).toBe("value");
|
expect(namer.name()).toBe("value");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("when description is blank", function() {
|
it("when description is blank", function () {
|
||||||
v.unit_description = null;
|
v.unit_description = null;
|
||||||
jest.spyOn(namer, "value_scaled").mockImplementation(() => true);
|
jest.spyOn(namer, "value_scaled").mockImplementation(() => true);
|
||||||
jest.spyOn(namer, "option_value_value_unit").mockImplementation(() => ["value", "unit"]);
|
jest.spyOn(namer, "option_value_value_unit").mockImplementation(() => ["value", "unit"]);
|
||||||
expect(namer.name()).toBe("valueunit");
|
expect(namer.name()).toBe("valueunit");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("when description is present", function() {
|
it("when description is present", function () {
|
||||||
v.unit_description = 'desc';
|
v.unit_description = "desc";
|
||||||
jest.spyOn(namer, "option_value_value_unit").mockImplementation(() => ["value", "unit"]);
|
jest.spyOn(namer, "option_value_value_unit").mockImplementation(() => ["value", "unit"]);
|
||||||
jest.spyOn(namer, "value_scaled").mockImplementation(() => true);
|
jest.spyOn(namer, "value_scaled").mockImplementation(() => true);
|
||||||
expect(namer.name()).toBe("valueunit desc");
|
expect(namer.name()).toBe("valueunit desc");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("when value is blank and description is present", function() {
|
it("when value is blank and description is present", function () {
|
||||||
v.unit_description = 'desc';
|
v.unit_description = "desc";
|
||||||
jest.spyOn(namer, "option_value_value_unit").mockImplementation(() => [null, null]);
|
jest.spyOn(namer, "option_value_value_unit").mockImplementation(() => [null, null]);
|
||||||
jest.spyOn(namer, "value_scaled").mockImplementation(() => true);
|
jest.spyOn(namer, "value_scaled").mockImplementation(() => true);
|
||||||
expect(namer.name()).toBe("desc");
|
expect(namer.name()).toBe("desc");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("spaces value and unit when value is unscaled", function() {
|
it("spaces value and unit when value is unscaled", function () {
|
||||||
v.unit_description = null;
|
v.unit_description = null;
|
||||||
jest.spyOn(namer, "option_value_value_unit").mockImplementation(() => ["value", "unit"]);
|
jest.spyOn(namer, "option_value_value_unit").mockImplementation(() => ["value", "unit"]);
|
||||||
jest.spyOn(namer, "value_scaled").mockImplementation(() => false);
|
jest.spyOn(namer, "value_scaled").mockImplementation(() => false);
|
||||||
expect(namer.name()).toBe("value unit");
|
expect(namer.name()).toBe("value unit");
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("determining if a variant's value is scaled", function() {
|
describe("determining if a variant's value is scaled", function () {
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
v = {};
|
v = {};
|
||||||
namer = new OptionValueNamer(v);
|
namer = new OptionValueNamer(v);
|
||||||
});
|
});
|
||||||
it("returns true when the product has a scale", function() {
|
it("returns true when the product has a scale", function () {
|
||||||
v.variant_unit_scale = 1000;
|
v.variant_unit_scale = 1000;
|
||||||
expect(namer.value_scaled()).toBe(true);
|
expect(namer.value_scaled()).toBe(true);
|
||||||
});
|
});
|
||||||
it("returns false otherwise", function() {
|
it("returns false otherwise", function () {
|
||||||
expect(namer.value_scaled()).toBe(false);
|
expect(namer.value_scaled()).toBe(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("generating option value's value and unit", function() {
|
describe("generating option value's value and unit", function () {
|
||||||
var v, namer;
|
var v, namer;
|
||||||
|
|
||||||
// Mock I18n. TODO: moved to a shared helper
|
// Mock I18n. TODO: moved to a shared helper
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
const mockedT = jest.fn();
|
const mockedT = jest.fn();
|
||||||
mockedT.mockImplementation((string, opts) => (string + ', ' + JSON.stringify(opts)));
|
mockedT.mockImplementation((string, opts) => string + ", " + JSON.stringify(opts));
|
||||||
|
|
||||||
global.I18n = { t: mockedT };
|
global.I18n = { t: mockedT };
|
||||||
})
|
});
|
||||||
// (jest still doesn't have aroundEach https://github.com/jestjs/jest/issues/4543 )
|
// (jest still doesn't have aroundEach https://github.com/jestjs/jest/issues/4543 )
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
delete global.I18n;
|
delete global.I18n;
|
||||||
})
|
});
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
v = {};
|
v = {};
|
||||||
namer = new OptionValueNamer(v);
|
namer = new OptionValueNamer(v);
|
||||||
});
|
});
|
||||||
it("generates simple values", function() {
|
it("generates simple values", function () {
|
||||||
v.variant_unit = 'weight';
|
v.variant_unit = "weight";
|
||||||
v.variant_unit_scale = 1.0;
|
v.variant_unit_scale = 1.0;
|
||||||
v.unit_value = 100;
|
v.unit_value = 100;
|
||||||
expect(namer.option_value_value_unit()).toEqual([100, 'g']);
|
expect(namer.option_value_value_unit()).toEqual([100, "g"]);
|
||||||
});
|
});
|
||||||
it("generates values when unit value is non-integer", function() {
|
it("generates values when unit value is non-integer", function () {
|
||||||
v.variant_unit = 'weight';
|
v.variant_unit = "weight";
|
||||||
v.variant_unit_scale = 1.0;
|
v.variant_unit_scale = 1.0;
|
||||||
v.unit_value = 123.45;
|
v.unit_value = 123.45;
|
||||||
expect(namer.option_value_value_unit()).toEqual([123.45, 'g']);
|
expect(namer.option_value_value_unit()).toEqual([123.45, "g"]);
|
||||||
});
|
});
|
||||||
it("returns a value of 1 when unit value equals the scale", function() {
|
it("returns a value of 1 when unit value equals the scale", function () {
|
||||||
v.variant_unit = 'weight';
|
v.variant_unit = "weight";
|
||||||
v.variant_unit_scale = 1000.0;
|
v.variant_unit_scale = 1000.0;
|
||||||
v.unit_value = 1000.0;
|
v.unit_value = 1000.0;
|
||||||
expect(namer.option_value_value_unit()).toEqual([1, 'kg']);
|
expect(namer.option_value_value_unit()).toEqual([1, "kg"]);
|
||||||
});
|
});
|
||||||
it("generates values for all weight scales", function() {
|
it("generates values for all weight scales", function () {
|
||||||
[[1.0, 'g'], [1000.0, 'kg'], [1000000.0, 'T']].forEach(([scale, unit]) => {
|
[
|
||||||
v.variant_unit = 'weight';
|
[1.0, "g"],
|
||||||
|
[1000.0, "kg"],
|
||||||
|
[1000000.0, "T"],
|
||||||
|
].forEach(([scale, unit]) => {
|
||||||
|
v.variant_unit = "weight";
|
||||||
v.variant_unit_scale = scale;
|
v.variant_unit_scale = scale;
|
||||||
v.unit_value = 100 * scale;
|
v.unit_value = 100 * scale;
|
||||||
expect(namer.option_value_value_unit()).toEqual([100, unit]);
|
expect(namer.option_value_value_unit()).toEqual([100, unit]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("generates values for all volume scales", function() {
|
it("generates values for all volume scales", function () {
|
||||||
[[0.001, 'mL'], [1.0, 'L'], [1000.0, 'kL']].forEach(([scale, unit]) => {
|
[
|
||||||
v.variant_unit = 'volume';
|
[0.001, "mL"],
|
||||||
|
[1.0, "L"],
|
||||||
|
[1000.0, "kL"],
|
||||||
|
].forEach(([scale, unit]) => {
|
||||||
|
v.variant_unit = "volume";
|
||||||
v.variant_unit_scale = scale;
|
v.variant_unit_scale = scale;
|
||||||
v.unit_value = 100 * scale;
|
v.unit_value = 100 * scale;
|
||||||
expect(namer.option_value_value_unit()).toEqual([100, unit]);
|
expect(namer.option_value_value_unit()).toEqual([100, unit]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("generates right values for volume with rounded values", function() {
|
it("generates right values for volume with rounded values", function () {
|
||||||
var unit;
|
var unit;
|
||||||
unit = 'L';
|
unit = "L";
|
||||||
v.variant_unit = 'volume';
|
v.variant_unit = "volume";
|
||||||
v.variant_unit_scale = 1.0;
|
v.variant_unit_scale = 1.0;
|
||||||
v.unit_value = 0.7;
|
v.unit_value = 0.7;
|
||||||
expect(namer.option_value_value_unit()).toEqual([700, 'mL']);
|
expect(namer.option_value_value_unit()).toEqual([700, "mL"]);
|
||||||
});
|
});
|
||||||
it("chooses the correct scale when value is very small", function() {
|
it("chooses the correct scale when value is very small", function () {
|
||||||
v.variant_unit = 'volume';
|
v.variant_unit = "volume";
|
||||||
v.variant_unit_scale = 0.001;
|
v.variant_unit_scale = 0.001;
|
||||||
v.unit_value = 0.0001;
|
v.unit_value = 0.0001;
|
||||||
expect(namer.option_value_value_unit()).toEqual([0.1, 'mL']);
|
expect(namer.option_value_value_unit()).toEqual([0.1, "mL"]);
|
||||||
});
|
});
|
||||||
it("generates values for item units", function() {
|
it("generates values for item units", function () {
|
||||||
//TODO
|
//TODO
|
||||||
// %w(packet box).each do |unit|
|
// %w(packet box).each do |unit|
|
||||||
// p = double(:product, variant_unit: 'items', variant_unit_scale: nil, variant_unit_name: unit)
|
// p = double(:product, variant_unit: 'items', variant_unit_scale: nil, variant_unit_name: unit)
|
||||||
@@ -141,17 +161,17 @@ describe("OptionValueNamer", () => {
|
|||||||
// v.stub(:unit_value) { 100 }
|
// v.stub(:unit_value) { 100 }
|
||||||
// subject.option_value_value_unit.should == [100, unit.pluralize]
|
// subject.option_value_value_unit.should == [100, unit.pluralize]
|
||||||
});
|
});
|
||||||
it("generates singular values for item units when value is 1", function() {
|
it("generates singular values for item units when value is 1", function () {
|
||||||
v.variant_unit = 'items';
|
v.variant_unit = "items";
|
||||||
v.variant_unit_scale = null;
|
v.variant_unit_scale = null;
|
||||||
v.variant_unit_name = 'packet';
|
v.variant_unit_name = "packet";
|
||||||
v.unit_value = 1;
|
v.unit_value = 1;
|
||||||
expect(namer.option_value_value_unit()).toEqual([1, 'packet']);
|
expect(namer.option_value_value_unit()).toEqual([1, "packet"]);
|
||||||
});
|
});
|
||||||
it("returns [null, null] when unit value is not set", function() {
|
it("returns [null, null] when unit value is not set", function () {
|
||||||
v.variant_unit = 'items';
|
v.variant_unit = "items";
|
||||||
v.variant_unit_scale = null;
|
v.variant_unit_scale = null;
|
||||||
v.variant_unit_name = 'foo';
|
v.variant_unit_name = "foo";
|
||||||
v.unit_value = null;
|
v.unit_value = null;
|
||||||
expect(namer.option_value_value_unit()).toEqual([null, null]);
|
expect(namer.option_value_value_unit()).toEqual([null, null]);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,28 +5,27 @@
|
|||||||
import { Application } from "stimulus";
|
import { Application } from "stimulus";
|
||||||
import variant_controller from "controllers/variant_controller";
|
import variant_controller from "controllers/variant_controller";
|
||||||
|
|
||||||
|
|
||||||
describe("VariantController", () => {
|
describe("VariantController", () => {
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
// Requires global var from page
|
// Requires global var from page
|
||||||
global.ofn_available_units_sorted = {
|
global.ofn_available_units_sorted = {
|
||||||
"weight": {
|
weight: {
|
||||||
"1.0":{"name":"g","system":"metric"},
|
"1.0": { name: "g", system: "metric" },
|
||||||
"1000.0":{"name":"kg","system":"metric"},
|
"1000.0": { name: "kg", system: "metric" },
|
||||||
"1000000.0":{"name":"T","system":"metric"}
|
"1000000.0": { name: "T", system: "metric" },
|
||||||
|
},
|
||||||
|
volume: {
|
||||||
|
0.001: { name: "mL", system: "metric" },
|
||||||
|
"1.0": { name: "L", system: "metric" },
|
||||||
|
4.54609: { name: "gal", system: "imperial" },
|
||||||
|
"1000.0": { name: "kL", system: "metric" },
|
||||||
},
|
},
|
||||||
"volume":{
|
|
||||||
"0.001":{"name":"mL","system":"metric"},
|
|
||||||
"1.0":{"name":"L","system":"metric"},
|
|
||||||
"4.54609":{"name":"gal","system":"imperial"},
|
|
||||||
"1000.0":{"name":"kL","system":"metric"}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const mockedT = jest.fn();
|
const mockedT = jest.fn();
|
||||||
mockedT.mockImplementation((string, opts) => (string + ', ' + JSON.stringify(opts)));
|
mockedT.mockImplementation((string, opts) => string + ", " + JSON.stringify(opts));
|
||||||
|
|
||||||
global.I18n = { t: mockedT };
|
global.I18n = { t: mockedT };
|
||||||
|
|
||||||
const application = Application.start();
|
const application = Application.start();
|
||||||
application.register("variant", variant_controller);
|
application.register("variant", variant_controller);
|
||||||
@@ -34,7 +33,7 @@ describe("VariantController", () => {
|
|||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
delete global.I18n;
|
delete global.I18n;
|
||||||
})
|
});
|
||||||
|
|
||||||
describe("variant_unit_with_scale", () => {
|
describe("variant_unit_with_scale", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -82,6 +81,6 @@ describe("VariantController", () => {
|
|||||||
expect(variant_unit.value).toBe("items");
|
expect(variant_unit.value).toBe("items");
|
||||||
expect(variant_unit_scale.value).toBe("");
|
expect(variant_unit_scale.value).toBe("");
|
||||||
});
|
});
|
||||||
})
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user