Skip to content

Commit 7afb787

Browse files
Functions could differ when asserting component
1 parent f61ec10 commit 7afb787

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/assertComponent.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ function assertComponentImpl(path, result, expectedElement) {
5757
const expectedValue = expectedElement.props[attr];
5858
if (typeof expectedValue === "object" && !Array.isArray(expectedValue)) {
5959
assertObject(`${pathName}.${attr}`, resultValue, expectedValue);
60+
} else if (typeof expectedValue === "function") {
61+
// functions could differ !!!
6062
} else {
6163
assertAttrValue(`${pathName}.${attr}`, resultValue, expectedValue);
6264
}
@@ -112,8 +114,10 @@ function assertObject(name, resultValue, expectedObject) {
112114
expectedKeys.forEach((key) => {
113115
const resultValue = resultObject[key];
114116
const expectedValue = expectedObject[key];
115-
if (typeof expectedValue === "object") {
117+
if (typeof expectedValue === "object" && !Array.isArray(expectedValue)) {
116118
assertObject(`${name}.${key}`, resultValue, expectedValue);
119+
} else if (typeof expectedValue === "function") {
120+
// functions could differ !!!
117121
} else {
118122
assertAttrValue(`${name}.${key}`, resultValue, expectedValue);
119123
}

test/assertComponent.test.mjs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,35 @@ describe("assertComponent.test.mjs", () => {
6868
);
6969
});
7070

71+
it("should fail if function attribute doesn't match", () => {
72+
//given
73+
const Comp = () => {
74+
return h("p", {
75+
testObj: {
76+
test: () => 1,
77+
},
78+
});
79+
};
80+
const comp = TestRenderer.create(h(Comp)).root.children[0];
81+
/** @type {Error?} */
82+
let resError = null;
83+
84+
//when
85+
try {
86+
assertComponent(comp, h("p", { testObj: { test: undefined } }));
87+
} catch (error) {
88+
resError = error;
89+
}
90+
91+
//then
92+
assert.deepEqual(
93+
resError?.message,
94+
"Attribute value doesn't match for p.testObj.test" +
95+
"\n\tactual: () => 1" +
96+
"\n\texpected: undefined"
97+
);
98+
});
99+
71100
it("should fail if boolean attribute doesn't match", () => {
72101
//given
73102
const Comp = () => {
@@ -205,13 +234,18 @@ describe("assertComponent.test.mjs", () => {
205234
id: id,
206235
hidden: true,
207236
height: 10,
237+
arr: [1, 2],
238+
test: undefined,
239+
onPress: () => 1,
208240
},
209241
h("div", {
210242
testArr: ["test"],
211243
testObj: {
212244
test: 1,
213245
nested: {
214246
test2: 2,
247+
arr2: [1, 2],
248+
onPress2: () => 1,
215249
},
216250
},
217251
}),
@@ -232,13 +266,18 @@ describe("assertComponent.test.mjs", () => {
232266
id: id,
233267
hidden: true,
234268
height: 10,
269+
arr: [1, 2],
270+
test: undefined,
271+
onPress: () => 2, // functions could differ !!!
235272
},
236273
h("div", {
237274
testArr: ["test"],
238275
testObj: {
239276
test: 1,
240277
nested: {
241278
test2: 2,
279+
arr2: [1, 2],
280+
onPress2: () => 2, // functions could differ !!!
242281
},
243282
},
244283
}),

0 commit comments

Comments
 (0)