hydration.ts 990 B

123456789101112131415161718192021222324252627
  1. import { expect, type Locator, type Page } from '@playwright/test'
  2. /**
  3. * A page can be on screen before React has taken it over. Typed into then,
  4. * a controlled field keeps the letters on screen while React's own state
  5. * stays empty, and the button beside it sends nothing. There is no reliable
  6. * signal for the moment that passes.
  7. */
  8. /** Gives a freshly opened page time to become interactive. */
  9. export async function settle(page: Page): Promise<void> {
  10. await page.waitForLoadState('networkidle')
  11. await page.waitForTimeout(1_000)
  12. }
  13. /**
  14. * Types into a field once the page has settled, and checks the words are
  15. * still there afterwards, repeating if they were lost to a late render.
  16. */
  17. export async function fillSettled(field: Locator, value: string): Promise<void> {
  18. await settle(field.page())
  19. await expect(async () => {
  20. await field.fill(value)
  21. await field.page().waitForTimeout(400)
  22. await expect(field).toHaveValue(value)
  23. }).toPass({ timeout: 30_000 })
  24. }