|
| 1 | +import { expect, test } from '@playwright/test' |
| 2 | +import { type Fixture, useFixture } from './fixture' |
| 3 | +import { waitForHydration } from './helper' |
| 4 | + |
| 5 | +test.describe('dev', () => { |
| 6 | + const f = useFixture({ root: 'examples/use-cache', mode: 'dev' }) |
| 7 | + defineTests(f) |
| 8 | +}) |
| 9 | + |
| 10 | +test.describe('build', () => { |
| 11 | + const f = useFixture({ root: 'examples/use-cache', mode: 'build' }) |
| 12 | + defineTests(f) |
| 13 | +}) |
| 14 | + |
| 15 | +function defineTests(f: Fixture) { |
| 16 | + test('use cache function', async ({ page }) => { |
| 17 | + await page.goto(f.url()) |
| 18 | + await waitForHydration(page) |
| 19 | + const locator = page.getByTestId('test-use-cache-fn') |
| 20 | + await expect(locator.locator('span')).toHaveText( |
| 21 | + '(actionCount: 0, cacheFnCount: 0)', |
| 22 | + ) |
| 23 | + |
| 24 | + // The action runs on every submit, but the cached function runs once per argument. |
| 25 | + await locator.getByRole('button').click() |
| 26 | + await expect(locator.locator('span')).toHaveText( |
| 27 | + '(actionCount: 1, cacheFnCount: 1)', |
| 28 | + ) |
| 29 | + await locator.getByRole('button').click() |
| 30 | + await expect(locator.locator('span')).toHaveText( |
| 31 | + '(actionCount: 2, cacheFnCount: 1)', |
| 32 | + ) |
| 33 | + await locator.getByRole('textbox').fill('test') |
| 34 | + await locator.getByRole('button').click() |
| 35 | + await expect(locator.locator('span')).toHaveText( |
| 36 | + '(actionCount: 3, cacheFnCount: 2)', |
| 37 | + ) |
| 38 | + await locator.getByRole('textbox').fill('test') |
| 39 | + await locator.getByRole('button').click() |
| 40 | + await expect(locator.locator('span')).toHaveText( |
| 41 | + '(actionCount: 4, cacheFnCount: 2)', |
| 42 | + ) |
| 43 | + |
| 44 | + // revalidate cache |
| 45 | + await locator.getByRole('textbox').fill('revalidate') |
| 46 | + await locator.getByRole('button').click() |
| 47 | + await expect(locator.locator('span')).toHaveText( |
| 48 | + '(actionCount: 5, cacheFnCount: 3)', |
| 49 | + ) |
| 50 | + await locator.getByRole('textbox').fill('test') |
| 51 | + await locator.getByRole('button').click() |
| 52 | + await expect(locator.locator('span')).toHaveText( |
| 53 | + '(actionCount: 6, cacheFnCount: 4)', |
| 54 | + ) |
| 55 | + }) |
| 56 | + |
| 57 | + test('use cache component', async ({ page }) => { |
| 58 | + await page.goto(f.url()) |
| 59 | + await waitForHydration(page) |
| 60 | + const static1 = await page |
| 61 | + .getByTestId('test-use-cache-component-static') |
| 62 | + .textContent() |
| 63 | + const dynamic1 = await page |
| 64 | + .getByTestId('test-use-cache-component-dynamic') |
| 65 | + .textContent() |
| 66 | + await page.waitForTimeout(100) |
| 67 | + await page.reload() |
| 68 | + const static2 = await page |
| 69 | + .getByTestId('test-use-cache-component-static') |
| 70 | + .textContent() |
| 71 | + const dynamic2 = await page |
| 72 | + .getByTestId('test-use-cache-component-dynamic') |
| 73 | + .textContent() |
| 74 | + |
| 75 | + // The cached shell stays stable while temporary-reference children are refreshed. |
| 76 | + expect({ static2, dynamic2 }).toEqual({ |
| 77 | + static2: expect.stringMatching(static1!), |
| 78 | + dynamic2: expect.not.stringMatching(dynamic1!), |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + test('use cache closure', async ({ page }) => { |
| 83 | + await page.goto(f.url()) |
| 84 | + await waitForHydration(page) |
| 85 | + const locator = page.getByTestId('test-use-cache-closure') |
| 86 | + await expect(locator.locator('span')).toHaveText( |
| 87 | + '(actionCount: 0, innerFnCount: 0)', |
| 88 | + ) |
| 89 | + |
| 90 | + // Both the captured outer value and call-time inner argument form the cache key. |
| 91 | + // (x, y) |
| 92 | + await locator.getByPlaceholder('outer').fill('x') |
| 93 | + await locator.getByPlaceholder('inner').fill('y') |
| 94 | + await locator.getByRole('button').click() |
| 95 | + await expect(locator.locator('span')).toHaveText( |
| 96 | + '(actionCount: 1, innerFnCount: 1)', |
| 97 | + ) |
| 98 | + |
| 99 | + // (x, y) |
| 100 | + await locator.getByPlaceholder('outer').fill('x') |
| 101 | + await locator.getByPlaceholder('inner').fill('y') |
| 102 | + await locator.getByRole('button').click() |
| 103 | + await expect(locator.locator('span')).toHaveText( |
| 104 | + '(actionCount: 2, innerFnCount: 1)', |
| 105 | + ) |
| 106 | + |
| 107 | + // (xx, y) |
| 108 | + await locator.getByPlaceholder('outer').fill('xx') |
| 109 | + await locator.getByPlaceholder('inner').fill('y') |
| 110 | + await locator.getByRole('button').click() |
| 111 | + await expect(locator.locator('span')).toHaveText( |
| 112 | + '(actionCount: 3, innerFnCount: 2)', |
| 113 | + ) |
| 114 | + |
| 115 | + // (xx, y) |
| 116 | + await locator.getByPlaceholder('outer').fill('xx') |
| 117 | + await locator.getByPlaceholder('inner').fill('y') |
| 118 | + await locator.getByRole('button').click() |
| 119 | + await expect(locator.locator('span')).toHaveText( |
| 120 | + '(actionCount: 4, innerFnCount: 2)', |
| 121 | + ) |
| 122 | + |
| 123 | + // (xx, yy) |
| 124 | + await locator.getByPlaceholder('outer').fill('xx') |
| 125 | + await locator.getByPlaceholder('inner').fill('yy') |
| 126 | + await locator.getByRole('button').click() |
| 127 | + await expect(locator.locator('span')).toHaveText( |
| 128 | + '(actionCount: 5, innerFnCount: 3)', |
| 129 | + ) |
| 130 | + }) |
| 131 | +} |
0 commit comments