|
| 1 | +import { test, expect } from "@playwright/test"; |
| 2 | + |
| 3 | +test.describe("Cache Handler - Next.js 16", () => { |
| 4 | + test("homepage should be cached with 'use cache'", async ({ page }) => { |
| 5 | + // First visit |
| 6 | + await page.goto("/"); |
| 7 | + const firstTimestamp = await page.textContent("div span"); |
| 8 | + expect(firstTimestamp).toBeTruthy(); |
| 9 | + |
| 10 | + // Second visit - should show same timestamp due to caching |
| 11 | + await page.goto("/"); |
| 12 | + const secondTimestamp = await page.textContent("div span"); |
| 13 | + |
| 14 | + // The timestamps should be the same because the page is cached |
| 15 | + expect(secondTimestamp).toBe(firstTimestamp); |
| 16 | + }); |
| 17 | + |
| 18 | + test("ISR page should be cached", async ({ page }) => { |
| 19 | + // Visit ISR page |
| 20 | + await page.goto("/isr-example/blog/1"); |
| 21 | + |
| 22 | + // Should load without errors |
| 23 | + await expect(page.locator("h1")).toBeVisible(); |
| 24 | + const title = await page.textContent("h1"); |
| 25 | + expect(title).toBeTruthy(); |
| 26 | + |
| 27 | + // Verify content is present |
| 28 | + await expect(page.locator("p")).toBeVisible(); |
| 29 | + }); |
| 30 | + |
| 31 | + test("fetch cache should work with tags", async ({ page }) => { |
| 32 | + // Visit fetch example page |
| 33 | + await page.goto("/fetch-example"); |
| 34 | + |
| 35 | + // Should load character data |
| 36 | + await expect(page.locator("h1")).toContainText("Character"); |
| 37 | + |
| 38 | + // Reload - should use cached data |
| 39 | + await page.reload(); |
| 40 | + await expect(page.locator("h1")).toContainText("Character"); |
| 41 | + }); |
| 42 | + |
| 43 | + test("static params should generate correctly", async ({ page }) => { |
| 44 | + // Visit a static param route |
| 45 | + await page.goto("/static-params-test/test1"); |
| 46 | + |
| 47 | + // Should load without errors |
| 48 | + await expect(page.locator("body")).toBeVisible(); |
| 49 | + |
| 50 | + // Check for content |
| 51 | + const content = await page.textContent("body"); |
| 52 | + expect(content).toBeTruthy(); |
| 53 | + }); |
| 54 | + |
| 55 | + test("API cache route should respond", async ({ page }) => { |
| 56 | + // Test the cache API endpoint |
| 57 | + const response = await page.request.get("/api/cache"); |
| 58 | + expect(response.ok()).toBeTruthy(); |
| 59 | + |
| 60 | + const data = await response.json(); |
| 61 | + expect(data).toBeDefined(); |
| 62 | + }); |
| 63 | + |
| 64 | + test("PPR example should render", async ({ page }) => { |
| 65 | + // Visit PPR example |
| 66 | + await page.goto("/ppr-example"); |
| 67 | + |
| 68 | + // Should show prerendered content |
| 69 | + await expect(page.locator("h1")).toContainText("prerendered"); |
| 70 | + |
| 71 | + // Should also show dynamic content (may be in suspense initially) |
| 72 | + await expect(page.locator("h1").nth(1)).toBeVisible({ timeout: 10000 }); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +test.describe("Cache Handler - Performance", () => { |
| 77 | + test("repeated requests should be fast (cached)", async ({ page }) => { |
| 78 | + // First request (cold) |
| 79 | + const start1 = Date.now(); |
| 80 | + await page.goto("/"); |
| 81 | + const duration1 = Date.now() - start1; |
| 82 | + |
| 83 | + // Second request (should be cached) |
| 84 | + const start2 = Date.now(); |
| 85 | + await page.goto("/"); |
| 86 | + const duration2 = Date.now() - start2; |
| 87 | + |
| 88 | + // Subsequent requests should generally be faster |
| 89 | + // (though this isn't guaranteed, it's a good indicator) |
| 90 | + console.log(`First request: ${duration1}ms, Second request: ${duration2}ms`); |
| 91 | + expect(duration2).toBeLessThan(duration1 * 2); // At least not slower |
| 92 | + }); |
| 93 | +}); |
0 commit comments