Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Macdonald <[email protected]>
  • Loading branch information
Parsifal-M committed Jun 2, 2024
1 parent 46b4d97 commit b3c05dd
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,200 +7,228 @@ import { openFeedbackBackendRef } from '../../api/types';
import { usePermission } from '@backstage/plugin-permission-react';

jest.mock('@backstage/plugin-permission-react', () => ({
usePermission: jest.fn(),
usePermission: jest.fn(),
}));

const mockOpenFeedbackBackendApi = {
getFeedback: () => Promise.resolve([
getFeedback: () =>
Promise.resolve([
{ id: 1, rating: 5, comment: 'Very good!, much test!', userRef: 'Baz' },
]),
removeFeedback: jest.fn(),
};

removeFeedback: jest.fn(),
};

const mockAlertApi = {
post: jest.fn(),
post: jest.fn(),
};


describe('FeedbackCards', () => {
it('renders a card with feedback', async () => {
(usePermission as jest.Mock).mockReturnValue({ allowed: true });
await act(async () => {
renderInTestApp(
<TestApiProvider apis={[
[alertApiRef, mockAlertApi],
[openFeedbackBackendRef, mockOpenFeedbackBackendApi]
]}>
<FeedbackCards />
</TestApiProvider>
);
});
expect(await screen.findByText('Very good!, much test!')).toBeInTheDocument();
it('renders a card with feedback', async () => {
(usePermission as jest.Mock).mockReturnValue({ allowed: true });
await act(async () => {
renderInTestApp(
<TestApiProvider
apis={[
[alertApiRef, mockAlertApi],
[openFeedbackBackendRef, mockOpenFeedbackBackendApi],
]}
>
<FeedbackCards />
</TestApiProvider>,
);
});
expect(
await screen.findByText('Very good!, much test!'),
).toBeInTheDocument();
});

it('renders an emoji for a rating', async () => {
(usePermission as jest.Mock).mockReturnValue({ allowed: true });
await act(async () => {
renderInTestApp(
<TestApiProvider apis={[
[alertApiRef, mockAlertApi],
[openFeedbackBackendRef, mockOpenFeedbackBackendApi]
]}>
<FeedbackCards />
</TestApiProvider>
);
});
const emoji = '🤩';
const userRef = 'Baz';
const title = new RegExp(`${userRef}.*${emoji}`);
expect(await screen.findByText(title)).toBeInTheDocument();
it('renders an emoji for a rating', async () => {
(usePermission as jest.Mock).mockReturnValue({ allowed: true });
await act(async () => {
renderInTestApp(
<TestApiProvider
apis={[
[alertApiRef, mockAlertApi],
[openFeedbackBackendRef, mockOpenFeedbackBackendApi],
]}
>
<FeedbackCards />
</TestApiProvider>,
);
});
const emoji = '🤩';
const userRef = 'Baz';
const title = new RegExp(`${userRef}.*${emoji}`);
expect(await screen.findByText(title)).toBeInTheDocument();
});

it('renders the delete dialog', async () => {
(usePermission as jest.Mock).mockReturnValue({ allowed: true });
await act(async () => {
renderInTestApp(
<TestApiProvider apis={[
[alertApiRef, mockAlertApi],
[openFeedbackBackendRef, mockOpenFeedbackBackendApi]
]}>
<FeedbackCards />
</TestApiProvider>
);
});
const deleteButton = await screen.findByTestId("delete-feedback-button");
fireEvent.click(deleteButton);
expect(await screen.findByText('Are you sure you want to delete this feedback?')).toBeInTheDocument();
it('renders the delete dialog', async () => {
(usePermission as jest.Mock).mockReturnValue({ allowed: true });
await act(async () => {
renderInTestApp(
<TestApiProvider
apis={[
[alertApiRef, mockAlertApi],
[openFeedbackBackendRef, mockOpenFeedbackBackendApi],
]}
>
<FeedbackCards />
</TestApiProvider>,
);
});
const deleteButton = await screen.findByTestId('delete-feedback-button');
fireEvent.click(deleteButton);
expect(
await screen.findByText('Are you sure you want to delete this feedback?'),
).toBeInTheDocument();
});

it('closes the feedback dialog and does not delete feedback when Cancel is clicked', async () => {
(usePermission as jest.Mock).mockReturnValue({ allowed: true });
await act(async () => {
renderInTestApp(
<TestApiProvider apis={[
[alertApiRef, mockAlertApi],
[openFeedbackBackendRef, mockOpenFeedbackBackendApi]
]}>
<FeedbackCards />
</TestApiProvider>
);
});
const deleteButton = await screen.findByTestId("delete-feedback-button");
fireEvent.click(deleteButton);
fireEvent.click(await screen.findByText('Cancel'));
expect(mockOpenFeedbackBackendApi.removeFeedback).not.toHaveBeenCalled();
await waitFor(() => {
expect(screen.queryByText('Are you sure you want to delete this feedback?')).not.toBeInTheDocument();
});
it('closes the feedback dialog and does not delete feedback when Cancel is clicked', async () => {
(usePermission as jest.Mock).mockReturnValue({ allowed: true });
await act(async () => {
renderInTestApp(
<TestApiProvider
apis={[
[alertApiRef, mockAlertApi],
[openFeedbackBackendRef, mockOpenFeedbackBackendApi],
]}
>
<FeedbackCards />
</TestApiProvider>,
);
});
const deleteButton = await screen.findByTestId('delete-feedback-button');
fireEvent.click(deleteButton);
fireEvent.click(await screen.findByText('Cancel'));
expect(mockOpenFeedbackBackendApi.removeFeedback).not.toHaveBeenCalled();
await waitFor(() => {
expect(
screen.queryByText('Are you sure you want to delete this feedback?'),
).not.toBeInTheDocument();
});
});

it('deletes feedback', async () => {
(usePermission as jest.Mock).mockReturnValue({ allowed: true });
await act(async () => {
renderInTestApp(
<TestApiProvider apis={[
[alertApiRef, mockAlertApi],
[openFeedbackBackendRef, mockOpenFeedbackBackendApi]
]}>
<FeedbackCards />
</TestApiProvider>
);
});
const deleteButton = await screen.findByTestId("delete-feedback-button");
fireEvent.click(deleteButton);
fireEvent.click(await screen.findByText('Delete'));
expect(mockOpenFeedbackBackendApi.removeFeedback).toHaveBeenCalledWith(1);
it('deletes feedback', async () => {
(usePermission as jest.Mock).mockReturnValue({ allowed: true });
await act(async () => {
renderInTestApp(
<TestApiProvider
apis={[
[alertApiRef, mockAlertApi],
[openFeedbackBackendRef, mockOpenFeedbackBackendApi],
]}
>
<FeedbackCards />
</TestApiProvider>,
);
});
const deleteButton = await screen.findByTestId('delete-feedback-button');
fireEvent.click(deleteButton);
fireEvent.click(await screen.findByText('Delete'));
expect(mockOpenFeedbackBackendApi.removeFeedback).toHaveBeenCalledWith(1);
});

it('throws an error if delete fails', async () => {
const errorMessage = 'Oops Something went wrong!';
(usePermission as jest.Mock).mockReturnValue({ allowed: true });
mockOpenFeedbackBackendApi.removeFeedback.mockImplementation(() => Promise.reject(new Error(errorMessage)));
await act(async () => {
renderInTestApp(
<TestApiProvider apis={[
[alertApiRef, mockAlertApi],
[openFeedbackBackendRef, mockOpenFeedbackBackendApi]
]}>
<FeedbackCards />
</TestApiProvider>
);
});
const deleteButton = await screen.findByTestId("delete-feedback-button");
fireEvent.click(deleteButton);
fireEvent.click(await screen.findByText('Delete'));

await waitFor(() => {
expect(mockAlertApi.post).toHaveBeenCalledWith({
message: `Failed to delete feedback: Error: ${errorMessage}`,
severity: 'error',
});
});
it('throws an error if delete fails', async () => {
const errorMessage = 'Oops Something went wrong!';
(usePermission as jest.Mock).mockReturnValue({ allowed: true });
mockOpenFeedbackBackendApi.removeFeedback.mockImplementation(() =>
Promise.reject(new Error(errorMessage)),
);
await act(async () => {
renderInTestApp(
<TestApiProvider
apis={[
[alertApiRef, mockAlertApi],
[openFeedbackBackendRef, mockOpenFeedbackBackendApi],
]}
>
<FeedbackCards />
</TestApiProvider>,
);
});
const deleteButton = await screen.findByTestId('delete-feedback-button');
fireEvent.click(deleteButton);
fireEvent.click(await screen.findByText('Delete'));

it('disables delete button when not allowed', async () => {
(usePermission as jest.Mock).mockReturnValue({ allowed: false });
await act(async () => {
renderInTestApp(
<TestApiProvider apis={[
[alertApiRef, mockAlertApi],
[openFeedbackBackendRef, mockOpenFeedbackBackendApi]
]}>
<FeedbackCards />
</TestApiProvider>
);
});
const deleteButton = await screen.findByTestId("delete-feedback-button");
expect(deleteButton).toBeDisabled();
await waitFor(() => {
expect(mockAlertApi.post).toHaveBeenCalledWith({
message: `Failed to delete feedback: Error: ${errorMessage}`,
severity: 'error',
});
});
});

it('renders feedback cards with respective userRef', async () => {
(usePermission as jest.Mock).mockReturnValue({ allowed: true });
mockOpenFeedbackBackendApi.getFeedback = () => Promise.resolve([
{ id: 1, rating: 5, comment: 'Very good!, much test!', userRef: 'Baz' },
{ id: 2, rating: 4, comment: 'Good job!', userRef: 'Anonymous' },
]);
await act(async () => {
renderInTestApp(
<TestApiProvider apis={[
[alertApiRef, mockAlertApi],
[openFeedbackBackendRef, mockOpenFeedbackBackendApi]
]}>
<FeedbackCards />
</TestApiProvider>
);
});
expect(await screen.findByText('Very good!, much test!')).toBeInTheDocument();
expect(await screen.findByText('Good job!')).toBeInTheDocument();
const emojiBaz = '🤩';
const emojiAnonymous = '😃';
const userRefBaz = 'Baz';
const userRefAnonymous = 'Anonymous';
const titleBaz = new RegExp(`${userRefBaz}.*${emojiBaz}`);
const titleAnonymous = new RegExp(`${userRefAnonymous}.*${emojiAnonymous}`);
expect(await screen.findByText(titleBaz)).toBeInTheDocument();
expect(await screen.findByText(titleAnonymous)).toBeInTheDocument();
it('disables delete button when not allowed', async () => {
(usePermission as jest.Mock).mockReturnValue({ allowed: false });
await act(async () => {
renderInTestApp(
<TestApiProvider
apis={[
[alertApiRef, mockAlertApi],
[openFeedbackBackendRef, mockOpenFeedbackBackendApi],
]}
>
<FeedbackCards />
</TestApiProvider>,
);
});
const deleteButton = await screen.findByTestId('delete-feedback-button');
expect(deleteButton).toBeDisabled();
});

it('posts an alert error when fails to fetch feedback', async () => {
const errorMessage = 'Oops Something went wrong!';
(usePermission as jest.Mock).mockReturnValue({ allowed: true });
mockOpenFeedbackBackendApi.getFeedback = () => Promise.reject(new Error(errorMessage));
await act(async () => {
renderInTestApp(
<TestApiProvider apis={[
[alertApiRef, mockAlertApi],
[openFeedbackBackendRef, mockOpenFeedbackBackendApi]
]}>
<FeedbackCards />
</TestApiProvider>
);
});
expect(mockAlertApi.post).toHaveBeenCalledWith({
message: `Failed to fetch feedback: Error: ${errorMessage}`,
severity: 'error',
});
it('renders feedback cards with respective userRef', async () => {
(usePermission as jest.Mock).mockReturnValue({ allowed: true });
mockOpenFeedbackBackendApi.getFeedback = () =>
Promise.resolve([
{ id: 1, rating: 5, comment: 'Very good!, much test!', userRef: 'Baz' },
{ id: 2, rating: 4, comment: 'Good job!', userRef: 'Anonymous' },
]);
await act(async () => {
renderInTestApp(
<TestApiProvider
apis={[
[alertApiRef, mockAlertApi],
[openFeedbackBackendRef, mockOpenFeedbackBackendApi],
]}
>
<FeedbackCards />
</TestApiProvider>,
);
});
expect(
await screen.findByText('Very good!, much test!'),
).toBeInTheDocument();
expect(await screen.findByText('Good job!')).toBeInTheDocument();
const emojiBaz = '🤩';
const emojiAnonymous = '😃';
const userRefBaz = 'Baz';
const userRefAnonymous = 'Anonymous';
const titleBaz = new RegExp(`${userRefBaz}.*${emojiBaz}`);
const titleAnonymous = new RegExp(`${userRefAnonymous}.*${emojiAnonymous}`);
expect(await screen.findByText(titleBaz)).toBeInTheDocument();
expect(await screen.findByText(titleAnonymous)).toBeInTheDocument();
});

});
it('posts an alert error when fails to fetch feedback', async () => {
const errorMessage = 'Oops Something went wrong!';
(usePermission as jest.Mock).mockReturnValue({ allowed: true });
mockOpenFeedbackBackendApi.getFeedback = () =>
Promise.reject(new Error(errorMessage));
await act(async () => {
renderInTestApp(
<TestApiProvider
apis={[
[alertApiRef, mockAlertApi],
[openFeedbackBackendRef, mockOpenFeedbackBackendApi],
]}
>
<FeedbackCards />
</TestApiProvider>,
);
});
expect(mockAlertApi.post).toHaveBeenCalledWith({
message: `Failed to fetch feedback: Error: ${errorMessage}`,
severity: 'error',
});
});
});
Loading

0 comments on commit b3c05dd

Please sign in to comment.