Skip to content

Commit

Permalink
refactor(scaffold): restore default functionality and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
esteban-url committed Dec 30, 2024
1 parent 4263d32 commit ce10dcf
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,118 @@ export default ScaffoldLayout
"
`;

exports[`custom templates > creates a service 1`] = `
"import type {
QueryResolvers,
MutationResolvers,
PostRelationResolvers,
} from 'types/graphql'

import { db } from 'src/lib/db'

export const posts: QueryResolvers['posts'] = () => {
return db.post.findMany()
}

export const post: QueryResolvers['post'] = ({ id }) => {
return db.post.findUnique({
where: { id },
})
}

export const createPost: MutationResolvers['createPost'] = ({ input }) => {
return db.post.create({
data: input,
})
}

export const updatePost: MutationResolvers['updatePost'] = ({ id, input }) => {
return db.post.update({
data: input,
where: { id },
})
}

export const deletePost: MutationResolvers['deletePost'] = ({ id }) => {
return db.post.delete({
where: { id },
})
}

export const Post: PostRelationResolvers = {
favorites: (_obj, { root }) => {
return db.post.findUnique({ where: { id: root?.id } }).favorites()
},
tag: (_obj, { root }) => {
return db.post.findUnique({ where: { id: root?.id } }).tag()
},
}
"
`;

exports[`custom templates > creates a service test 1`] = `
"import type { Post } from '@prisma/client'

import { posts, post, createPost, updatePost, deletePost } from './posts'
import type { StandardScenario } from './posts.scenarios'

// Generated boilerplate tests do not account for all circumstances
// and can fail without adjustments, e.g. Float.
// Please refer to the RedwoodJS Testing Docs:
// https://redwoodjs.com/docs/testing#testing-services
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations

describe('posts', () => {
scenario('returns all posts', async (scenario: StandardScenario) => {
const result = await posts()

expect(result.length).toEqual(Object.keys(scenario.post).length)
})

scenario('returns a single post', async (scenario: StandardScenario) => {
const result = await post({ id: scenario.post.one.id })

expect(result).toEqual(scenario.post.one)
})

scenario('creates a post', async () => {
const result = await createPost({
input: {
title: 'String',
slug: 'String1234567',
author: 'String',
body: 'String',
metadata: { foo: 'bar' },
},
})

expect(result.title).toEqual('String')
expect(result.slug).toEqual('String1234567')
expect(result.author).toEqual('String')
expect(result.body).toEqual('String')
expect(result.metadata).toEqual({ foo: 'bar' })
})

scenario('updates a post', async (scenario: StandardScenario) => {
const original = (await post({ id: scenario.post.one.id })) as Post
const result = await updatePost({
id: original.id,
input: { title: 'String2' },
})

expect(result.title).toEqual('String2')
})

scenario('deletes a post', async (scenario: StandardScenario) => {
const original = (await deletePost({ id: scenario.post.one.id })) as Post
const result = await post({ id: original.id })

expect(result).toEqual(null)
})
})
"
`;

exports[`custom templates > creates an sdl 1`] = `
"export const schema = gql\`
type Post {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ describe('in javascript (default) mode', () => {
files = await scaffold.files({
...getDefaultArgs(defaults),
model: 'Post',
tests: true,
nestScaffoldByModel: true,
})
})

test('returns exactly 48 files', async () => {
expect(Object.keys(files).length).toEqual(48)
test('returns exactly 19 files', async () => {
expect(Object.keys(files).length).toEqual(19)
})
// SDL

Expand Down Expand Up @@ -299,7 +298,6 @@ describe('in javascript (default) mode', () => {
scaffold.files({
...getDefaultArgs(defaults),
model: 'NoEditableField',
tests: true,
nestScaffoldByModel: true,
}),
).rejects.toThrow(
Expand Down Expand Up @@ -461,13 +459,12 @@ describe('in typescript mode', () => {
...getDefaultArgs(defaults),
model: 'Post',
typescript: true,
tests: true,
nestScaffoldByModel: true,
})
})

test('returns exactly 48 files', () => {
expect(Object.keys(tsFiles).length).toEqual(48)
test('returns exactly 19 files', () => {
expect(Object.keys(tsFiles).length).toEqual(19)
})

// SDL
Expand Down Expand Up @@ -962,8 +959,10 @@ describe('custom templates', () => {
force: false,
model: 'Post',
typescript: true,
tests: true,
nestScaffoldByModel: true,
tests: true,
stories: true,
serviceTests: true,
})
})

Expand Down Expand Up @@ -1100,6 +1099,24 @@ describe('custom templates', () => {
).toMatchSnapshot()
})

// Service

test('creates a service', () => {
expect(
tsFiles[
path.normalize('/path/to/project/api/src/services/posts/posts.ts')
],
).toMatchSnapshot()
})

test('creates a service test', () => {
expect(
tsFiles[
path.normalize('/path/to/project/api/src/services/posts/posts.test.ts')
],
).toMatchSnapshot()
})

// Layout
// (Including this in the test just to make sure we're testing at least one
// web-side file that we don't have a custom template for)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ describe('in javascript (default) mode', () => {
files = await scaffold.files({
...getDefaultArgs(defaults),
model: 'Post',
tests: true,
nestScaffoldByModel: false,
})
})

