|
| 1 | +import { expect, type Page, test } from '@playwright/test'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Inject a marker in the window object to detect full page reloads. |
| 5 | + * If the page is fully reloaded, this marker will disappear. |
| 6 | + */ |
| 7 | +async function markPageAsLoaded(page: Page): Promise<void> { |
| 8 | + await page.evaluate(() => { |
| 9 | + (window as any).__turboTestMarker = 'initial-load'; |
| 10 | + }); |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Verify that the page was not fully reloaded by checking if the marker is still present. |
| 15 | + * This proves that Turbo handled the navigation without a full page reload. |
| 16 | + */ |
| 17 | +async function expectNoFullPageReload(page: Page): Promise<void> { |
| 18 | + const markerStillPresent = await page.evaluate(() => { |
| 19 | + return (window as any).__turboTestMarker === 'initial-load'; |
| 20 | + }); |
| 21 | + expect(markerStillPresent).toBe(true); |
| 22 | +} |
| 23 | + |
| 24 | +test('Can navigate with Turbo Drive without full page reload', async ({ page }) => { |
| 25 | + await page.goto('/ux-turbo/drive'); |
| 26 | + await markPageAsLoaded(page); |
| 27 | + |
| 28 | + // Check initial page content |
| 29 | + await expect(page.locator('h2')).toContainText('Turbo Drive Navigation - Page 1'); |
| 30 | + const initialTime = await page.locator('#page-load-time').textContent(); |
| 31 | + |
| 32 | + // Navigate to page 2 |
| 33 | + await page.click('#navigate-to-page-2'); |
| 34 | + |
| 35 | + // Wait for navigation to complete |
| 36 | + await expect(page.locator('h2')).toContainText('Turbo Drive Navigation - Page 2'); |
| 37 | + |
| 38 | + // The time on page 2 should be different (it's a new request) |
| 39 | + const page2Time = await page.locator('#page-load-time').textContent(); |
| 40 | + expect(page2Time).not.toBe(initialTime); |
| 41 | + |
| 42 | + // Navigate back to page 1 |
| 43 | + await page.click('#navigate-back'); |
| 44 | + await expect(page.locator('h2')).toContainText('Turbo Drive Navigation - Page 1'); |
| 45 | + |
| 46 | + await expectNoFullPageReload(page); |
| 47 | +}); |
| 48 | + |
| 49 | +test('Can navigate inside a Turbo Frame without affecting the rest of the page', async ({ page }) => { |
| 50 | + await page.goto('/ux-turbo/frame'); |
| 51 | + await markPageAsLoaded(page); |
| 52 | + |
| 53 | + // Check initial state |
| 54 | + await expect(page.locator('#frame-initial-content')).toContainText('This is the initial frame content'); |
| 55 | + await expect(page.locator('#content-outside-frame')).toContainText('This content is outside the frame'); |
| 56 | + |
| 57 | + // Click link inside the frame |
| 58 | + await page.click('#load-frame-content'); |
| 59 | + |
| 60 | + // Wait for frame content to update |
| 61 | + await expect(page.locator('#frame-updated-content')).toContainText('The frame content has been updated'); |
| 62 | + |
| 63 | + // Verify content outside frame hasn't changed |
| 64 | + await expect(page.locator('#content-outside-frame')).toContainText('This content is outside the frame'); |
| 65 | + |
| 66 | + // Verify the frame initial content is no longer visible |
| 67 | + await expect(page.locator('#frame-initial-content')).not.toBeVisible(); |
| 68 | + |
| 69 | + await expectNoFullPageReload(page); |
| 70 | +}); |
| 71 | + |
| 72 | +test('Can update page content with Turbo Streams after form submission', async ({ page }) => { |
| 73 | + await page.goto('/ux-turbo/stream'); |
| 74 | + await markPageAsLoaded(page); |
| 75 | + // Submit the form |
| 76 | + await page.click('#submit-turbo-stream'); |
| 77 | + |
| 78 | + // Wait for Turbo Stream to update the content |
| 79 | + await expect(page.locator('#updated-by-stream')).toContainText('This content was updated by a Turbo Stream'); |
| 80 | + |
| 81 | + // Verify the form was removed by the Turbo Stream |
| 82 | + await expect(page.locator('#form-container')).not.toBeVisible(); |
| 83 | + |
| 84 | + // Verify the target element still exists but with new content |
| 85 | + await expect(page.locator('#stream-target')).toBeVisible(); |
| 86 | + await expect(page.locator('#stream-target')).toHaveClass(/alert-success/); |
| 87 | + |
| 88 | + await expectNoFullPageReload(page); |
| 89 | +}); |
0 commit comments