-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: modify deepQuerySelectorAll behavior, add getAllElementsAndShad…
…owRoots. (#36) * feat: introduce getAllElementsAndShadowRoots, make deepQuerySelector behave closer to spec * tests * fix: deep query selectors actually work * chore: prettier
- Loading branch information
1 parent
4e6f567
commit 697a0dc
Showing
5 changed files
with
142 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import * as React from "react"; | ||
import { render } from "@testing-library/react"; | ||
import { Duplicates, TripleShadowRoots } from "../components"; | ||
import { deepQuerySelector, deepQuerySelectorAll } from "../src/index"; | ||
|
||
describe("deepQuerySelector()", () => { | ||
test("Should find and return the first button.", () => { | ||
const { container, baseElement } = render(<Duplicates />); | ||
|
||
const btn = document | ||
.querySelector("duplicate-buttons") | ||
?.shadowRoot?.querySelector("button"); | ||
const secondButton = document | ||
.querySelector("duplicate-buttons") | ||
?.shadowRoot?.querySelectorAll("button")[1]; | ||
|
||
expect(deepQuerySelector(container, "button")).toBeInstanceOf( | ||
HTMLButtonElement | ||
); | ||
expect(deepQuerySelector(container, "button")).toBe(btn); | ||
expect(deepQuerySelector(container, "button")).not.toBe(secondButton); | ||
|
||
expect(deepQuerySelector(baseElement, "button")).toBeInstanceOf( | ||
HTMLButtonElement | ||
); | ||
expect(deepQuerySelector(baseElement, "button")).toBe(btn); | ||
expect(deepQuerySelector(baseElement, "button")).not.toBe(secondButton); | ||
}); | ||
|
||
test("Should find and return the 3rd level button element", () => { | ||
const { container, baseElement } = render(<TripleShadowRoots />); | ||
let el = deepQuerySelector(container, "button"); | ||
expect(el).toBeInstanceOf(HTMLButtonElement); | ||
el = deepQuerySelector(baseElement, "button"); | ||
expect(el).toBeInstanceOf(HTMLButtonElement); | ||
}); | ||
|
||
test("Should not find and 3rd level button element when shallow is true", () => { | ||
const { container, baseElement } = render(<TripleShadowRoots />); | ||
let el = deepQuerySelector(container, "button", { shallow: true }); | ||
expect(el).not.toBeInstanceOf(HTMLButtonElement); | ||
el = deepQuerySelector(baseElement, "button", { shallow: true }); | ||
expect(el).not.toBeInstanceOf(HTMLButtonElement); | ||
}); | ||
}); | ||
|
||
describe("deepQuerySelectorAll()", () => { | ||
test("Should find and return both buttons", () => { | ||
const { container, baseElement } = render(<Duplicates />); | ||
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)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters