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

Commit f69f73a

Browse files
author
Mark Skelton
committed
Update matcher and tests
1 parent d6ca67e commit f69f73a

File tree

3 files changed

+87
-83
lines changed

3 files changed

+87
-83
lines changed
Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`toHaveFocus negative: target element don't have focus 1`] = `
4-
"[2mexpect([22m[31mreceived[39m[2m).[22mtoHaveFocus[2m([22m[32mexpected[39m[2m)[22m
3+
exports[`toHaveFocus element negative 1`] = `
4+
"[2mexpect([22m[31mreceived[39m[2m).[22mtoHaveFocus[2m()[22m
55
6-
Expected: element to have focus"
6+
Expected: true
7+
Received: false"
78
`;
89

9-
exports[`toHaveFocus negative: target element not found 1`] = `
10-
"[2mexpect([22m[31mreceived[39m[2m).[22mtoHaveFocus[2m([22m[32mexpected[39m[2m)[22m
10+
exports[`toHaveFocus selector negative 1`] = `
11+
"[2mexpect([22m[31mreceived[39m[2m).[22mtoHaveFocus[2m()[22m
1112
12-
Expected: element to have focus
13-
Received: element was not found"
13+
Expected: [32mtrue[39m
14+
Received: [31mfalse[39m"
1415
`;
1516

