From 2bff63fa1811951e5be7a7b83c1f0c23af68c2e9 Mon Sep 17 00:00:00 2001 From: Chirag Madlani <12962843+chirag-madlani@users.noreply.github.com> Date: Thu, 1 Jun 2023 17:09:49 +0530 Subject: [PATCH] test(ui): unit tests for my data widget (#11841) * test(ui): unit tests for my data widget * update testid * fix mock * update no data placeholder message --- .../MyDataWidget/MyDataWidget.component.tsx | 4 +- .../MyData/MyDataWidget/MyDataWidget.test.tsx | 144 ++++++++++++++++++ .../ui/src/components/nav-bar/NavBar.tsx | 2 +- 3 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 openmetadata-ui/src/main/resources/ui/src/components/MyData/MyDataWidget/MyDataWidget.test.tsx diff --git a/openmetadata-ui/src/main/resources/ui/src/components/MyData/MyDataWidget/MyDataWidget.component.tsx b/openmetadata-ui/src/main/resources/ui/src/components/MyData/MyDataWidget/MyDataWidget.component.tsx index 0ec10709d8b..1cf1119ad07 100644 --- a/openmetadata-ui/src/main/resources/ui/src/components/MyData/MyDataWidget/MyDataWidget.component.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/components/MyData/MyDataWidget/MyDataWidget.component.tsx @@ -70,7 +70,7 @@ const MyDataWidgetInternal = () => { {data.length ? ( {t('label.view-all')}{' '} @@ -120,7 +120,7 @@ const MyDataWidgetInternal = () => { ); }) - : t('message.no-recently-viewed-date')} + : t('server.no-owned-entities')} diff --git a/openmetadata-ui/src/main/resources/ui/src/components/MyData/MyDataWidget/MyDataWidget.test.tsx b/openmetadata-ui/src/main/resources/ui/src/components/MyData/MyDataWidget/MyDataWidget.test.tsx new file mode 100644 index 00000000000..df1d740d290 --- /dev/null +++ b/openmetadata-ui/src/main/resources/ui/src/components/MyData/MyDataWidget/MyDataWidget.test.tsx @@ -0,0 +1,144 @@ +/* + * Copyright 2023 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 { MemoryRouter } from 'react-router-dom'; +import { getUserById } from 'rest/userAPI'; +import { MyDataWidget } from './MyDataWidget.component'; + +const userDetails = { + id: '123', +}; + +jest.mock('rest/userAPI', () => ({ + getUserById: jest.fn().mockImplementation(() => + Promise.resolve({ + owns: [], + }) + ), +})); + +jest.mock('utils/EntityUtils', () => ({ + getEntityName: jest.fn().mockImplementation((obj) => obj.name), +})); + +jest.mock('utils/TableUtils', () => ({ + getEntityLink: jest.fn().mockImplementation((link) => link), + getEntityIcon: jest.fn().mockImplementation((obj) => obj.name), +})); + +jest.mock('./../../../AppState', () => ({ + getCurrentUserDetails: jest.fn().mockImplementation(() => { + return userDetails; + }), +})); + +jest.mock( + 'components/Skeleton/MyData/EntityListSkeleton/EntityListSkeleton.component', + () => { + return jest.fn().mockImplementation(({ children }) => <>{children}); + } +); + +describe('MyDataWidget component', () => { + it('should fetch data', () => { + act(() => { + render(); + }); + + expect(getUserById).toHaveBeenCalledWith('123', 'owns'); + }); + + it('should render header', () => { + act(() => { + render( + + + + ); + }); + + expect(screen.getByText('label.my-data')).toBeInTheDocument(); + }); + + it('should not render view all for 0 length data', () => { + act(() => { + render( + + + + ); + }); + + expect(screen.queryByTestId('view-all-link')).not.toBeInTheDocument(); + }); + + it('should render view all for data present', async () => { + (getUserById as jest.Mock).mockImplementationOnce(() => + Promise.resolve({ + owns: [ + { + id: '1', + name: 'test 1', + fullyQualifiedName: 'test-1', + type: 'table', + }, + { + id: '2', + name: 'test 2', + fullyQualifiedName: 'test-2', + type: 'table', + }, + ], + }) + ); + act(() => { + render( + + + + ); + }); + + expect(await screen.findByText('label.view-all')).toBeInTheDocument(); + }); + + it('should render table names', async () => { + (getUserById as jest.Mock).mockResolvedValueOnce({ + owns: [ + { + id: '1', + name: 'test 1', + fullyQualifiedName: 'test-1', + type: 'table', + }, + { + id: '2', + name: 'test 2', + fullyQualifiedName: 'test-2', + type: 'table', + }, + ], + }); + act(() => { + render( + + + + ); + }); + + expect(await screen.findByText('test 1')).toBeInTheDocument(); + expect(await screen.findByText('test 2')).toBeInTheDocument(); + }); +}); diff --git a/openmetadata-ui/src/main/resources/ui/src/components/nav-bar/NavBar.tsx b/openmetadata-ui/src/main/resources/ui/src/components/nav-bar/NavBar.tsx index 43eda91e0e5..1348f1a3061 100644 --- a/openmetadata-ui/src/main/resources/ui/src/components/nav-bar/NavBar.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/components/nav-bar/NavBar.tsx @@ -336,7 +336,7 @@ const NavBar = ({