Skip to content

Commit

Permalink
Functions could differ when asserting component
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-podzigun committed Jun 14, 2023
1 parent f61ec10 commit 7afb787
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/assertComponent.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ function assertComponentImpl(path, result, expectedElement) {
const expectedValue = expectedElement.props[attr];
if (typeof expectedValue === "object" && !Array.isArray(expectedValue)) {
assertObject(`${pathName}.${attr}`, resultValue, expectedValue);
} else if (typeof expectedValue === "function") {
// functions could differ !!!
} else {
assertAttrValue(`${pathName}.${attr}`, resultValue, expectedValue);
}
Expand Down Expand Up @@ -112,8 +114,10 @@ function assertObject(name, resultValue, expectedObject) {
expectedKeys.forEach((key) => {
const resultValue = resultObject[key];
const expectedValue = expectedObject[key];
if (typeof expectedValue === "object") {
if (typeof expectedValue === "object" && !Array.isArray(expectedValue)) {
assertObject(`${name}.${key}`, resultValue, expectedValue);
} else if (typeof expectedValue === "function") {
// functions could differ !!!
} else {
assertAttrValue(`${name}.${key}`, resultValue, expectedValue);
}
Expand Down
39 changes: 39 additions & 0 deletions test/assertComponent.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,35 @@ describe("assertComponent.test.mjs", () => {
);
});

it("should fail if function attribute doesn't match", () => {
//given
const Comp = () => {
return h("p", {
testObj: {
test: () => 1,
},
});
};
const comp = TestRenderer.create(h(Comp)).root.children[0];
/** @type {Error?} */
let resError = null;

//when
try {
assertComponent(comp, h("p", { testObj: { test: undefined } }));
} catch (error) {
resError = error;
}

//then
assert.deepEqual(
resError?.message,
"Attribute value doesn't match for p.testObj.test" +
"\n\tactual: () => 1" +
"\n\texpected: undefined"
);
});

it("should fail if boolean attribute doesn't match", () => {
//given
const Comp = () => {
Expand Down Expand Up @@ -205,13 +234,18 @@ describe("assertComponent.test.mjs", () => {
id: id,
hidden: true,
height: 10,
arr: [1, 2],
test: undefined,
onPress: () => 1,
},
h("div", {
testArr: ["test"],
testObj: {
test: 1,
nested: {
test2: 2,
arr2: [1, 2],
onPress2: () => 1,
},
},
}),
Expand All @@ -232,13 +266,18 @@ describe("assertComponent.test.mjs", () => {
id: id,
hidden: true,
height: 10,
arr: [1, 2],
test: undefined,
onPress: () => 2, // functions could differ !!!
},
h("div", {
testArr: ["test"],
testObj: {
test: 1,
nested: {
test2: 2,
arr2: [1, 2],
onPress2: () => 2, // functions could differ !!!
},
},
}),
Expand Down

0 comments on commit 7afb787

Please sign in to comment.