Skip to content

Commit

Permalink
Read the mutation logs to assert the direction attribute change
Browse files Browse the repository at this point in the history
It's less dependent on the timing of the mutation. Even if the attribute
is no longer present, we can still assert that it was present at some
point.
  • Loading branch information
afcapel authored and domchristie committed Dec 2, 2023
1 parent 70cda68 commit c6f82a5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/tests/functional/visit_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import {
getSearchParam,
isScrolledToSelector,
isScrolledToTop,
nextAttributeMutationNamed,
nextBeat,
nextEventNamed,
noNextAttributeMutationNamed,
pathname,
readEventLogs,
resetMutationLogs,
scrollToSelector,
visitAction,
waitUntilNoSelector,
waitUntilSelector,
willChangeBody
} from "../helpers/page"

Expand Down Expand Up @@ -224,14 +225,15 @@ test("test Visit with network error", async ({ page }) => {

test("Visit direction data attribute when clicking a link", async ({ page }) => {
page.click("#same-origin-link")

await assertVisitDirectionAttribute(page, "forward")
})

test("Visit direction data attribute when navigating back", async ({ page }) => {
await page.click("#same-origin-link")
await nextEventNamed(page, "turbo:load")

await resetMutationLogs(page)

page.goBack()

await assertVisitDirectionAttribute(page, "back")
Expand Down Expand Up @@ -270,6 +272,6 @@ async function visitLocation(page, location) {
}

async function assertVisitDirectionAttribute(page, direction) {
await waitUntilSelector(page, `[data-turbo-visit-direction='${direction}']`)
assert.equal(await nextAttributeMutationNamed(page, "html", "data-turbo-visit-direction"), direction)
await waitUntilNoSelector(page, "[data-turbo-visit-direction]")
}
6 changes: 6 additions & 0 deletions src/tests/helpers/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ export function propertyForSelector(page, selector, propertyName) {
return page.locator(selector).evaluate((element, propertyName) => element[propertyName], propertyName)
}

export function resetMutationLogs(page) {
return page.evaluate(() => {
window.mutationLogs = []
})
}

async function readArray(page, identifier, length) {
return page.evaluate(
({ identifier, length }) => {
Expand Down

0 comments on commit c6f82a5

Please sign in to comment.