Skip to content

Commit 9d0a411

Browse files
committed
test(org): wait for the route's prefetch and for the removal before reloading
1 parent ede5ffb commit 9d0a411

4 files changed

Lines changed: 37 additions & 14 deletions

File tree

‎apps/sim/scripts/org-redesign-e2e/checks/home.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,16 @@ export async function homeChecks() {
356356
const row = recents.getByRole('link', { name: SEEDED_SOURCE.title })
357357
await expect(row).toBeVisible()
358358
await row.hover()
359+
const removed = page.waitForResponse(
360+
(response) =>
361+
response.request().method() === 'DELETE' &&
362+
new URL(response.url()).pathname.endsWith('/search/history/sources')
363+
)
359364
await recents
360365
.getByRole('button', { name: `Remove ${SEEDED_SOURCE.title} from recents` })
361366
.click()
362367
await expect(row).toHaveCount(0)
368+
assert.ok((await removed).ok(), 'The server refused the removal')
363369
await page.reload()
364370
await expect(homeRegion(page, 'Email')).toBeVisible()
365371
await session.settle()

‎apps/sim/scripts/org-redesign-e2e/checks/links.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
mainNav,
3333
menuEntry,
3434
pageTurn,
35+
routePrefetched,
3536
} from '@/scripts/org-redesign-e2e/locators'
3637

3738
export async function linksChecks() {
@@ -391,14 +392,10 @@ export async function linksChecks() {
391392
if (route.request().headers()['next-router-prefetch'] === undefined) await hold.promise
392393
await route.continue()
393394
})
394-
const loadingPrefetched = page.waitForResponse(
395-
async (response) =>
396-
isLinksPayload(new URL(response.url())) &&
397-
// A prefetch the page moved past has no body left to read, and is not the one awaited.
398-
(await response.text().catch(() => '')).includes('Loading links')
399-
)
395+
const prefetched = routePrefetched(page, isLinksPayload)
400396
await page.goto(orgUrl('/home'))
401-
await loadingPrefetched
397+
await mainNav(page).getByRole('link', { name: 'Links', exact: true }).hover()
398+
await prefetched()
402399
await mainNav(page).getByRole('link', { name: 'Links', exact: true }).click()
403400
await expect(page.getByRole('status', { name: 'Loading links' })).toBeVisible()
404401
await expect(page.getByRole('button', { name: 'New link', exact: true })).toBeVisible()

‎apps/sim/scripts/org-redesign-e2e/checks/people.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
section,
1515
withSession,
1616
} from '@/scripts/org-redesign-e2e/harness'
17-
import { mainNav, navLabels } from '@/scripts/org-redesign-e2e/locators'
17+
import { mainNav, navLabels, routePrefetched } from '@/scripts/org-redesign-e2e/locators'
1818

1919
export async function peoplePhaseChecks() {
2020
section('People with its flag on')
@@ -32,14 +32,11 @@ export async function peoplePhaseChecks() {
3232
if (route.request().headers()['next-router-prefetch'] === undefined) await hold.promise
3333
await route.continue()
3434
})
35-
// The router can only show a loading boundary a prefetch has already delivered.
36-
const prefetched = page.waitForResponse((response) =>
37-
isPeoplePayload(new URL(response.url()))
38-
)
35+
const prefetched = routePrefetched(page, isPeoplePayload)
3936
await page.goto(orgUrl('/home'))
4037
const link = mainNav(page).getByRole('link', { name: 'People', exact: true })
4138
await link.hover()
42-
await prefetched
39+
await prefetched()
4340
await link.click()
4441
const rows = page.getByRole('status', { name: 'Loading people…' })
4542
await expect(rows).toBeVisible()

‎apps/sim/scripts/org-redesign-e2e/locators.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from 'node:assert/strict'
2-
import type { Locator, Page } from '@playwright/test'
2+
import type { Locator, Page, Request } from '@playwright/test'
33
import { expect } from '@/scripts/org-redesign-e2e/harness'
44

55
declare global {
@@ -70,6 +70,29 @@ export async function openRowMenu(page: Page, label: string) {
7070
return page.getByRole('menu', { name: `Actions for ${label}` })
7171
}
7272

73+
/**
74+
* Resolves once the route's prefetch has landed: a response arrived and none is outstanding. The
75+
* router can only show a loading boundary a prefetch already delivered, and it fetches a route in
76+
* several requests whose order and timing are its own.
77+
*/
78+
export function routePrefetched(page: Page, isPayload: (address: URL) => boolean) {
79+
let outstanding = 0
80+
let landed = 0
81+
const matches = (request: Request) =>
82+
isPayload(new URL(request.url())) && request.headers()['next-router-prefetch'] !== undefined
83+
page.on('request', (request) => {
84+
if (matches(request)) outstanding += 1
85+
})
86+
const finish = (request: Request) => {
87+
if (!matches(request)) return
88+
outstanding -= 1
89+
landed += 1
90+
}
91+
page.on('requestfinished', finish)
92+
page.on('requestfailed', finish)
93+
return () => expect.poll(() => landed > 0 && outstanding === 0).toBe(true)
94+
}
95+
7396
export async function clipboard(page: Page) {
7497
return page.evaluate(() => navigator.clipboard.readText())
7598
}

0 commit comments

Comments
 (0)