Skip to content

Commit 9a3e71e

Browse files
Added deep assertion for array attributes
1 parent 636b429 commit 9a3e71e

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

src/assertComponent.mjs

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,7 @@ function assertComponentImpl(path, result, expectedElement) {
5555
.forEach((attr) => {
5656
const resultValue = result.props[attr];
5757
const expectedValue = expectedElement.props[attr];
58-
if (typeof expectedValue === "object" && !Array.isArray(expectedValue)) {
59-
assertObject(`${pathName}.${attr}`, resultValue, expectedValue);
60-
} else if (typeof expectedValue === "function") {
61-
// functions could differ !!!
62-
} else {
63-
assertAttrValue(`${pathName}.${attr}`, resultValue, expectedValue);
64-
}
58+
assertAny(`${pathName}.${attr}`, resultValue, expectedValue);
6559
});
6660

6761
const children = getComponentChildren(result);
@@ -97,30 +91,59 @@ function assertComponentImpl(path, result, expectedElement) {
9791
}
9892

9993
/**
100-
*
94+
* @param {string} name
95+
* @param {any} resultValue
96+
* @param {any} expectedValue
97+
*/
98+
function assertAny(name, resultValue, expectedValue) {
99+
if (typeof expectedValue === "object") {
100+
if (Array.isArray(expectedValue)) {
101+
assertArray(name, resultValue, expectedValue);
102+
} else {
103+
assertObject(name, resultValue, expectedValue);
104+
}
105+
} else if (typeof expectedValue === "function") {
106+
// functions could differ !!!
107+
} else {
108+
assertAttrValue(name, resultValue, expectedValue);
109+
}
110+
}
111+
112+
/**
113+
* @param {string} name
114+
* @param {any} resultArray
115+
* @param {any[]} expectedArray
116+
*/
117+
function assertArray(name, resultArray, expectedArray) {
118+
assertAttrValue(`${name}.isArray`, Array.isArray(resultArray), true);
119+
assertAttrValue(`${name}.length`, resultArray.length, expectedArray.length);
120+
121+
if (resultArray !== expectedArray) {
122+
expectedArray.forEach((expectedValue, index) => {
123+
const resultValue = resultArray[index];
124+
assertAny(`${name}.${index}`, resultValue, expectedValue);
125+
});
126+
}
127+
}
128+
129+
/**
101130
* @param {string} name
102131
* @param {any} resultValue
103132
* @param {any} expectedObject
104133
*/
105134
function assertObject(name, resultValue, expectedObject) {
106-
assertAttrValue(name, typeof resultValue, "object");
135+
assertAttrValue(`${name}.typeof`, typeof resultValue, "object");
107136

108137
if (resultValue !== expectedObject) {
109138
const resultObject = resultValue;
110139
const resultKeys = new Set(Object.keys(resultObject));
111140
const expectedKeys = new Set(Object.keys(expectedObject));
112-
assertAttrValue(name, resultKeys, expectedKeys);
141+
assertAttrValue(`${name}.keys`, resultKeys, expectedKeys);
113142

114143
expectedKeys.forEach((key) => {
115144
const resultValue = resultObject[key];
116145
const expectedValue = expectedObject[key];
117-
if (typeof expectedValue === "object" && !Array.isArray(expectedValue)) {
118-
assertObject(`${name}.${key}`, resultValue, expectedValue);
119-
} else if (typeof expectedValue === "function") {
120-
// functions could differ !!!
121-
} else {
122-
assertAttrValue(`${name}.${key}`, resultValue, expectedValue);
123-
}
146+
assertAny(`${name}.${key}`, resultValue, expectedValue);
124147
});
125148
}
126149
}

test/assertComponent.test.mjs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ describe("assertComponent.test.mjs", () => {
3535
//then
3636
assert.deepEqual(
3737
resError?.message,
38-
"Attribute value doesn't match for p.testArr" +
39-
"\n\tactual: test1,test2" +
40-
"\n\texpected: test2,test1"
38+
"Attribute value doesn't match for p.testArr.0" +
39+
"\n\tactual: test1" +
40+
"\n\texpected: test2"
4141
);
4242
});
4343

@@ -241,7 +241,9 @@ describe("assertComponent.test.mjs", () => {
241241
onPress: () => 1,
242242
},
243243
h("div", {
244-
testArr: ["test"],
244+
emptyArr: [],
245+
simpleArr: ["test1", "test2"],
246+
objectArr: [{ a: "test", f: () => {} }],
245247
testObj: {
246248
test: 1,
247249
nested: {
@@ -273,7 +275,9 @@ describe("assertComponent.test.mjs", () => {
273275
onPress: () => 2, // functions could differ !!!
274276
},
275277
h("div", {
276-
testArr: ["test"],
278+
emptyArr: [],
279+
simpleArr: ["test1", "test2"],
280+
objectArr: [{ a: "test", f: () => {} }],
277281
testObj: {
278282
test: 1,
279283
nested: {

0 commit comments

Comments
 (0)