diff --git a/openmetadata-ui/src/main/resources/ui/src/components/Modals/SchemaModal/SchemaModal.test.tsx b/openmetadata-ui/src/main/resources/ui/src/components/Modals/SchemaModal/SchemaModal.test.tsx new file mode 100644 index 00000000000..57606cc7fbd --- /dev/null +++ b/openmetadata-ui/src/main/resources/ui/src/components/Modals/SchemaModal/SchemaModal.test.tsx @@ -0,0 +1,92 @@ +/* + * Copyright 2021 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 { fireEvent, render } from '@testing-library/react'; +import React from 'react'; +import ReactDOM from 'react-dom'; +import { MemoryRouter } from 'react-router-dom'; +import SchemaModal from './SchemaModal'; + +const onClose = jest.fn(); + +const mockProp = { + onClose, + data: {}, +}; + +jest.mock('../../schema-editor/SchemaEditor', () => { + return jest.fn().mockReturnValue(
SchemaEditor
); +}); + +describe('Test Schema modal component', () => { + beforeAll(() => { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + ReactDOM.createPortal = jest.fn().mockImplementation((element, _node) => { + return element; + }); + }); + + it('Should render schema modal component', async () => { + const { findByTestId } = render(, { + wrapper: MemoryRouter, + }); + + const modalContainer = await findByTestId('schema-modal'); + + expect(modalContainer).toBeInTheDocument(); + + const modalBackdrop = await findByTestId('schema-modal-backdrop'); + + expect(modalBackdrop).toBeInTheDocument(); + + const header = await findByTestId('schema-modal-header'); + + expect(header).toBeInTheDocument(); + + const modalCloseButton = await findByTestId('schema-modal-close-button'); + + expect(modalCloseButton).toBeInTheDocument(); + + const modalBody = await findByTestId('schem-modal-body'); + + expect(modalBody).toBeInTheDocument(); + }); + + it('Should call onClose method on click of backedrop', async () => { + const { findByTestId } = render(, { + wrapper: MemoryRouter, + }); + + const modalBackdrop = await findByTestId('schema-modal-backdrop'); + + expect(modalBackdrop).toBeInTheDocument(); + + fireEvent.click(modalBackdrop); + + expect(onClose).toBeCalled(); + }); + + it('Should call onClose method on click of close button', async () => { + const { findByTestId } = render(, { + wrapper: MemoryRouter, + }); + + const modalCloseButton = await findByTestId('schema-modal-close-button'); + + expect(modalCloseButton).toBeInTheDocument(); + + fireEvent.click(modalCloseButton); + + expect(onClose).toBeCalled(); + }); +}); diff --git a/openmetadata-ui/src/main/resources/ui/src/components/Modals/SchemaModal/SchemaModal.tsx b/openmetadata-ui/src/main/resources/ui/src/components/Modals/SchemaModal/SchemaModal.tsx new file mode 100644 index 00000000000..c6d6ad2c1b8 --- /dev/null +++ b/openmetadata-ui/src/main/resources/ui/src/components/Modals/SchemaModal/SchemaModal.tsx @@ -0,0 +1,74 @@ +/* + * Copyright 2021 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 classNames from 'classnames'; +import React, { FC, HTMLAttributes } from 'react'; +import ReactDOM from 'react-dom'; +import SchemaEditor from '../../schema-editor/SchemaEditor'; + +interface Prop extends HTMLAttributes { + onClose: () => void; + // eslint-disable-next-line + data: any; +} + +const SchemaModal: FC = ({ className, onClose, data }) => { + return ReactDOM.createPortal( + +
+
+
+

+ JSON data +

+ +
+ + + +
+
+
+ +
+
+
, + document.body + ); +}; + +export default SchemaModal; diff --git a/openmetadata-ui/src/main/resources/ui/src/components/SampleDataTable/RowData.test.tsx b/openmetadata-ui/src/main/resources/ui/src/components/SampleDataTable/RowData.test.tsx new file mode 100644 index 00000000000..34f18b757aa --- /dev/null +++ b/openmetadata-ui/src/main/resources/ui/src/components/SampleDataTable/RowData.test.tsx @@ -0,0 +1,63 @@ +/* + * Copyright 2021 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 { fireEvent, render } from '@testing-library/react'; +import React from 'react'; +import { MemoryRouter } from 'react-router-dom'; +import { RowData } from './RowData'; + +jest.mock('../Modals/SchemaModal/SchemaModal', () => { + return jest + .fn() + .mockReturnValue(
Schema Modal
); +}); + +const mockProp = { + data: {}, +}; + +describe('Test RowData Component', () => { + it('Should render json data if data is typeof object', async () => { + const { findByTestId } = render(, { + wrapper: MemoryRouter, + }); + + const jsonData = await findByTestId('json-object'); + + expect(jsonData).toBeInTheDocument(); + + fireEvent.click(jsonData); + + expect(await findByTestId('schema-modal')).toBeInTheDocument(); + }); + + it('Should render string data if data is not a tyeopf object', async () => { + const { findByTestId } = render(, { + wrapper: MemoryRouter, + }); + + const stringData = await findByTestId('string-data'); + + expect(stringData).toBeInTheDocument(); + }); + + it('Should render fallback data if no data is there', async () => { + const { findByTestId } = render(, { + wrapper: MemoryRouter, + }); + + const emptyData = await findByTestId('empty-data'); + + expect(emptyData).toBeInTheDocument(); + }); +}); diff --git a/openmetadata-ui/src/main/resources/ui/src/components/SampleDataTable/RowData.tsx b/openmetadata-ui/src/main/resources/ui/src/components/SampleDataTable/RowData.tsx new file mode 100644 index 00000000000..001417d6a4c --- /dev/null +++ b/openmetadata-ui/src/main/resources/ui/src/components/SampleDataTable/RowData.tsx @@ -0,0 +1,46 @@ +/* + * Copyright 2021 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 React, { Fragment, useState } from 'react'; +import SchemaModal from '../Modals/SchemaModal/SchemaModal'; + +// eslint-disable-next-line +export const RowData = ({ data }: { data: any }) => { + const [isFullView, setIsFullView] = useState(false); + + const onClose = () => setIsFullView(false); + const onOpen = () => setIsFullView(true); + + // eslint-disable-next-line + const getDataElement = (rowValue: any) => { + if (typeof rowValue === 'object') { + return ( +

+ {JSON.stringify(rowValue)} +

+ ); + } + + return

{rowValue.toString()}

; + }; + + return ( + + {data ? getDataElement(data) :

--

} + {isFullView && } +
+ ); +}; diff --git a/openmetadata-ui/src/main/resources/ui/src/components/SampleDataTable/SampleDataTable.component.tsx b/openmetadata-ui/src/main/resources/ui/src/components/SampleDataTable/SampleDataTable.component.tsx index 05079e312da..43fd9a5e7fe 100644 --- a/openmetadata-ui/src/main/resources/ui/src/components/SampleDataTable/SampleDataTable.component.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/components/SampleDataTable/SampleDataTable.component.tsx @@ -28,15 +28,19 @@ import React, { import { TableData } from '../../generated/entity/data/table'; import { withLoader } from '../../hoc/withLoader'; import { isEven } from '../../utils/CommonUtils'; +import { RowData } from './RowData'; -export type SampleColumns = { name: string; dataType: string }; +export interface SampleColumns { + name: string; + dataType: string; +} -type Props = { +interface Props { sampleData?: { columns?: Array; rows?: TableData['rows']; }; -}; +} const SampleDataTable: FunctionComponent = ({ sampleData }: Props) => { const tableRef = useRef(null); @@ -133,7 +137,7 @@ const SampleDataTable: FunctionComponent = ({ sampleData }: Props) => { className="tableBody-cell" data-testid="cell" key={index}> - {data ? data.toString() : '--'} + ); })}