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

Commit 4446e8e

Browse files
mxschmittmskelton
andauthored
fix: toHaveSelector when ElementHandle is passed (#118)
Co-authored-by: Mark Skelton <mdskelton99@gmail.com>
1 parent dd3da8e commit 4446e8e

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

src/matchers/toHaveSelector/index.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ describe("toHaveSelector", () => {
88
await page.setContent(`<div id="foobar">Bar</div>`)
99
await expect(page).toHaveSelector("#foobar")
1010
})
11+
it("positive with an element handle", async () => {
12+
await page.setContent(`<div id="foo"><div id="bar">Baz</div></div>`)
13+
const handle = await page.$("#foo")
14+
expect(handle).toBeTruthy()
15+
await expect(handle).toHaveSelector("#bar")
16+
})
1117
it("positive in frame", async () => {
1218
await page.setContent(`<iframe src="http://localhost:8080"></iframe>`)
1319
const handle = page.$("iframe")

src/matchers/toHaveSelector/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
import { SyncExpectationResult } from "expect/build/types"
22
import { PageWaitForSelectorOptions } from "../../../global"
3-
import { ExpectInputType, getFrame } from "../utils"
3+
import { ExpectInputType, getElementHandle } from "../utils"
44

55
const toHaveSelector: jest.CustomMatcher = async function (
66
arg: ExpectInputType,
77
selector: string,
88
options: PageWaitForSelectorOptions = {}
99
): Promise<SyncExpectationResult> {
10-
const frame = await getFrame(arg)
11-
const pass = await frame!
12-
.waitForSelector(selector, {
13-
state: this.isNot ? "hidden" : "visible",
14-
...options,
15-
})
10+
const pass = await getElementHandle(
11+
[
12+
arg,
13+
selector,
14+
{
15+
state: this.isNot ? "hidden" : "visible",
16+
...options,
17+
},
18+
],
19+
0
20+
)
1621
.then(() => !this.isNot)
1722
.catch(() => this.isNot)
1823

src/matchers/toHaveSelectorCount/index.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ describe("toHaveSelectorCount", () => {
1111
)
1212
await expect(page).toHaveSelectorCount(".foobar", 2)
1313
})
14+
it("positive with an element handle", async () => {
15+
await page.setContent(`
16+
<div id="parent">
17+
<div class="foobar">Bar</div>
18+
<div class="foobar">Bar</div>
19+
<div class="foobar">Bar</div>
20+
<div class="foobar">Bar</div>
21+
</div>
22+
`)
23+
const handle = await page.$("#parent")
24+
expect(handle).toBeTruthy()
25+
await expect(handle).toHaveSelectorCount(".foobar", 4)
26+
})
1427
it("positive in frame", async () => {
1528
await page.setContent(`<iframe src="http://localhost:8080"></iframe>`)
1629
const handle = page.$("iframe")

src/matchers/toHaveSelectorCount/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SyncExpectationResult } from "expect/build/types"
22
import { PageWaitForSelectorOptions } from "../../../global"
3-
import { ExpectInputType, getFrame, getMessage, quote } from "../utils"
3+
import { ExpectInputType, getElementHandle, getMessage, quote } from "../utils"
44

55
const toHaveSelectorCount: jest.CustomMatcher = async function (
66
arg: ExpectInputType,
@@ -9,10 +9,13 @@ const toHaveSelectorCount: jest.CustomMatcher = async function (
99
options: PageWaitForSelectorOptions = {}
1010
): Promise<SyncExpectationResult> {
1111
try {
12-
const frame = (await getFrame(arg))!
13-
await frame.waitForSelector(selector, { state: "attached", ...options })
12+
const [elementHandle] = await getElementHandle([arg, selector, options], 1)
13+
await elementHandle.waitForSelector(selector, {
14+
state: "attached",
15+
...options,
16+
})
1417
/* istanbul ignore next */
15-
const actualCount = await frame.$$eval(selector, (el) => el.length)
18+
const actualCount = await elementHandle.$$eval(selector, (el) => el.length)
1619

1720
return {
1821
pass: actualCount === expectedValue,

0 commit comments

Comments
 (0)