Skip to content

Commit

Permalink
test: unit test cases of right side bar (open-metadata#15123)
Browse files Browse the repository at this point in the history
* test: unit test cases of announcements widget

* test: unit test case of following widget
  • Loading branch information
harsh-vador authored Feb 12, 2024
1 parent f6e5de3 commit 6cfa986
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2024 Collate.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { act, render, screen } from '@testing-library/react';
import React from 'react';
import { TaskType, ThreadType } from '../../../generated/api/feed/createThread';
import AnnouncementsWidget from './AnnouncementsWidget';

const mockAnnouncementData = [
{
id: 'f0761441-478e-4373-919e-70b8f587c43f',
type: ThreadType.Announcement,
href: 'http://localhost:8585/api/v1/feed/f0761441-478e-4373-919e-70b8f587c43f',
threadTs: 1707475672423,
about: '<#E::tableu>',
entityId: '07f9dc02-9cbd-447c-9fc9-988c419a45e0',
updatedAt: 1707475792808,
resolved: true,
message: 'wertyui',
announcement: {
description: 'description',
startTime: 1707475665,
endTime: 1707648467,
},
task: {
id: 1,
assignees: [
{
id: '1',
type: 'user',
},
],
type: TaskType.RequestDescription,
},
},
];

jest.mock(
'../../ActivityFeed/ActivityFeedCard/FeedCardBody/FeedCardBodyV1',
() => {
return jest.fn().mockImplementation(() => <div>FeedCardBodyV1</div>);
}
);

jest.mock(
'../../ActivityFeed/ActivityFeedCard/FeedCardHeader/FeedCardHeaderV1',
() => {
return jest.fn().mockImplementation(() => <div>FeedCardHeaderV1</div>);
}
);

jest.mock('../../Loader/Loader', () =>
jest.fn().mockReturnValue(<div>Loader</div>)
);

describe('AnnouncementsWidget', () => {
it('should render Announcements Widget', () => {
const { container } = render(<AnnouncementsWidget widgetKey="testKey" />);

expect(container).toBeTruthy();
});

it('should render loading state', () => {
render(<AnnouncementsWidget isAnnouncementLoading widgetKey="testKey" />);

expect(screen.getByText('Loader')).toBeInTheDocument();
});

it('should render empty state', () => {
render(<AnnouncementsWidget announcements={[]} widgetKey="testKey" />);

expect(screen.getByTestId('no-data-placeholder')).toBeInTheDocument();
});

it('should render announcements', async () => {
await act(async () => {
render(
<AnnouncementsWidget
announcements={mockAnnouncementData}
widgetKey="testKey"
/>
);
});

expect(screen.getByText('FeedCardBodyV1')).toBeInTheDocument();
expect(screen.getByText('FeedCardHeaderV1')).toBeInTheDocument();
expect(screen.getByText('label.announcement')).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2024 Collate.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { act, fireEvent, render, screen } from '@testing-library/react';
import React from 'react';
import FollowingWidget from './FollowingWidget';

jest.mock('../../Auth/AuthProviders/AuthProvider', () => ({
useAuthContext: jest.fn().mockImplementation(() => ({
currentUser: { name: 'testUser' },
})),
}));

jest.mock('react-router-dom', () => ({
Link: jest
.fn()
.mockImplementation(({ children }: { children: React.ReactNode }) => (
<p data-testid="link">{children}</p>
)),
}));

const mockProps = {
followedData: [
{
id: '1',
type: 'data',
name: 'Data 1',
description: 'Description 1',
displayName: 'Data 1',
fullyQualifiedName: 'org.data.1',
href: '/entity/1',
deleted: false,
inherited: false,
},
],
followedDataCount: 1,
isLoadingOwnedData: false,
widgetKey: 'testKey',
};

describe('FollowingWidget', () => {
it('should render Following Widget', () => {
render(<FollowingWidget {...mockProps} />);

expect(screen.getByTestId('following-widget')).toBeInTheDocument();
expect(
screen.getByTestId('following-data-total-count')
).toBeInTheDocument();
expect(screen.getByText('label.following')).toBeInTheDocument();
});

it('should render loading state', () => {
render(<FollowingWidget {...mockProps} isLoadingOwnedData />);

expect(screen.getByTestId('entity-list-skeleton')).toBeInTheDocument();
});

it('should render empty state', () => {
render(<FollowingWidget {...mockProps} followedData={[]} />);

expect(screen.getByTestId('no-data-placeholder')).toBeInTheDocument();
expect(
screen.getByText('message.not-followed-anything')
).toBeInTheDocument();
});

it('should remove widget when close button is clicked', async () => {
const handleRemoveWidget = jest.fn();

render(
<FollowingWidget
{...mockProps}
isEditView
handleRemoveWidget={handleRemoveWidget}
/>
);
await act(async () => {
fireEvent.click(screen.getByTestId('remove-widget-button'));
});

expect(handleRemoveWidget).toHaveBeenCalledWith('testKey');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const EntityListSkeleton = ({
...props
}: EntityListSkeletonProps) => {
return loading ? (
<div className="m-t-md">
<div className="m-t-md" data-testid="entity-list-skeleton">
{getSkeletonMockData(dataLength).map(() => (
<LabelCountSkeleton
active
Expand Down

0 comments on commit 6cfa986

Please sign in to comment.