16-
exports[`toHaveFocus timeout should throw an error after the timeout exceeds 1`] = `
17-
"expect(received).toHaveFocus(expected)
17+
exports[`toHaveFocus timeout should throw an error after the timeout exceeds 1`] = `"Error: Timeout exceed for element '#foobar'"`;
1818

19-
Expected: element to have focus
20-
Received: element was not found"
21-
`;
22-
23-
exports[`toHaveFocus with 'not' usage negative: target element has focus 1`] = `
24-
"expect(received).not.toHaveFocus(expected)
19+
exports[`toHaveFocus with 'not' usage negative 1`] = `
20+
"expect(received).not.toHaveFocus()
2521
26-
Expected: element to not have focus"
22+
Expected: not [32mtrue[39m"
2723
`;

src/matchers/toHaveFocus/index.test.ts

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,89 @@
11
import { assertSnapshot } from "../tests/utils"
22

3+
const iframeSrc = `<iframe src="https://interactive-examples.mdn.mozilla.net/pages/tabbed/input-text.html">`
4+
35
describe("toHaveFocus", () => {
46
beforeEach(async () => {
57
await jestPlaywright.resetContext()
68
})
7-
it("positive", async () => {
8-
await page.setContent(`<input id="foobar">`)
9-
await page.keyboard.press("Tab")
10-
await expect(page).toHaveFocus("#foobar")
11-
})
12-
it("negative: target element don't have focus", async () => {
13-
await page.setContent(`<input id="foo"><input id="bar">`)
14-
await page.keyboard.press("Tab")
15-
await assertSnapshot(() => expect(page).toHaveFocus("#bar"))
9+
10+
describe("selector", () => {
11+
it("positive", async () => {
12+
await page.setContent(`<input id="foobar">`)
13+
await page.keyboard.press("Tab")
14+
await expect(page).toHaveFocus("#foobar")
15+
})
16+
17+
it("positive in frame", async () => {
18+
await page.setContent('<iframe src="https://example.com">')
19+
await page.keyboard.press("Tab")
20+
21+
const handle = page.$("iframe")
22+
await expect(handle).toHaveFocus("a")
23+
await expect(await handle).toHaveFocus("a")
24+
25+
const frame = (await handle)?.contentFrame()
26+
await expect(frame).toHaveFocus("a")
27+
await expect(await frame).toHaveFocus("a")
28+
})
29+
30+
it("negative", async () => {
31+
await page.setContent(`<input id="foo"><input id="bar">`)
32+
await page.keyboard.press("Tab")
33+
await assertSnapshot(() => expect(page).toHaveFocus("#bar"))
34+
})
1635
})
17-
it("negative: target element not found", async () => {
18-
await page.setContent(`<input id="foo">`)
19-
await page.keyboard.press("Tab")
20-
await assertSnapshot(() =>
21-
expect(page).toHaveFocus("#bar", { timeout: 1000 })
22-
)
36+
37+
describe("element", () => {
38+
it("positive", async () => {
39+
await page.setContent(`<input id="foobar">`)
40+
await page.keyboard.press("Tab")
41+
const element = page.$("#foobar")
42+
await expect(element).toHaveFocus()
43+
await expect(await element).toHaveFocus()
44+
})
45+
46+
it("negative", async () => {
47+
await page.setContent(`<input id="foo"><input id="bar">`)
48+
await page.keyboard.press("Tab")
49+
await assertSnapshot(() => expect(page.$("#bar")).toHaveFocus())
50+
})
2351
})
52+
2453
describe("with 'not' usage", () => {
2554
it("positive", async () => {
2655
await page.setContent('<input id="foo"><input id="bar">')
2756
await page.keyboard.press("Tab")
2857
await expect(page).not.toHaveFocus("#bar")
2958
})
30-
it("negative: target element has focus", async () => {
59+
60+
it("positive in frame", async () => {
61+
await page.setContent('<iframe src="https://example.com">')
62+
63+
const handle = page.$("iframe")
64+
await expect(handle).not.toHaveFocus("a")
65+
await expect(await handle).not.toHaveFocus("a")
66+
67+
const frame = (await handle)?.contentFrame()
68+
await expect(frame).not.toHaveFocus("a")
69+
await expect(await frame).not.toHaveFocus("a")
70+
})
71+
72+
it("negative", async () => {
3173
await page.setContent('<input id="foobar">')
3274
await page.keyboard.press("Tab")
3375
await assertSnapshot(() => expect(page).not.toHaveFocus("#foobar"))
3476
})
3577
})
78+
3679
describe("timeout", () => {
37-
it("positive: should be able to use a custom timeout", async () => {
38-
setTimeout(async () => {
39-
await page.setContent(`<input id="foobar">`)
40-
await page.keyboard.press("Tab")
41-
}, 500)
42-
await expect(page).toHaveFocus("#foobar", { timeout: 1000 })
43-
})
4480
it("should throw an error after the timeout exceeds", async () => {
4581
const start = new Date().getTime()
4682
await assertSnapshot(() =>
4783
expect(page).toHaveFocus("#foobar", { timeout: 1000 })
4884
)
4985
const duration = new Date().getTime() - start
86+
expect(duration).toBeGreaterThan(1000)
5087
expect(duration).toBeLessThan(1500)
5188
})
5289
})

src/matchers/toHaveFocus/index.ts

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,26 @@
11
import { SyncExpectationResult } from "expect/build/types"
2-
import type { Page } from "playwright-core"
32
import { PageWaitForSelectorOptions } from "../../../global"
3+
import { getElementHandle, getMessage, InputArguments } from "../utils"
44

5-
const enum FailureReason {
6-
NotFound,
7-
NotFocused,
8-
}
9-
10-
async function isFocused(
11-
page: Page,
12-
selector: string,
13-
options: PageWaitForSelectorOptions = {}
14-
) {
5+
const toHaveFocus: jest.CustomMatcher = async function (
6+
...args: InputArguments
7+
): Promise<SyncExpectationResult> {
158
try {
16-
await page.waitForSelector(selector, options)
9+
const [elementHandle] = await getElementHandle(args, 0)
1710
/* istanbul ignore next */
18-
const isFocused = await page.$eval(
19-
selector,
11+
const isFocused = await elementHandle.evaluate(
2012
(el) => el === document.activeElement
2113
)
2214

23-
return { pass: isFocused, reason: FailureReason.NotFocused }
24-
} catch (e) {
25-
return { pass: false, reason: FailureReason.NotFound }
26-
}
27-
}
28-
29-
const toHaveFocus: jest.CustomMatcher = async function (
30-
page: Page,
31-
selector: string,
32-
options: PageWaitForSelectorOptions = {}
33-
): Promise<SyncExpectationResult> {
34-
const result = await isFocused(page, selector, options)
35-
36-
return {
37-
pass: result.pass,
38-
message: () => {
39-
const not = this.isNot ? " not" : ""
40-
const hint = this.utils.matcherHint("toHaveFocus", undefined, undefined, {
41-
isNot: this.isNot,
42-
promise: this.promise,
43-
})
44-
45-
const message =
46-
result.reason === FailureReason.NotFound
47-
? `Expected: element to${not} have focus\n` +
48-
"Received: element was not found"
49-
: `Expected: element to${not} have focus`
50-
51-
return hint + "\n\n" + message
52-
},
15+
return {
16+
pass: isFocused,
17+
message: () => getMessage(this, "toHaveFocus", true, isFocused, ""),
18+
}
19+
} catch (err) {
20+
return {
21+
pass: false,
22+
message: () => err.toString(),
23+
}
5324
}
5425
}
5526

0 commit comments

Comments
 (0)