Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds mockCurrentUser() to API-side Jest #1624

Merged
merged 2 commits into from
Jan 12, 2021
Merged

Adds mockCurrentUser() to API-side Jest #1624

merged 2 commits into from
Jan 12, 2021

Conversation

cannikin
Copy link
Member

@cannikin cannikin commented Jan 12, 2021

Thanks @peterp!

This is beautiful because the syntax matches mockCurrentUser() on the web-side:

// service
export const deleteComment = ({ id }) => {
  requireAuth({ role: 'moderator' })
  return db.comment.delete({
    where: { id },
  })
}

// test
scenario('deletes a comment', async (scenario) => {
  mockCurrentUser({ roles: ['moderator'] })

  const comment = await deleteComment({
    id: scenario.comment.jane.id,
  })
  expect(comment.id).toEqual(scenario.comment.jane.id)

  expect(
    await db.comment.findUnique({ where: { id: scenario.comment.jane.id } })
  ).toEqual(null)
})

Closes #680

@github-actions
Copy link

@cannikin cannikin requested a review from peterp January 12, 2021 01:39
Copy link
Contributor

@peterp peterp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aaaah, the best code is almost no code.

@peterp peterp added this to the next release milestone Jan 12, 2021
@peterp peterp merged commit 1176e00 into main Jan 12, 2021
@thedavidprice thedavidprice deleted the rc-api-mockcurrentuser branch January 12, 2021 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide a way to Mock user authentication in jest tests
2 participants