diff --git a/src/matchers/toHaveSelector/index.test.ts b/src/matchers/toHaveSelector/index.test.ts index 7beeafd..fd57275 100644 --- a/src/matchers/toHaveSelector/index.test.ts +++ b/src/matchers/toHaveSelector/index.test.ts @@ -8,6 +8,12 @@ describe("toHaveSelector", () => { await page.setContent(`
Bar
`) await expect(page).toHaveSelector("#foobar") }) + it("positive with an element handle", async () => { + await page.setContent(`
Baz
`) + const handle = await page.$("#foo") + expect(handle).toBeTruthy() + await expect(handle).toHaveSelector("#bar") + }) it("positive in frame", async () => { await page.setContent(``) const handle = page.$("iframe") diff --git a/src/matchers/toHaveSelector/index.ts b/src/matchers/toHaveSelector/index.ts index c2a7aa5..83ebe8f 100644 --- a/src/matchers/toHaveSelector/index.ts +++ b/src/matchers/toHaveSelector/index.ts @@ -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 { - 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) diff --git a/src/matchers/toHaveSelectorCount/index.test.ts b/src/matchers/toHaveSelectorCount/index.test.ts index e93ef2e..b9c0920 100644 --- a/src/matchers/toHaveSelectorCount/index.test.ts +++ b/src/matchers/toHaveSelectorCount/index.test.ts @@ -11,6 +11,19 @@ describe("toHaveSelectorCount", () => { ) await expect(page).toHaveSelectorCount(".foobar", 2) }) + it("positive with an element handle", async () => { + await page.setContent(` +
+
Bar
+
Bar
+
Bar
+
Bar
+
+ `) + const handle = await page.$("#parent") + expect(handle).toBeTruthy() + await expect(handle).toHaveSelectorCount(".foobar", 4) + }) it("positive in frame", async () => { await page.setContent(``) const handle = page.$("iframe") diff --git a/src/matchers/toHaveSelectorCount/index.ts b/src/matchers/toHaveSelectorCount/index.ts index 54d55aa..66730e0 100644 --- a/src/matchers/toHaveSelectorCount/index.ts +++ b/src/matchers/toHaveSelectorCount/index.ts @@ -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, @@ -9,10 +9,13 @@ const toHaveSelectorCount: jest.CustomMatcher = async function ( options: PageWaitForSelectorOptions = {} ): Promise { try { - const frame = (await getFrame(arg))! - await frame.waitForSelector(selector, { state: "attached", ...options }) + const [elementHandle] = await getElementHandle([arg, selector, options], 1) + 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,