|
| 1 | +import { expect, test, type Page } from '@playwright/test' |
| 2 | +import { useFixture } from './fixture' |
| 3 | +import { |
| 4 | + expectNoPageError, |
| 5 | + expectNoReload, |
| 6 | + testNoJs, |
| 7 | + waitForHydration, |
| 8 | +} from './helper' |
| 9 | + |
| 10 | +test.describe('dev-custom-server-function', () => { |
| 11 | + const f = useFixture({ |
| 12 | + root: 'examples/custom-server-function', |
| 13 | + mode: 'dev', |
| 14 | + }) |
| 15 | + defineTest(f) |
| 16 | + |
| 17 | + test('moves an action between custom and built-in ownership', async ({ |
| 18 | + page, |
| 19 | + }) => { |
| 20 | + using _ = expectNoPageError(page) |
| 21 | + await page.goto(f.url()) |
| 22 | + await waitForHydration(page) |
| 23 | + await using _noReload = await expectNoReload(page) |
| 24 | + |
| 25 | + const editor = f.createEditor('src/features/mixed-directives/actions.ts') |
| 26 | + // Switch one export from the custom plugin to the built-in plugin. HMR |
| 27 | + // must remove the custom claim while preserving the module's other claims. |
| 28 | + editor.edit((source) => |
| 29 | + source |
| 30 | + .replace(`'use custom-server'`, `'use server'`) |
| 31 | + .replace( |
| 32 | + `customLabel = 'Custom'`, |
| 33 | + `customLabel = 'Custom changed to built-in'`, |
| 34 | + ) |
| 35 | + .replace('customCount++', 'customCount += 10'), |
| 36 | + ) |
| 37 | + |
| 38 | + await expect( |
| 39 | + page.getByRole('button', { name: 'Custom changed to built-in: 0' }), |
| 40 | + ).toBeVisible() |
| 41 | + await page.getByRole('button', { name: 'Built-in: 0', exact: true }).click() |
| 42 | + await expect( |
| 43 | + page.getByRole('button', { name: 'Built-in: 1', exact: true }), |
| 44 | + ).toBeVisible() |
| 45 | + await page |
| 46 | + .getByRole('button', { name: 'Custom changed to built-in: 0' }) |
| 47 | + .click() |
| 48 | + await expect( |
| 49 | + page.getByRole('button', { name: 'Custom changed to built-in: 10' }), |
| 50 | + ).toBeVisible() |
| 51 | + |
| 52 | + // Switch the export back and verify neither owner retained stale state. |
| 53 | + editor.reset() |
| 54 | + await expect(page.getByRole('button', { name: 'Custom: 0' })).toBeVisible() |
| 55 | + await page.getByRole('button', { name: 'Built-in: 0', exact: true }).click() |
| 56 | + await expect( |
| 57 | + page.getByRole('button', { name: 'Built-in: 1', exact: true }), |
| 58 | + ).toBeVisible() |
| 59 | + await page.getByRole('button', { name: 'Custom: 0' }).click() |
| 60 | + await expect(page.getByRole('button', { name: 'Custom: 1' })).toBeVisible() |
| 61 | + }) |
| 62 | +}) |
| 63 | + |
| 64 | +test.describe('build-custom-server-function', () => { |
| 65 | + const f = useFixture({ |
| 66 | + root: 'examples/custom-server-function', |
| 67 | + mode: 'build', |
| 68 | + }) |
| 69 | + defineTest(f) |
| 70 | +}) |
| 71 | + |
| 72 | +function defineTest(f: ReturnType<typeof useFixture>) { |
| 73 | + test('built-in and custom server functions', async ({ page }) => { |
| 74 | + using _ = expectNoPageError(page) |
| 75 | + await page.goto(f.url()) |
| 76 | + await waitForHydration(page) |
| 77 | + await testActions(page) |
| 78 | + }) |
| 79 | + |
| 80 | + testNoJs('progressive forms', async ({ page }) => { |
| 81 | + await page.goto(f.url()) |
| 82 | + await testActions(page) |
| 83 | + }) |
| 84 | + |
| 85 | + async function testActions(page: Page) { |
| 86 | + // The first two actions are inline functions in one RSC-reachable module. |
| 87 | + await page.getByRole('button', { name: 'Built-in: 0' }).click() |
| 88 | + await expect( |
| 89 | + page.getByRole('button', { name: 'Built-in: 1' }), |
| 90 | + ).toBeVisible() |
| 91 | + |
| 92 | + await page.getByRole('button', { name: 'Custom: 0' }).click() |
| 93 | + await expect(page.getByRole('button', { name: 'Custom: 1' })).toBeVisible() |
| 94 | + |
| 95 | + // This module is only imported by a Client Component, so its implementation |
| 96 | + // reaches the RSC build through the aggregated server reference manifest. |
| 97 | + await page.getByRole('button', { name: 'From client: 0' }).click() |
| 98 | + await expect( |
| 99 | + page.getByRole('button', { name: 'From client: 1' }), |
| 100 | + ).toBeVisible() |
| 101 | + |
| 102 | + await page.getByRole('button', { name: 'Reset' }).click() |
| 103 | + await expect( |
| 104 | + page.getByRole('button', { name: 'Built-in: 0' }), |
| 105 | + ).toBeVisible() |
| 106 | + await expect(page.getByRole('button', { name: 'Custom: 0' })).toBeVisible() |
| 107 | + } |
| 108 | +} |
0 commit comments