Prettify javascript

Also update .prettierignore so that spec files get prettified as well
This commit is contained in:
Gaetan Craig-Riou
2024-07-15 10:07:36 +10:00
parent 768825d689
commit 4cd83d3fd4
5 changed files with 98 additions and 76 deletions

View File

@@ -5,6 +5,7 @@
*.yaml
*.json
*.html
**/*.rb
# JS
# Enabled: app/webpacker/controllers/*.js and app/webpacker/packs/*.js
@@ -27,6 +28,5 @@ postcss.config.js
/coverage/
/engines/
/public/
/spec/
/tmp/
/vendor/

View File

@@ -11,7 +11,7 @@ export default class VariantController extends Controller {
this.variantUnitScale = this.element.querySelector('[name$="[variant_unit_scale]"]');
this.variantUnitName = this.element.querySelector('[name$="[variant_unit_name]"]');
this.variantUnitWithScale = this.element.querySelector('[name$="[variant_unit_with_scale]"]');
// on variant_unit_with_scale changed; update variant_unit and variant_unit_scale
this.variantUnitWithScale.addEventListener("change", this.#updateUnitAndScale.bind(this), {
passive: true,
@@ -87,7 +87,7 @@ export default class VariantController extends Controller {
variant_unit_name: this.variantUnitName.value,
};
}
// Extract variant_unit and variant_unit_scale from dropdown variant_unit_with_scale,
// and update hidden product fields
#updateUnitAndScale(event) {

View File

@@ -9,7 +9,7 @@ export default class OptionValueNamer {
name() {
const [value, unit] = this.option_value_value_unit();
const separator = this.value_scaled() ? '' : ' ';
const separator = this.value_scaled() ? "" : " ";
const name_fields = [];
if (value && unit) {
name_fields.push(`${value}${separator}${unit}`);
@@ -20,7 +20,7 @@ export default class OptionValueNamer {
if (this.variant.unit_description) {
name_fields.push(this.variant.unit_description);
}
return name_fields.join(' ');
return name_fields.join(" ");
}
value_scaled() {
@@ -55,7 +55,7 @@ export default class OptionValueNamer {
}
return I18n.t(["inflections", unit_key], {
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
// available unit.
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;
// 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) {
return [largestScale, this.variantUnitManager.getUnitName(largestScale, this.variant.variant_unit)];
return [
largestScale,
this.variantUnitManager.getUnitName(largestScale, this.variant.variant_unit),
];
} else {
return [scales[0], this.variantUnitManager.getUnitName(scales[0], this.variant.variant_unit)];
}
}
}

View File

@@ -7,133 +7,153 @@ import OptionValueNamer from "js/services/option_value_namer";
describe("OptionValueNamer", () => {
beforeAll(() => {
// 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;
beforeEach(function() {
beforeEach(function () {
v = {};
var ofn_available_units_sorted = ofn_available_units_sorted;
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, "option_value_value_unit").mockImplementation(() => ["value", ""]);
expect(namer.name()).toBe("value");
});
it("when description is blank", function() {
it("when description is blank", function () {
v.unit_description = null;
jest.spyOn(namer, "value_scaled").mockImplementation(() => true);
jest.spyOn(namer, "option_value_value_unit").mockImplementation(() => ["value", "unit"]);
expect(namer.name()).toBe("valueunit");
});
it("when description is present", function() {
v.unit_description = 'desc';
it("when description is present", function () {
v.unit_description = "desc";
jest.spyOn(namer, "option_value_value_unit").mockImplementation(() => ["value", "unit"]);
jest.spyOn(namer, "value_scaled").mockImplementation(() => true);
expect(namer.name()).toBe("valueunit desc");
});
it("when value is blank and description is present", function() {
v.unit_description = 'desc';
it("when value is blank and description is present", function () {
v.unit_description = "desc";
jest.spyOn(namer, "option_value_value_unit").mockImplementation(() => [null, null]);
jest.spyOn(namer, "value_scaled").mockImplementation(() => true);
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;
jest.spyOn(namer, "option_value_value_unit").mockImplementation(() => ["value", "unit"]);
jest.spyOn(namer, "value_scaled").mockImplementation(() => false);
expect(namer.name()).toBe("value unit");
});
describe("determining if a variant's value is scaled", function() {
beforeEach(function() {
describe("determining if a variant's value is scaled", function () {
beforeEach(function () {
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;
expect(namer.value_scaled()).toBe(true);
});
it("returns false otherwise", function() {
it("returns false otherwise", function () {
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;
// Mock I18n. TODO: moved to a shared helper
beforeAll(() => {
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 )
afterAll(() => {
delete global.I18n;
})
});
beforeEach(function() {
beforeEach(function () {
v = {};
namer = new OptionValueNamer(v);
});
it("generates simple values", function() {
v.variant_unit = 'weight';
it("generates simple values", function () {
v.variant_unit = "weight";
v.variant_unit_scale = 1.0;
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() {
v.variant_unit = 'weight';
it("generates values when unit value is non-integer", function () {
v.variant_unit = "weight";
v.variant_unit_scale = 1.0;
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() {
v.variant_unit = 'weight';
it("returns a value of 1 when unit value equals the scale", function () {
v.variant_unit = "weight";
v.variant_unit_scale = 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() {
[[1.0, 'g'], [1000.0, 'kg'], [1000000.0, 'T']].forEach(([scale, unit]) => {
v.variant_unit = 'weight';
it("generates values for all weight scales", function () {
[
[1.0, "g"],
[1000.0, "kg"],
[1000000.0, "T"],
].forEach(([scale, unit]) => {
v.variant_unit = "weight";
v.variant_unit_scale = scale;
v.unit_value = 100 * scale;
expect(namer.option_value_value_unit()).toEqual([100, unit]);
});
});
it("generates values for all volume scales", function() {
[[0.001, 'mL'], [1.0, 'L'], [1000.0, 'kL']].forEach(([scale, unit]) => {
v.variant_unit = 'volume';
it("generates values for all volume scales", function () {
[
[0.001, "mL"],
[1.0, "L"],
[1000.0, "kL"],
].forEach(([scale, unit]) => {
v.variant_unit = "volume";
v.variant_unit_scale = scale;
v.unit_value = 100 * scale;
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;
unit = 'L';
v.variant_unit = 'volume';
unit = "L";
v.variant_unit = "volume";
v.variant_unit_scale = 1.0;
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() {
v.variant_unit = 'volume';
it("chooses the correct scale when value is very small", function () {
v.variant_unit = "volume";
v.variant_unit_scale = 0.001;
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
// %w(packet box).each do |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 }
// subject.option_value_value_unit.should == [100, unit.pluralize]
});
it("generates singular values for item units when value is 1", function() {
v.variant_unit = 'items';
it("generates singular values for item units when value is 1", function () {
v.variant_unit = "items";
v.variant_unit_scale = null;
v.variant_unit_name = 'packet';
v.variant_unit_name = "packet";
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() {
v.variant_unit = 'items';
it("returns [null, null] when unit value is not set", function () {
v.variant_unit = "items";
v.variant_unit_scale = null;
v.variant_unit_name = 'foo';
v.variant_unit_name = "foo";
v.unit_value = null;
expect(namer.option_value_value_unit()).toEqual([null, null]);
});

View File

@@ -5,28 +5,27 @@
import { Application } from "stimulus";
import variant_controller from "controllers/variant_controller";
describe("VariantController", () => {
beforeAll(() => {
// 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"}
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" },
},
"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();
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();
application.register("variant", variant_controller);
@@ -34,7 +33,7 @@ describe("VariantController", () => {
afterAll(() => {
delete global.I18n;
})
});
describe("variant_unit_with_scale", () => {
beforeEach(() => {
@@ -82,6 +81,6 @@ describe("VariantController", () => {
expect(variant_unit.value).toBe("items");
expect(variant_unit_scale.value).toBe("");
});
})
});
});
});