Skip to content

Commit

Permalink
Add turbo:before-prefetch test coverage
Browse files Browse the repository at this point in the history
Expand the link prefetching functional test coverage to exercise
prefetching in relation to the events dispatched during the lifecycle.
For example, include coverage for:

* `turbo:before-prefetch` being dispatched
* `turbo:before-fetch-request` dispatched only once during a successful
  prefetch
  • Loading branch information
seanpdoyle committed Feb 24, 2024
1 parent 00527e5 commit cb2a240
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/tests/fixtures/hover_to_prefetch.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<meta charset="utf-8" />
<title>Hover to Prefetch</title>
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
<script src="/src/tests/fixtures/test.js"></script>
<meta name="turbo-prefetch" content="true" />
</head>

Expand Down
1 change: 1 addition & 0 deletions src/tests/fixtures/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"turbo:before-visit",
"turbo:load",
"turbo:render",
"turbo:before-prefetch",
"turbo:before-fetch-request",
"turbo:submit-start",
"turbo:submit-end",
Expand Down
21 changes: 18 additions & 3 deletions src/tests/functional/link_prefetch_observer_tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from "@playwright/test"
import { expect, test } from "@playwright/test"
import { assert } from "chai"
import { nextBeat, sleep } from "../helpers/page"
import { nextBeat, nextEventOnTarget, noNextEventOnTarget, sleep } from "../helpers/page"
import fs from "fs"
import path from "path"

Expand All @@ -17,7 +17,22 @@ test.afterEach(() => {

test("it prefetches the page", async ({ page }) => {
await goTo({ page, path: "/hover_to_prefetch.html" })
await assertPrefetchedOnHover({ page, selector: "#anchor_for_prefetch" })

const link = page.locator("#anchor_for_prefetch")

await link.hover()
await nextEventOnTarget(page, "anchor_for_prefetch", "turbo:before-prefetch")
const { url, fetchOptions } = await nextEventOnTarget(page, "anchor_for_prefetch", "turbo:before-fetch-request")

expect(url).toEqual(await link.evaluate(a => a.href))
expect(fetchOptions.headers["X-Sec-Purpose"]).toEqual("prefetch")

await link.hover()
await noNextEventOnTarget(page, "anchor_for_prefetch", "turbo:before-fetch-request")
await link.click()
await noNextEventOnTarget(page, "anchor_for_prefetch", "turbo:before-fetch-request")

await expect(page.locator("body")).toHaveText("Prefetched Page Content")
})

test("it doesn't follow the link", async ({ page }) => {
Expand Down

0 comments on commit cb2a240

Please sign in to comment.