From 5364d731b3ec5c455bf5c89fcebad0f2ffa15a6c Mon Sep 17 00:00:00 2001 From: Harsh Vador Date: Tue, 20 Feb 2024 12:44:34 +0530 Subject: [PATCH 1/2] test: unit test case of notification box --- .../NotificationBox.component.tsx | 3 +- .../NotificationBox/NotificationBox.test.tsx | 166 ++++++++++++++++++ 2 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 openmetadata-ui/src/main/resources/ui/src/components/NotificationBox/NotificationBox.test.tsx diff --git a/openmetadata-ui/src/main/resources/ui/src/components/NotificationBox/NotificationBox.component.tsx b/openmetadata-ui/src/main/resources/ui/src/components/NotificationBox/NotificationBox.component.tsx index 731f4679bdc7..043ed358c658 100644 --- a/openmetadata-ui/src/main/resources/ui/src/components/NotificationBox/NotificationBox.component.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/components/NotificationBox/NotificationBox.component.tsx @@ -50,7 +50,6 @@ const NotificationBox = ({ const { currentUser } = useAuthContext(); const [notifications, setNotifications] = useState([]); const [isLoading, setIsLoading] = useState(false); - const [viewAllPath, setViewAllPath] = useState( getUserPath( currentUser?.name as string, @@ -191,7 +190,7 @@ const NotificationBox = ({ ); return ( -
+
({ + getFeedsWithFilter: jest + .fn() + .mockImplementation(() => Promise.resolve({ data: mockThread })), + getFilters: jest.fn().mockImplementation(() => ({ + threadType: ThreadType.Task, + feedFilter: FeedFilter.ASSIGNED_TO, + })), +})); + +jest.mock('../../utils/FeedUtils', () => ({ + getEntityFQN: jest.fn().mockReturnValue('entityFQN'), + getEntityType: jest.fn().mockReturnValue('entityType'), +})); + +jest.mock('./NotificationFeedCard.component', () => { + return jest.fn().mockReturnValue(

NotificationFeedCard

); +}); + +jest.mock('../Auth/AuthProviders/AuthProvider', () => ({ + useAuthContext: jest.fn(() => ({ + currentUser: mockUserData, + })), +})); + +jest.mock('../../utils/ToastUtils', () => ({ + showErrorToast: jest.fn().mockImplementation(() => mockShowErrorToast()), +})); + +describe('Test NotificationBox Component', () => { + it('should render NotificationBox', async () => { + await act(async () => { + render(); + }); + const notificationBox = await screen.findByTestId('notification-box'); + const notificationHeading = await screen.findByTestId( + 'notification-heading' + ); + + expect(notificationBox).toBeInTheDocument(); + expect(notificationHeading).toHaveTextContent('label.notification-plural'); + }); + + it('should render tabs', async () => { + await act(async () => { + render(); + }); + const tabs = await screen.findAllByRole('tab'); + + expect(tabs).toHaveLength(tabList.length); + }); + + it('should render error toast if any error occurs', async () => { + (getFeedsWithFilter as jest.Mock).mockRejectedValue(new Error()); + await act(async () => { + render(); + }); + + expect(mockShowErrorToast).toHaveBeenCalled(); + }); + + it('should call onMarkTaskNotificationRead when active tab is changed', async () => { + jest.useFakeTimers(); + + await act(async () => { + render(); + }); + const tabs = await screen.findAllByRole('tab'); + await act(async () => { + fireEvent.click(tabs[0]); + }); + jest.advanceTimersByTime(NOTIFICATION_READ_TIMER); + + expect(mockOnMarkTaskNotificationRead).toHaveBeenCalled(); + }); + + it('should render no data in case of no notification', async () => { + (getFeedsWithFilter as jest.Mock).mockImplementation(() => + Promise.resolve({ data: [] }) + ), + await act(async () => { + render(); + }); + + expect( + await screen.findByText('message.no-notification-found') + ).toBeInTheDocument(); + }); +}); From 6b5acac468a652c073245284373b01fd9d945f0b Mon Sep 17 00:00:00 2001 From: Harsh Vador Date: Tue, 20 Feb 2024 13:17:59 +0530 Subject: [PATCH 2/2] remove unnecessary assertion --- .../components/NotificationBox/NotificationBox.component.tsx | 2 +- .../ui/src/components/NotificationBox/NotificationBox.test.tsx | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/openmetadata-ui/src/main/resources/ui/src/components/NotificationBox/NotificationBox.component.tsx b/openmetadata-ui/src/main/resources/ui/src/components/NotificationBox/NotificationBox.component.tsx index 043ed358c658..76dae732cdb8 100644 --- a/openmetadata-ui/src/main/resources/ui/src/components/NotificationBox/NotificationBox.component.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/components/NotificationBox/NotificationBox.component.tsx @@ -190,7 +190,7 @@ const NotificationBox = ({ ); return ( -
+
{ await act(async () => { render(); }); - const notificationBox = await screen.findByTestId('notification-box'); const notificationHeading = await screen.findByTestId( 'notification-heading' ); - expect(notificationBox).toBeInTheDocument(); expect(notificationHeading).toHaveTextContent('label.notification-plural'); });