FIx #7136 UI: Admin is not able to edit an announcement (#7144)

* FIx #7136  UI: Admin is not able to edit an announcement

* Change edit check

* Addressing review comments
This commit is contained in:
Sachin Chaurasiya 2022-09-02 11:03:44 +05:30 committed by GitHub
parent 2f84bfbfa8
commit ccd3d1fcae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 20 deletions

View File

@ -43,6 +43,7 @@ jest.mock('../../../AppState', () => {
fullyQualifiedName: 'aaron_johnson0',
displayName: 'Aaron Johnson',
deleted: false,
isAdmin: true,
href: 'http://localhost:8585/api/v1/users/011bdb24-90a7-4a97-ba66-24002adb2b12',
teams: [{ id: '8754b53f-15cd-4d9a-af52-bdb3a2abffss' }],
};
@ -96,22 +97,6 @@ describe('Test Popover content component', () => {
expect(replyButton).toBe(null);
});
it('Should not render the edit button if user is not a author', async () => {
render(<PopoverContent {...mockProps} isAuthor={false} />);
const editButton = screen.queryByTestId('edit-message');
expect(editButton).toBe(null);
});
it('Should not render the delete button if user is not a author', async () => {
render(<PopoverContent {...mockProps} isAuthor={false} />);
const deleteButton = screen.queryByTestId('delete-message');
expect(deleteButton).toBe(null);
});
it('Should render reaction popover on click of reaction button', async () => {
render(<PopoverContent {...mockProps} />);
@ -162,4 +147,28 @@ describe('Test Popover content component', () => {
expect(onConfirmation).toBeCalled();
});
it('Announcement should be editable by admin user', async () => {
render(<PopoverContent {...mockProps} isAnnouncement isAuthor={false} />);
const editButton = await screen.findByTestId('edit-message');
expect(editButton).toBeInTheDocument();
fireEvent.click(editButton);
expect(onEdit).toBeCalled();
});
it('Announcement should be delete by admin user', async () => {
render(<PopoverContent {...mockProps} isAnnouncement isAuthor={false} />);
const deleteButton = await screen.findByTestId('delete-message');
expect(deleteButton).toBeInTheDocument();
fireEvent.click(deleteButton);
expect(onConfirmation).toBeCalled();
});
});

View File

@ -51,7 +51,6 @@ const PopoverContent: FC<Props> = ({
onReactionSelect,
onPopoverHide,
onEdit,
isAnnouncement,
}) => {
// get current user details
const currentUser = useMemo(
@ -69,7 +68,16 @@ const PopoverContent: FC<Props> = ({
setVisible(newVisible);
};
const deleteButtonCheck = threadId && postId && onConfirmation && isAuthor;
const deleteButtonCheck = useMemo(() => {
const baseCheck = Boolean(threadId && postId && onConfirmation);
return Boolean(baseCheck && (isAuthor || currentUser?.isAdmin));
}, [threadId, postId, onConfirmation, isAuthor, currentUser]);
const editCheck = useMemo(
() => isAuthor || currentUser?.isAdmin,
[isAuthor, currentUser]
);
const handleDelete = (e: React.MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();
@ -146,8 +154,6 @@ const PopoverContent: FC<Props> = ({
onEdit && onEdit();
};
const editCheck = (isAnnouncement || !isThread) && isAuthor;
return (
<Space>
<Popover