My app was started from the redwood-tutorial repo, but has since morphed and I am starting to add in more test coverage. I am adding a test for createPost, now that posts are being associated with a User.
I’ve finished the code, and the posts save correctly in the app. This is the function:
export const createPost = ({ input }) => {
return db.post.create({
data: { ...input, userId: context.currentUser.id },
})
To test this I am using the mockCurrentUser()
function, and setting up a test like this:
scenario('creates a post', async () => {
mockCurrentUser({ roles: 'admin' })
const result = await createPost({
input: {
title: 'Test Create Post',
body: 'String about a nice story for testing the createPost function on the api side.',
},
})
expect(result.title).toEqual('String')
expect(result.body).toEqual('String')
})
Here is the error I get in my testing output:
FAIL api api/src/services/posts/posts.test.js
posts
✓ returns all posts (136 ms)
✓ returns a single post (46 ms)
✕ creates a post (50 ms)
✓ updates a post (27 ms)
✓ deletes a post (23 ms)
● posts › creates a post
Invalid `db.post.create()` invocation in
/Users/benstraw/dev/benstraw/grrquarterly/api/src/services/posts/posts.js:14:18
11 }
12
13 export const createPost = ({ input }) => {
→ 14 return db.post.create({
data: {
title: 'Test Create Post',
body: 'String about a nice story for testing the createPost function on the api side.',
userId: undefined,
+ user: {
+ create?: UserCreateWithoutPostsInput | UserUncheckedCreateWithoutPostsInput,
+ connectOrCreate?: UserCreateOrConnectWithoutPostsInput,
+ connect?: UserWhereUniqueInput
+ },
? createdAt?: DateTime,
? comments?: {
? create?: CommentCreateWithoutPostInput | CommentCreateWithoutPostInput | CommentUncheckedCreateWithoutPostInput | CommentUncheckedCreateWithoutPostInput,
? connectOrCreate?: CommentCreateOrConnectWithoutPostInput | CommentCreateOrConnectWithoutPostInput,
? connect?: CommentWhereUniqueInput | CommentWhereUniqueInput
? }
}
})
Argument user for data.user is missing.
Note: Lines with + are required, lines with ? are optional.
I have tried a bunch of different scenario options, the one for this test run was:
export const standard = defineScenario({
post: {
one: {
data: {
title: 'String',
body: 'String',
user: {
create: {
email: 'admin@admin.com',
name: 'Admin',
roles: 'admin',
hashedPassword: 'String',
salt: 'String',
},
},
},
},
two: {
data: {
title: 'String',
body: 'String',
user: {
create: {
email: 'moderator@moderator.com',
name: 'Moderator',
roles: 'moderator',
hashedPassword: 'String',
salt: 'String',
},
},
},
},
},
})
I am new to redwood, but have good familiarity with js and web apps in general, so forgive me if I am just missing something dumb.
Thanks.