Skip to content

Commit 9a79b30

Browse files
authored
Loading Functional Tests: Replace assert with expect (#1466)
Follow-up to #1454 This commit continues to migrate from `assert` usage toward Playwright's `expect`. This commit focuses on the `src/tests/functional/loading_tests.js` file. The majority of this diff replaces `hasSelector` with `expect(…).toBeAttached()` and `expect(…).toHaveAttribute()`. [assertions]: https://playwright.dev/docs/test-assertions
1 parent 1171481 commit 9a79b30

File tree

1 file changed

+42
-63
lines changed

1 file changed

+42
-63
lines changed
Lines changed: 42 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { test } from "@playwright/test"
2-
import { assert } from "chai"
1+
import { expect, test } from "@playwright/test"
32
import {
43
attributeForSelector,
5-
hasSelector,
64
nextAttributeMutationNamed,
75
nextBeat,
86
nextBody,
@@ -18,96 +16,77 @@ test.beforeEach(async ({ page }) => {
1816
})
1917

2018
test("eager loading within a details element", async ({ page }) => {
21-
await nextBeat()
22-
assert.ok(await hasSelector(page, "#loading-eager turbo-frame#frame h2"))
23-
assert.ok(await hasSelector(page, "#loading-eager turbo-frame[complete]"), "has [complete] attribute")
19+
await expect(page.locator("#loading-eager turbo-frame#frame h2")).toBeAttached()
20+
await expect(page.locator("#loading-eager turbo-frame"), "has [complete] attribute").toHaveAttribute("complete")
2421
})
2522

2623
test("lazy loading within a details element", async ({ page }) => {
27-
await nextBeat()
28-
29-
const frameContents = "#loading-lazy turbo-frame h2"
30-
assert.notOk(await hasSelector(page, frameContents))
31-
assert.ok(await hasSelector(page, "#loading-lazy turbo-frame:not([complete])"))
24+
await expect(page.locator("#loading-lazy turbo-frame h2")).not.toBeAttached()
25+
await expect(page.locator("#loading-lazy turbo-frame")).not.toHaveAttribute("complete")
3226

3327
await page.click("#loading-lazy summary")
34-
await nextBeat()
3528

36-
const contents = await page.locator(frameContents)
37-
assert.equal(await contents.textContent(), "Hello from a frame")
38-
assert.ok(await hasSelector(page, "#loading-lazy turbo-frame[complete]"), "has [complete] attribute")
29+
await expect(page.locator("#loading-lazy turbo-frame h2")).toHaveText("Hello from a frame")
30+
await expect(page.locator("#loading-lazy turbo-frame"), "has [complete] attribute").toHaveAttribute("complete")
3931
})
4032

4133
test("changing loading attribute from lazy to eager loads frame", async ({ page }) => {
42-
const frameContents = "#loading-lazy turbo-frame h2"
43-
await nextBeat()
34+
const frame = page.locator("#loading-lazy turbo-frame")
4435

45-
assert.notOk(await hasSelector(page, frameContents))
36+
await expect(frame.locator("h2")).not.toBeAttached()
4637

47-
await page.evaluate(() => document.querySelector("#loading-lazy turbo-frame")?.setAttribute("loading", "eager"))
48-
await nextBeat()
38+
await frame.evaluate((frame) => frame.setAttribute("loading", "eager"))
4939

50-
const contents = await page.locator(frameContents)
5140
await page.click("#loading-lazy summary")
52-
assert.equal(await contents.textContent(), "Hello from a frame")
41+
await expect(frame.locator("h2")).toHaveText("Hello from a frame")
5342
})
5443

5544
test("navigating a visible frame with loading=lazy navigates", async ({ page }) => {
5645
await page.click("#loading-lazy summary")
57-
await nextBeat()
5846

59-
const initialContents = await page.locator("#hello h2")
60-
assert.equal(await initialContents.textContent(), "Hello from a frame")
47+
await expect(page.locator("#hello h2")).toHaveText("Hello from a frame")
6148

6249
await page.click("#hello a")
63-
await nextBeat()
6450

65-
const navigatedContents = await page.locator("#hello h2")
66-
assert.equal(await navigatedContents.textContent(), "Frames: #hello")
51+
await expect(page.locator("#hello h2")).toHaveText("Frames: #hello")
6752
})
6853

6954
test("changing src attribute on a frame with loading=lazy defers navigation", async ({ page }) => {
70-
const frameContents = "#loading-lazy turbo-frame h2"
71-
await nextBeat()
55+
const frame = page.locator("#loading-lazy turbo-frame")
7256

73-
await page.evaluate(() =>
74-
document.querySelector("#loading-lazy turbo-frame")?.setAttribute("src", "/src/tests/fixtures/frames.html")
57+
await frame.evaluate((frame) =>
58+
frame.setAttribute("src", "/src/tests/fixtures/frames.html")
7559
)
76-
assert.notOk(await hasSelector(page, frameContents))
60+
await expect(frame.locator("h2")).not.toBeAttached()
7761

7862
await page.click("#loading-lazy summary")
79-
await nextBeat()
8063

81-
const contents = await page.locator(frameContents)
82-
assert.equal(await contents.textContent(), "Frames: #hello")
64+
await expect(frame.locator("h2")).toHaveText("Frames: #hello")
8365
})
8466

8567
test("changing src attribute on a frame with loading=eager navigates", async ({ page }) => {
86-
const frameContents = "#loading-eager turbo-frame h2"
87-
await nextBeat()
68+
const frame = page.locator("#loading-eager turbo-frame")
8869

89-
await page.evaluate(() =>
90-
document.querySelector("#loading-eager turbo-frame")?.setAttribute("src", "/src/tests/fixtures/frames.html")
70+
await frame.evaluate((frame) =>
71+
frame.setAttribute("src", "/src/tests/fixtures/frames.html")
9172
)
9273

9374
await page.click("#loading-eager summary")
94-
await nextBeat()
9575

96-
const contents = await page.locator(frameContents)
97-
assert.equal(await contents.textContent(), "Frames: #frame")
76+
await expect(frame.locator("h2")).toHaveText("Frames: #frame")
9877
})
9978

10079
test("reloading a frame reloads the content", async ({ page }) => {
10180
await page.click("#loading-eager summary")
10281
await nextEventOnTarget(page, "frame", "turbo:frame-load")
10382

104-
const frameContent = "#loading-eager turbo-frame#frame h2"
105-
assert.ok(await hasSelector(page, frameContent))
106-
assert.equal(await nextAttributeMutationNamed(page, "frame", "complete"), "", "has [complete] attribute")
83+
const frame = page.locator("#loading-eager turbo-frame#frame")
84+
await expect(frame.locator("h2")).toBeAttached()
85+
expect(await nextAttributeMutationNamed(page, "frame", "complete"), "has [complete] attribute").toEqual("")
10786

108-
await page.evaluate(() => document.querySelector("#loading-eager turbo-frame")?.reload())
109-
assert.ok(await hasSelector(page, frameContent))
110-
assert.equal(await nextAttributeMutationNamed(page, "frame", "complete"), null, "clears [complete] attribute")
87+
await frame.evaluate((frame) => frame.reload())
88+
await expect(frame.locator("h2")).toBeAttached()
89+
expect(await nextAttributeMutationNamed(page, "frame", "complete"), "clears [complete] attribute").toEqual(null)
11190
})
11291

11392
test("navigating away from a page does not reload its frames", async ({ page }) => {
@@ -116,42 +95,42 @@ test("navigating away from a page does not reload its frames", async ({ page })
11695

11796
const eventLogs = await readEventLogs(page)
11897
const requestLogs = eventLogs.filter(([name]) => name == "turbo:before-fetch-request")
119-
assert.equal(requestLogs.length, 1)
98+
expect(requestLogs.length).toEqual(1)
12099
})
121100

122101
test("changing [src] attribute on a [complete] frame with loading=lazy defers navigation", async ({ page }) => {
123102
await page.click("#loading-lazy summary")
124103
await nextEventOnTarget(page, "hello", "turbo:frame-load")
125104

126-
assert.ok(await hasSelector(page, "#loading-lazy turbo-frame[complete]"), "lazy frame is complete")
127-
assert.equal(await page.textContent("#hello h2"), "Hello from a frame")
105+
await expect(page.locator("#loading-lazy turbo-frame"), "lazy frame is complete").toHaveAttribute("complete")
106+
await expect(page.locator("#hello h2")).toHaveText("Hello from a frame")
128107

129108
await page.click("#loading-lazy summary")
130109
await page.click("#one")
131110
await nextEventNamed(page, "turbo:load")
132111
await page.goBack()
133112
await nextEventNamed(page, "turbo:load")
134113

135-
assert.ok(await noNextEventOnTarget(page, "hello", "turbo:frame-load"))
114+
expect(await noNextEventOnTarget(page, "hello", "turbo:frame-load")).toBeTruthy()
136115

137116
let src = new URL((await attributeForSelector(page, "#hello", "src")) || "")
138117

139-
assert.ok(await hasSelector(page, "#loading-lazy turbo-frame[complete]"), "lazy frame is complete")
140-
assert.equal(src.pathname, "/src/tests/fixtures/frames/hello.html", "lazy frame retains [src]")
118+
await expect(page.locator("#loading-lazy turbo-frame"), "lazy frame is complete").toHaveAttribute("complete")
119+
expect(src.pathname, "lazy frame retains [src]").toEqual("/src/tests/fixtures/frames/hello.html")
141120

142121
await page.click("#link-lazy-frame")
143122

144-
assert.ok(await noNextEventOnTarget(page, "hello", "turbo:frame-load"))
145-
assert.ok(await hasSelector(page, "#loading-lazy turbo-frame:not([complete])"), "lazy frame is not complete")
123+
expect(await noNextEventOnTarget(page, "hello", "turbo:frame-load")).toBeTruthy()
124+
await expect(page.locator("#loading-lazy turbo-frame"), "lazy frame is not complete").not.toHaveAttribute("complete")
146125

147126
await page.click("#loading-lazy summary")
148127
await nextEventOnTarget(page, "hello", "turbo:frame-load")
149128

150129
src = new URL((await attributeForSelector(page, "#hello", "src")) || "")
151130

152-
assert.equal(await page.textContent("#loading-lazy turbo-frame h2"), "Frames: #hello")
153-
assert.ok(await hasSelector(page, "#loading-lazy turbo-frame[complete]"), "lazy frame is complete")
154-
assert.equal(src.pathname, "/src/tests/fixtures/frames.html", "lazy frame navigates")
131+
await expect(page.locator("#loading-lazy turbo-frame h2")).toHaveText("Frames: #hello")
132+
await expect(page.locator("#loading-lazy turbo-frame"), "lazy frame is complete").toHaveAttribute("complete")
133+
expect(src.pathname, "lazy frame navigates").toEqual("/src/tests/fixtures/frames.html")
155134
})
156135

157136
test("navigating away from a page and then back does not reload its frames", async ({ page }) => {
@@ -166,8 +145,8 @@ test("navigating away from a page and then back does not reload its frames", asy
166145
const requestsOnEagerFrame = requestLogs.filter((record) => record[2] == "frame")
167146
const requestsOnLazyFrame = requestLogs.filter((record) => record[2] == "hello")
168147

169-
assert.equal(requestsOnEagerFrame.length, 0, "does not reload eager frame")
170-
assert.equal(requestsOnLazyFrame.length, 0, "does not reload lazy frame")
148+
expect(requestsOnEagerFrame.length, "does not reload eager frame").toEqual(0)
149+
expect(requestsOnLazyFrame.length, "does not reload lazy frame").toEqual(0)
171150

172151
await page.click("#loading-lazy summary")
173152
await nextEventOnTarget(page, "hello", "turbo:before-fetch-request")
@@ -193,5 +172,5 @@ test("disconnecting and reconnecting a frame does not reload the frame", async (
193172

194173
const eventLogs = await readEventLogs(page)
195174
const requestLogs = eventLogs.filter(([name]) => name == "turbo:before-fetch-request")
196-
assert.equal(requestLogs.length, 0)
175+
expect(requestLogs.length).toEqual(0)
197176
})

0 commit comments

Comments
 (0)