Skip to content

Commit

Permalink
fix: deepQuerySelector types and processing shadow roots (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
KonnorRogers committed Nov 11, 2022
1 parent d2a1a2a commit 92934e2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
6 changes: 5 additions & 1 deletion __tests__/deep-query-selectors.test.tsx
Expand Up @@ -47,13 +47,17 @@ describe("deepQuerySelector()", () => {
describe("deepQuerySelectorAll()", () => {
test("Should find and return both buttons", () => {
const { container, baseElement } = render(<Duplicates />);
let btns = deepQuerySelectorAll(container, "button");

let btns = deepQuerySelectorAll(container, "button");
expect(btns).toHaveLength(2);
btns.forEach((btn) => expect(btn).toBeInstanceOf(HTMLButtonElement));

btns = deepQuerySelectorAll(baseElement, "button");
expect(btns).toHaveLength(2);
btns.forEach((btn) => expect(btn).toBeInstanceOf(HTMLButtonElement));

btns = deepQuerySelectorAll(document.querySelector("duplicate-buttons") as HTMLElement, "button");
expect(btns).toHaveLength(2);
btns.forEach((btn) => expect(btn).toBeInstanceOf(HTMLButtonElement));
});
});
9 changes: 8 additions & 1 deletion __tests__/within.test.tsx
@@ -1,5 +1,5 @@
import * as React from "react";
import { MySelect, Select } from "../components";
import { Duplicates, MySelect, Select, TripleShadowRoots } from "../components";
import { render } from "@testing-library/react";
import { screen, within } from "../src/index";

Expand All @@ -26,4 +26,11 @@ describe("within", () => {

expect(option).toBeInTheDocument();
});

it("Should find the nested button", async () => {
render(<Duplicates />);
const btns = await within(document.querySelector("duplicate-buttons") as HTMLElement).findAllByShadowRole("button")
expect(btns).toHaveLength(2)
btns.forEach((btn) => expect(btn).toBeInstanceOf(HTMLButtonElement))
})
});
7 changes: 4 additions & 3 deletions src/deep-query-selectors.ts
@@ -1,14 +1,14 @@
import { Container, ShadowOptions } from "./types";

export function deepQuerySelector(
export function deepQuerySelector<T extends HTMLElement>(
container: Container,
selector: string,
options: ShadowOptions = { shallow: false }
): Element | null {
): T | null {
const els = deepQuerySelectorAll(container, selector, options);

if (Array.isArray(els) && els.length > 0) {
return els[0] as Element | null;
return els[0] as T | null;
}

return null;
Expand Down Expand Up @@ -74,6 +74,7 @@ function recurse(
container.shadowRoot.mode !== "closed"
) {
elements.push(container.shadowRoot);
elementsToProcess.push(container.shadowRoot)
}

elementsToProcess.forEach((containerElement) => {
Expand Down

0 comments on commit 92934e2

Please sign in to comment.