Skip to content

Commit

Permalink
test: add test for server directives (#627)
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored Jan 7, 2025
1 parent 10ba198 commit 625880e
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/server-actions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "server-actions-example",
"type": "module",
"exports": {
"./inline": "./dist/inline.js",
"./client": "./dist/client.js"
},
"devDependencies": {
"@types/react": "*"
},
"dependencies": {
"react": "*"
}
}
5 changes: 5 additions & 0 deletions examples/server-actions/src/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use server'

export async function action1() {
return 'action1'
}
7 changes: 7 additions & 0 deletions examples/server-actions/src/client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use client'

import { action1 } from './action'

export default function Page() {
return <button onClick={action1}>Action 1</button>
}
11 changes: 11 additions & 0 deletions examples/server-actions/src/inline.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @ts-ignore externals
import ClientComponent from 'client-component'

export default function Page() {
async function inlineAction() {
'use server'
return 'inline-action'
}

return <ClientComponent action={inlineAction} />
}
6 changes: 6 additions & 0 deletions examples/server-actions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"jsx": "react-jsx",
"target": "ES2022"
}
}
1 change: 1 addition & 0 deletions test/integration/mixed-directives/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`integration - mixed-directives should work with js only project 1`] = `
{
"action-server-DOTFC6td.js": "'use server';
async function action1() {
return 'action1';
}
export { action1 as a };
",
"client.js": "'use client';
import { jsx } from 'react/jsx-runtime';
import { a as action1 } from './action-server-DOTFC6td.js';
function Page() {
return /*#__PURE__*/ jsx("button", {
onClick: action1,
children: "Action 1"
});
}
export { Page as default };
",
"inline.js": "import { jsx } from 'react/jsx-runtime';
import ClientComponent from 'client-component';
// @ts-ignore externals
function Page() {
async function inlineAction() {
'use server';
return 'inline-action';
}
return /*#__PURE__*/ jsx(ClientComponent, {
action: inlineAction
});
}
export { Page as default };
",
}
`;
25 changes: 25 additions & 0 deletions test/integration/mixed-directives/mixed-directives.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { readFileSync } from 'fs'
import { createIntegrationTest, getFileNamesFromDirectory } from '../utils'

describe('integration - mixed-directives', () => {
it('should work with js only project', async () => {
await createIntegrationTest(
{
directory: __dirname,
},
async ({ distDir }) => {
const distFiles = await getFileNamesFromDirectory(distDir)
const fileContents = distFiles
.map((file) => [file, readFileSync(`${distDir}/${file}`, 'utf-8')])
.reduce((acc, pair) => {
return {
...acc,
[pair[0]]: pair[1],
}
}, {})

expect(fileContents).toMatchSnapshot(``)
},
)
})
})
12 changes: 12 additions & 0 deletions test/integration/mixed-directives/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "mixed-directives",
"type": "module",
"exports": {
"./inline": "./dist/inline.js",
"./client": "./dist/client.js"
},
"dependencies": {
"react": "*",
"react-dom": "*"
}
}
5 changes: 5 additions & 0 deletions test/integration/mixed-directives/src/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use server'

export async function action1() {
return 'action1'
}
7 changes: 7 additions & 0 deletions test/integration/mixed-directives/src/client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use client'

import { action1 } from './action'

export default function Page() {
return <button onClick={action1}>Action 1</button>
}
11 changes: 11 additions & 0 deletions test/integration/mixed-directives/src/inline.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @ts-ignore externals
import ClientComponent from 'client-component'

export default function Page() {
async function inlineAction() {
'use server'
return 'inline-action'
}

return <ClientComponent action={inlineAction} />
}
6 changes: 6 additions & 0 deletions test/integration/mixed-directives/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"jsx": "react-jsx",
"target": "ES2022"
}
}

0 comments on commit 625880e

Please sign in to comment.