|
1 | 1 | import { assertSnapshot } from "../tests/utils"
|
2 | 2 |
|
| 3 | +const iframeSrc = `<iframe src="https://interactive-examples.mdn.mozilla.net/pages/tabbed/input-text.html">` |
| 4 | + |
3 | 5 | describe("toHaveFocus", () => {
|
4 | 6 | beforeEach(async () => {
|
5 | 7 | await jestPlaywright.resetContext()
|
6 | 8 | })
|
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 | + }) |
16 | 35 | })
|
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 | + }) |
23 | 51 | })
|
| 52 | + |
24 | 53 | describe("with 'not' usage", () => {
|
25 | 54 | it("positive", async () => {
|
26 | 55 | await page.setContent('<input id="foo"><input id="bar">')
|
27 | 56 | await page.keyboard.press("Tab")
|
28 | 57 | await expect(page).not.toHaveFocus("#bar")
|
29 | 58 | })
|
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 () => { |
31 | 73 | await page.setContent('<input id="foobar">')
|
32 | 74 | await page.keyboard.press("Tab")
|
33 | 75 | await assertSnapshot(() => expect(page).not.toHaveFocus("#foobar"))
|
34 | 76 | })
|
35 | 77 | })
|
| 78 | + |
36 | 79 | 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 |
| - }) |
44 | 80 | it("should throw an error after the timeout exceeds", async () => {
|
45 | 81 | const start = new Date().getTime()
|
46 | 82 | await assertSnapshot(() =>
|
47 | 83 | expect(page).toHaveFocus("#foobar", { timeout: 1000 })
|
48 | 84 | )
|
49 | 85 | const duration = new Date().getTime() - start
|
| 86 | + expect(duration).toBeGreaterThan(1000) |
50 | 87 | expect(duration).toBeLessThan(1500)
|
51 | 88 | })
|
52 | 89 | })
|
|
0 commit comments