Skip to content

Commit

Permalink
fix equality check to be strict equals for number null
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashton-Pragma committed Feb 22, 2023
1 parent c98d3dd commit 670f533
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/binding/providers/bind-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class BindProvider extends OneWayProvider {
}

_number(value) {
if (value == null || value == "") return null;
if (value == null || value === "") return null;
return Number(value);
}

Expand Down
2 changes: 1 addition & 1 deletion src/store/binding-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export class BindingData {
value = Boolean(value);
}
else if (dataType === "number" || (dataType == null && typeof value !== "object" && (isNaN(value) == false && value != ""))) {
value = value == null || value == "" ? null : Number(value);
value = value == null || value === "" ? null : Number(value);
}

if (obj.type == "data") {
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/providers/bind-provider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,33 @@ describe("bind-provider", async () => {
assertEquals(crsbinding.data.getProperty(id, "value"), null);
}
});

it("should allow 0 as input", async () => {
// Arrange
const id = crsbinding.data.addObject("value", {value: null});

const input = document.createElement("input");
input.type = "number";
instance = new module.BindProvider(input, id, "value", "value");

const addEventListener = input.addEventListener;
input.addEventListener = (event, callback) => {

addEventListener(event, callback);

// Act
input.value = "123";
input.performEvent("change", input);

// Assert
assertEquals(crsbinding.data.getProperty(id, "value"), 123);

// Act
input.value = "0";
input.performEvent("change", input);

// Assert
assertEquals(crsbinding.data.getProperty(id, "value"), 0);
}
});
});

0 comments on commit 670f533

Please sign in to comment.