Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

fix: toHaveSelector when ElementHandle is passed #118

Merged
merged 4 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/matchers/toHaveSelector/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ describe("toHaveSelector", () => {
await page.setContent(`<div id="foobar">Bar</div>`)
await expect(page).toHaveSelector("#foobar")
})
it("positive with an element handle", async () => {
await page.setContent(`<div id="foobar">Bar</div>`)
const handle = await page.$("#foobar")
expect(handle).toBeTruthy()
await expect(handle).toHaveSelector("#foobar")
mxschmitt marked this conversation as resolved.
Show resolved Hide resolved
})
it("positive in frame", async () => {
await page.setContent(`<iframe src="http://localhost:8080"></iframe>`)
const handle = page.$("iframe")
Expand Down
19 changes: 12 additions & 7 deletions src/matchers/toHaveSelector/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { SyncExpectationResult } from "expect/build/types"
import { PageWaitForSelectorOptions } from "../../../global"
import { ExpectInputType, getFrame } from "../utils"
import { ExpectInputType, getElementHandle } from "../utils"

const toHaveSelector: jest.CustomMatcher = async function (
arg: ExpectInputType,
selector: string,
options: PageWaitForSelectorOptions = {}
): Promise<SyncExpectationResult> {
const frame = await getFrame(arg)
const pass = await frame!
.waitForSelector(selector, {
state: this.isNot ? "hidden" : "visible",
...options,
})
const pass = await getElementHandle(
[
arg,
selector,
{
state: this.isNot ? "hidden" : "visible",
...options,
},
],
0
)
.then(() => !this.isNot)
.catch(() => this.isNot)

Expand Down
13 changes: 13 additions & 0 deletions src/matchers/toHaveSelectorCount/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ describe("toHaveSelectorCount", () => {
)
await expect(page).toHaveSelectorCount(".foobar", 2)
})
it("positive with an element handle", async () => {
await page.setContent(`
<div id="parent">
<div class="foobar">Bar</div>
<div class="foobar">Bar</div>
<div class="foobar">Bar</div>
<div class="foobar">Bar</div>
</div>
`)
const handle = await page.$("#parent")
expect(handle).toBeTruthy()
await expect(handle).toHaveSelectorCount(".foobar", 4)
})
it("positive in frame", async () => {
await page.setContent(`<iframe src="http://localhost:8080"></iframe>`)
const handle = page.$("iframe")
Expand Down
11 changes: 7 additions & 4 deletions src/matchers/toHaveSelectorCount/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SyncExpectationResult } from "expect/build/types"
import { PageWaitForSelectorOptions } from "../../../global"
import { ExpectInputType, getFrame, getMessage, quote } from "../utils"
import { ExpectInputType, getElementHandle, getMessage, quote } from "../utils"

const toHaveSelectorCount: jest.CustomMatcher = async function (
arg: ExpectInputType,
Expand All @@ -9,10 +9,13 @@ const toHaveSelectorCount: jest.CustomMatcher = async function (
options: PageWaitForSelectorOptions = {}
): Promise<SyncExpectationResult> {
try {
const frame = (await getFrame(arg))!
await frame.waitForSelector(selector, { state: "attached", ...options })
const [elementHandle] = await getElementHandle([arg, selector], 1)
mxschmitt marked this conversation as resolved.
Show resolved Hide resolved
await elementHandle.waitForSelector(selector, {
state: "attached",
...options,
})
/* istanbul ignore next */
const actualCount = await frame.$$eval(selector, (el) => el.length)
const actualCount = await elementHandle.$$eval(selector, (el) => el.length)

return {
pass: actualCount === expectedValue,
Expand Down
2 changes: 1 addition & 1 deletion src/matchers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const isElementHandle = (value: Handle): value is ElementHandle => {

export const getFrame = async (value: ExpectInputType) => {
const resolved = await value
return isElementHandle(resolved) ? resolved.contentFrame() : resolved
return isElementHandle(resolved) ? await resolved.contentFrame() : resolved
mxschmitt marked this conversation as resolved.
Show resolved Hide resolved
}

const isObject = (value: unknown) =>
Expand Down