From 20525b9cc0973aac8c1b807779765c9e6a284c42 Mon Sep 17 00:00:00 2001 From: tkt <37575408+tktcorporation@users.noreply.github.com> Date: Mon, 10 Oct 2022 13:33:37 +0000 Subject: [PATCH] :sparkles: finish https://redwoodjs.com/docs/tutorial/chapter7/rbac#mocking-currentuser-for-jest --- web/src/components/Comment/Comment.test.tsx | 45 ++++++++++++++----- .../CommentsCell/CommentsCell.mock.ts | 2 + 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/web/src/components/Comment/Comment.test.tsx b/web/src/components/Comment/Comment.test.tsx index cf8ba5f66..de77db0ce 100644 --- a/web/src/components/Comment/Comment.test.tsx +++ b/web/src/components/Comment/Comment.test.tsx @@ -1,21 +1,44 @@ -import { render, screen } from '@redwoodjs/testing' +import { render, screen, waitFor } from '@redwoodjs/testing' import Comment from './Comment' +const COMMENT = { + id: 1, + name: 'John Doe', + body: 'This is my comment', + createdAt: '2020-01-02T12:34:56Z', + postId: 1, +} + describe('Comment', () => { it('renders successfully', () => { - const comment = { - name: 'John Doe', - body: 'This is my comment', - createdAt: '2020-01-02T12:34:56Z', - } - render() - - expect(screen.getByText(comment.name)).toBeInTheDocument() - expect(screen.getByText(comment.body)).toBeInTheDocument() + render() + + expect(screen.getByText(COMMENT.name)).toBeInTheDocument() + expect(screen.getByText(COMMENT.body)).toBeInTheDocument() const dateExpect = screen.getByText('2 January 2020') expect(dateExpect).toBeInTheDocument() expect(dateExpect.nodeName).toEqual('TIME') - expect(dateExpect).toHaveAttribute('datetime', comment.createdAt) + expect(dateExpect).toHaveAttribute('datetime', COMMENT.createdAt) + }) + + it('does not render a delete button if user is logged out', async () => { + render() + + await waitFor(() => + expect(screen.queryByText('Delete')).not.toBeInTheDocument() + ) + }) + + it('renders a delete button if the user is a moderator', async () => { + mockCurrentUser({ + roles: 'moderator', + id: 1, + email: 'moderator@moderator.com', + }) + + render() + + await waitFor(() => expect(screen.getByText('Delete')).toBeInTheDocument()) }) }) diff --git a/web/src/components/CommentsCell/CommentsCell.mock.ts b/web/src/components/CommentsCell/CommentsCell.mock.ts index 812c33a27..964dc729b 100644 --- a/web/src/components/CommentsCell/CommentsCell.mock.ts +++ b/web/src/components/CommentsCell/CommentsCell.mock.ts @@ -4,12 +4,14 @@ export const standard = () => ({ id: 1, name: 'Rob Cameron', body: 'First comment', + postId: 1, createdAt: '2020-01-02T12:34:56Z', }, { id: 2, name: 'David Price', body: 'Second comment', + postId: 2, createdAt: '2020-02-03T23:00:00Z', }, ],