Skip to content

Commit

Permalink
chore(rsc): Switch last remaining transform-server test to inline sna…
Browse files Browse the repository at this point in the history
…pshot (redwoodjs#11240)
  • Loading branch information
Tobbe authored Aug 14, 2024
1 parent 25630e9 commit b7488e4
Showing 1 changed file with 17 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,41 +245,25 @@ describe('rscTransformUseServerPlugin module scoped "use server"', () => {
'settings.json',
\`{ "delay": \${formData.get('delay')} }\n\`
)
}`
}`.trim()

const output = await pluginTransform(input, id)

if (typeof output !== 'string') {
throw new Error('Expected output to be a string')
}

// Check that the file has a "use server" directive at the top
// Comments and other directives are allowed before it.
// Maybe also imports, I'm not sure, but am going to allow it for now. If
// someone finds a problem with that, we can revisit.
const outputLines = output.split('\n')
const firstCodeLineIndex = outputLines.findIndex(
(line) =>
line.startsWith('export ') ||
line.startsWith('async ') ||
line.startsWith('function ') ||
line.startsWith('const ') ||
line.startsWith('let ') ||
line.startsWith('var '),
)
expect(
outputLines
.slice(0, firstCodeLineIndex)
.some((line) => line.startsWith('"use server"')),
).toBeTruthy()
expect(output).toContain(
'import {registerServerReference} from "react-server-dom-webpack/server";',
)
expect(output).toContain(
`registerServerReference(formAction,"${id}","default");`,
)
// One import and (exactly) one call to registerServerReference, so two
// matches
expect(output.match(/registerServerReference/g)).toHaveLength(2)
expect(output).toMatchInlineSnapshot(`
"'use server'
import fs from 'node:fs'
export async function formAction(formData: FormData) {
await fs.promises.writeFile(
'settings.json',
\`{ "delay": \${formData.get('delay')} }\`
)
}
import {registerServerReference} from "react-server-dom-webpack/server";
registerServerReference(formAction,"some/path/to/actions.ts","formAction");
"
`)
})
})

0 comments on commit b7488e4

Please sign in to comment.