Skip to content

Commit

Permalink
RSC: Insert 'use client' in scaffolded components (#9998)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe authored Feb 13, 2024
1 parent e5f09e4 commit 0ef0289
Show file tree
Hide file tree
Showing 19 changed files with 400 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { files } from '../../../generate/scaffold/scaffold'
import { tasks } from '../scaffold'

vi.mock('fs', async () => ({ default: (await import('memfs')).fs }))
vi.mock('fs-extra')
vi.mock('execa')

Expand All @@ -32,8 +33,9 @@ vi.mock('../../../../lib/schemaHelpers', async (importOriginal) => {
const path = require('path')
return {
...originalSchemaHelpers,
getSchema: () =>
require(path.join(globalThis.__dirname, 'fixtures', 'post.json')),
getSchema: () => {
return require(path.join(globalThis.__dirname, 'fixtures', 'post.json'))
},
}
})

Expand Down Expand Up @@ -64,15 +66,21 @@ templateDirectories.forEach((directory) => {
describe('rw destroy scaffold', () => {
describe('destroy scaffold post', () => {
beforeEach(async () => {
vol.fromJSON(scaffoldTemplates)
// This fs is needed for the `files` function imported from `generate`
vol.fromJSON({ 'redwood.toml': '', ...scaffoldTemplates }, '/')

const postFiles = await files({
...getDefaultArgs(defaults),
model: 'Post',
tests: false,
nestScaffoldByModel: true,
})

// This fs is needed for all the tests here
vol.fromJSON({
'redwood.toml': '',
...scaffoldTemplates,
...(await files({
...getDefaultArgs(defaults),
model: 'Post',
tests: false,
nestScaffoldByModel: true,
})),
...postFiles,
[getPaths().web.routes]: [
'<Routes>',
' <Route path="/posts/new" page={NewPostPage} name="newPost" />',
Expand Down Expand Up @@ -118,8 +126,7 @@ describe('rw destroy scaffold', () => {
beforeEach(async () => {
// clear filesystem so files call works as expected
vol.reset()
vol.fromJSON(scaffoldTemplates)

vol.fromJSON({ 'redwood.toml': '', ...scaffoldTemplates }, '/')
vol.fromJSON({
...scaffoldTemplates,
...(await files({
Expand Down Expand Up @@ -193,8 +200,9 @@ describe('rw destroy scaffold', () => {

describe('destroy namespaced scaffold post', () => {
beforeEach(async () => {
vol.fromJSON(scaffoldTemplates)
// vol.fromJSON(scaffoldTemplates)
vol.fromJSON({
'redwood.toml': '',
...scaffoldTemplates,
...(await files({
...getDefaultArgs(defaults),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { files } from '../../../generate/scaffold/scaffold'
import { tasks } from '../scaffold'

vi.mock('fs', async () => ({ default: (await import('memfs')).fs }))
vi.mock('fs-extra')
vi.mock('execa')

Expand Down Expand Up @@ -64,30 +65,38 @@ templateDirectories.forEach(async (directory) => {
describe('rw destroy scaffold', () => {
describe('destroy scaffold post', () => {
beforeEach(async () => {
vol.fromJSON(scaffoldTemplates)
vol.fromJSON({
...scaffoldTemplates,
...(await files({
...getDefaultArgs(defaults),
model: 'Post',
tests: false,
nestScaffoldByModel: false,
})),
[getPaths().web.routes]: [
'<Routes>',
' <Route path="/posts/new" page={NewPostPage} name="newPost" />',
' <Route path="/posts/{id:Int}/edit" page={EditPostPage} name="editPost" />',
' <Route path="/posts/{id:Int}" page={PostPage} name="post" />',
' <Route path="/posts" page={PostsPage} name="posts" />',
' <Route path="/" page={HomePage} name="home" />',
' <Route notfound page={NotFoundPage} />',
'</Routes>',
].join('\n'),
// This fs is needed for the `files` function imported from `generate`
vol.fromJSON({ 'redwood.toml': '', ...scaffoldTemplates }, '/')

const postFiles = await files({
...getDefaultArgs(defaults),
model: 'Post',
tests: false,
nestScaffoldByModel: false,
})

// This fs is needed for all the tests here
vol.fromJSON(
{
...scaffoldTemplates,
...postFiles,
[getPaths().web.routes]: [
'<Routes>',
' <Route path="/posts/new" page={NewPostPage} name="newPost" />',
' <Route path="/posts/{id:Int}/edit" page={EditPostPage} name="editPost" />',
' <Route path="/posts/{id:Int}" page={PostPage} name="post" />',
' <Route path="/posts" page={PostsPage} name="posts" />',
' <Route path="/" page={HomePage} name="home" />',
' <Route notfound page={NotFoundPage} />',
'</Routes>',
].join('\n'),
},
'/'
)
})

afterEach(() => {
vol.fromJSON(scaffoldTemplates)
vol.fromJSON({ 'redwood.toml': '', ...scaffoldTemplates }, '/')
vi.spyOn(fs, 'unlinkSync').mockClear()
})

Expand All @@ -109,15 +118,15 @@ describe('rw destroy scaffold', () => {
nestScaffoldByModel: false,
})
)
expect(generatedFiles.length).toEqual(unlinkSpy.mock.calls.length)
generatedFiles.forEach((f) => expect(unlinkSpy).toHaveBeenCalledWith(f))
expect(generatedFiles.length).toEqual(unlinkSpy.mock.calls.length)
})
})

describe('for typescript files', () => {
beforeEach(async () => {
vol.reset()
vol.fromJSON(scaffoldTemplates)
vol.fromJSON({ 'redwood.toml': '', ...scaffoldTemplates }, '/')
vol.fromJSON({
...scaffoldTemplates,
...(await files({
Expand Down Expand Up @@ -191,6 +200,7 @@ describe('rw destroy scaffold', () => {

describe('destroy namespaced scaffold post', () => {
beforeEach(async () => {
vol.fromJSON({ 'redwood.toml': '' }, '/')
vol.fromJSON({
...scaffoldTemplates,
...(await files({
Expand Down Expand Up @@ -244,6 +254,7 @@ describe('rw destroy scaffold', () => {

describe('for typescript files', () => {
beforeEach(async () => {
vol.fromJSON({ 'redwood.toml': '' }, '/')
vol.fromJSON({
...scaffoldTemplates,
...(await files({
Expand Down
Loading

0 comments on commit 0ef0289

Please sign in to comment.