hydrationStrategies.spec.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import path from 'node:path'
  2. import { setupPuppeteer } from './e2eUtils'
  3. import type { Ref } from '../../src/runtime'
  4. declare const window: Window & {
  5. isHydrated: boolean
  6. isRootMounted: boolean
  7. teardownCalled?: boolean
  8. show: Ref<boolean>
  9. }
  10. describe('async component hydration strategies', () => {
  11. const { page, click, text, count } = setupPuppeteer(['--window-size=800,600'])
  12. async function goToCase(name: string, query = '') {
  13. const file = `file://${path.resolve(__dirname, `./hydration-strat-${name}.html${query}`)}`
  14. await page().goto(file)
  15. }
  16. async function assertHydrationSuccess(n = '1') {
  17. await click('button')
  18. expect(await text('button')).toBe(n)
  19. }
  20. test('idle', async () => {
  21. const messages: string[] = []
  22. page().on('console', e => messages.push(e.text()))
  23. await goToCase('idle')
  24. // not hydrated yet
  25. expect(await page().evaluate(() => window.isHydrated)).toBe(false)
  26. // wait for hydration
  27. await page().waitForFunction(() => window.isHydrated)
  28. // assert message order: hyration should happen after already queued main thread work
  29. expect(messages.slice(1)).toMatchObject(['resolve', 'busy', 'hydrated'])
  30. await assertHydrationSuccess()
  31. })
  32. test('visible', async () => {
  33. await goToCase('visible')
  34. await page().waitForFunction(() => window.isRootMounted)
  35. expect(await page().evaluate(() => window.isHydrated)).toBe(false)
  36. // scroll down
  37. await page().evaluate(() => window.scrollTo({ top: 1000 }))
  38. await page().waitForFunction(() => window.isHydrated)
  39. await assertHydrationSuccess()
  40. })
  41. test('visible (with rootMargin)', async () => {
  42. await goToCase('visible', '?rootMargin=1000')
  43. await page().waitForFunction(() => window.isRootMounted)
  44. // should hydrate without needing to scroll
  45. await page().waitForFunction(() => window.isHydrated)
  46. await assertHydrationSuccess()
  47. })
  48. test('visible (fragment)', async () => {
  49. await goToCase('visible', '?fragment')
  50. await page().waitForFunction(() => window.isRootMounted)
  51. expect(await page().evaluate(() => window.isHydrated)).toBe(false)
  52. expect(await count('span')).toBe(2)
  53. // scroll down
  54. await page().evaluate(() => window.scrollTo({ top: 1000 }))
  55. await page().waitForFunction(() => window.isHydrated)
  56. await assertHydrationSuccess()
  57. })
  58. test('visible (root v-if) should not throw error', async () => {
  59. const spy = vi.fn()
  60. const currentPage = page()
  61. currentPage.on('pageerror', spy)
  62. await goToCase('visible', '?v-if')
  63. await page().waitForFunction(() => window.isRootMounted)
  64. expect(await page().evaluate(() => window.isHydrated)).toBe(false)
  65. expect(spy).toBeCalledTimes(0)
  66. currentPage.off('pageerror', spy)
  67. })
  68. test('media query', async () => {
  69. await goToCase('media')
  70. await page().waitForFunction(() => window.isRootMounted)
  71. expect(await page().evaluate(() => window.isHydrated)).toBe(false)
  72. // resize
  73. await page().setViewport({ width: 400, height: 600 })
  74. await page().waitForFunction(() => window.isHydrated)
  75. await assertHydrationSuccess()
  76. })
  77. test('interaction', async () => {
  78. await goToCase('interaction')
  79. await page().waitForFunction(() => window.isRootMounted)
  80. expect(await page().evaluate(() => window.isHydrated)).toBe(false)
  81. await click('button')
  82. await page().waitForFunction(() => window.isHydrated)
  83. // should replay event
  84. expect(await text('button')).toBe('1')
  85. await assertHydrationSuccess('2')
  86. })
  87. test('interaction (fragment)', async () => {
  88. await goToCase('interaction', '?fragment')
  89. await page().waitForFunction(() => window.isRootMounted)
  90. expect(await page().evaluate(() => window.isHydrated)).toBe(false)
  91. await click('button')
  92. await page().waitForFunction(() => window.isHydrated)
  93. // should replay event
  94. expect(await text('button')).toBe('1')
  95. await assertHydrationSuccess('2')
  96. })
  97. test('custom', async () => {
  98. await goToCase('custom')
  99. await page().waitForFunction(() => window.isRootMounted)
  100. expect(await page().evaluate(() => window.isHydrated)).toBe(false)
  101. await click('#custom-trigger')
  102. await page().waitForFunction(() => window.isHydrated)
  103. await assertHydrationSuccess()
  104. })
  105. test('custom teardown', async () => {
  106. await goToCase('custom')
  107. await page().waitForFunction(() => window.isRootMounted)
  108. expect(await page().evaluate(() => window.isHydrated)).toBe(false)
  109. await page().evaluate(() => (window.show.value = false))
  110. expect(await text('#app')).toBe('off')
  111. expect(await page().evaluate(() => window.isHydrated)).toBe(false)
  112. expect(await page().evaluate(() => window.teardownCalled)).toBe(true)
  113. })
  114. })