Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add turbo:before-prefetch test coverage #1205

Merged
merged 1 commit into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 26 additions & 12 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, noNextEventNamed, 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 Expand Up @@ -65,17 +80,16 @@ test("it doesn't prefetch the page when link has data-turbo=false", async ({ pag
test("allows to cancel prefetch requests with custom logic", 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.evaluate(a => a.addEventListener("turbo:before-prefetch", event => event.preventDefault()))

await page.evaluate(() => {
document.body.addEventListener("turbo:before-prefetch", (event) => {
if (event.target.hasAttribute("data-remote")) {
event.preventDefault()
}
})
})
await page.pause()
await link.hover()
await nextEventOnTarget(page, "anchor_for_prefetch", "turbo:before-prefetch")
await noNextEventNamed(page, "turbo:before-fetch-request")
await link.click()

await assertNotPrefetchedOnHover({ page, selector: "#anchor_for_prefetch" })
await expect(page.locator("body")).toHaveText("Prefetched Page Content")
})

test("it doesn't prefetch UJS links", async ({ page }) => {
Expand Down
Loading