test('returns exactly 48 files', () => {
expect(Object.keys(files).length).toEqual(48)
test('returns exactly 19 files', () => {
expect(Object.keys(files).length).toEqual(19)
})
// SDL

Expand Down Expand Up @@ -303,13 +302,12 @@ describe('in typescript mode', () => {
...getDefaultArgs(defaults),
model: 'Post',
typescript: true,
tests: true,
nestScaffoldByModel: false,
})
})

test('returns exactly 48 files', () => {
expect(Object.keys(tsFiles).length).toEqual(48)
test('returns exactly 19 files', () => {
expect(Object.keys(tsFiles).length).toEqual(19)
})

// SDL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ describe('admin/post', () => {
filesLower = await scaffold.files({
model: 'Post',
path: 'admin',
tests: true,
nestScaffoldByModel: true,
})
})

describe('creates the correct files with the correct imports', () => {
it('returns exactly 48 files', () => {
expect(Object.keys(filesLower).length).toEqual(48)
it('returns exactly 19 files', () => {
expect(Object.keys(filesLower).length).toEqual(19)
})

// Layout
Expand Down Expand Up @@ -353,14 +352,13 @@ describe('Admin/Post', () => {
filesUpper = await scaffold.files({
model: 'Post',
path: 'Admin',
tests: true,
nestScaffoldByModel: true,
})
})

describe('creates the correct files with the correct imports', () => {
it('returns exactly 48 files', () => {
expect(Object.keys(filesUpper).length).toEqual(48)
it('returns exactly 19 files', () => {
expect(Object.keys(filesUpper).length).toEqual(19)
})

// Layout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ describe('admin/pages/post', () => {
filesNestedLower = await scaffold.files({
model: 'Post',
path: 'admin/pages',
tests: true,
nestScaffoldByModel: true,
})
})

describe('creates the correct files with the correct imports', () => {
test('returns exactly 48 files', () => {
expect(Object.keys(filesNestedLower).length).toEqual(48)
test('returns exactly 19 files', () => {
expect(Object.keys(filesNestedLower).length).toEqual(19)
})

// Layout
Expand Down Expand Up @@ -363,14 +362,13 @@ describe('Admin/Pages/Post/Post', () => {
filesNestedUpper = await scaffold.files({
model: 'Post',
path: 'Admin/Pages',
tests: true,
nestScaffoldByModel: true,
})
})

describe('creates the correct files with the correct imports', () => {
test('returns exactly 48 files', () => {
expect(Object.keys(filesNestedUpper).length).toEqual(48)
test('returns exactly 19 files', () => {
expect(Object.keys(filesNestedUpper).length).toEqual(19)
})

// Layout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ describe('admin/pages/post', () => {
filesNestedLower = await scaffold.files({
model: 'Post',
path: 'admin/pages',
tests: true,
nestScaffoldByModel: false,
})
})

describe('creates the correct files with the correct imports', () => {
test('returns exactly 48 files', () => {
expect(Object.keys(filesNestedLower).length).toEqual(48)
test('returns exactly 19 files', () => {
expect(Object.keys(filesNestedLower).length).toEqual(19)
})

// Layout
Expand Down Expand Up @@ -355,14 +354,13 @@ describe('Admin/Pages/Post/Post', () => {
filesNestedUpper = await scaffold.files({
model: 'Post',
path: 'Admin/Pages',
tests: true,
nestScaffoldByModel: false,
})
})

describe('creates the correct files with the correct imports', () => {
test('returns exactly 48 files', () => {
expect(Object.keys(filesNestedUpper).length).toEqual(48)
test('returns exactly 19 files', () => {
expect(Object.keys(filesNestedUpper).length).toEqual(19)
})

// Layout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ describe('AdminPages/Post', () => {
filesMultiwordUpper = await scaffold.files({
model: 'Post',
path: 'AdminPages',
tests: true,
nestScaffoldByModel: true,
})
})

describe('creates the correct files with the correct imports', () => {
test('returns exactly 48 files', () => {
expect(Object.keys(filesMultiwordUpper).length).toEqual(48)
test('returns exactly 19 files', () => {
expect(Object.keys(filesMultiwordUpper).length).toEqual(19)
})

// Layout
Expand Down Expand Up @@ -363,14 +362,13 @@ describe('admin-pages/Post', () => {
filesMultiwordDash = await scaffold.files({
model: 'Post',
path: 'admin-pages',
tests: true,
nestScaffoldByModel: true,
})
})

describe('creates the correct files with the correct imports', () => {
test('returns exactly 48 files', () => {
expect(Object.keys(filesMultiwordDash).length).toEqual(48)
test('returns exactly 19 files', () => {
expect(Object.keys(filesMultiwordDash).length).toEqual(19)
})

// Layout
Expand Down Expand Up @@ -704,14 +702,13 @@ describe('admin_pages/Post', () => {
filesMultiwordUnderscore = await scaffold.files({
model: 'Post',
path: 'admin_pages',
tests: true,
nestScaffoldByModel: true,
})
})

describe('creates the correct files with the correct imports', () => {
test('returns exactly 48 files', () => {
expect(Object.keys(filesMultiwordUnderscore).length).toEqual(48)
test('returns exactly 19 files', () => {
expect(Object.keys(filesMultiwordUnderscore).length).toEqual(19)
})

// Layout
Expand Down
Loading

0 comments on commit ce10dcf

Please sign in to comment.