Skip to content

Commit

Permalink
Add test for findOne that adresses previous fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zachsents committed Jan 6, 2025
1 parent fab7c6e commit a50fa4a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/querying.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ describe("querying", () => {
const manyNodesWide = parseDocument(
`<body>${"<div></div>".repeat(200_000)}Text</body>`,
);
const someDeepNodes = parseDocument(
`<body><div><div></div></div><div><p></p></div></body>`,
);

describe("find", () => {
it("should accept many children without RangeError", () =>
Expand Down Expand Up @@ -107,6 +110,15 @@ describe("querying", () => {
),
).toBe((manyNodesWide.children[0] as Element).children[0]));

it("should find elements in children in any branch", () =>
expect(
findOne(
(elem) => elem.name === "p",
someDeepNodes.children,
true,
),
).toBeTruthy());

it("should not find elements in children if recurse is false", () =>
expect(
findOne((elem) => elem.name === "div", manyNodesWide, false),
Expand Down
2 changes: 1 addition & 1 deletion src/querying.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function findOne(
}
if (recurse && hasChildren(node) && node.children.length > 0) {
const found = findOne(test, node.children, true);
if(found) return found
if (found) return found;
}
}

Expand Down

0 comments on commit a50fa4a

Please sign in to comment.