diff --git a/openmetadata-ui/src/main/resources/ui/src/components/DataQuality/AddDataQualityTest/components/TestCaseForm.test.tsx b/openmetadata-ui/src/main/resources/ui/src/components/DataQuality/AddDataQualityTest/components/TestCaseForm.test.tsx index 10370121299..885a94c4a8c 100644 --- a/openmetadata-ui/src/main/resources/ui/src/components/DataQuality/AddDataQualityTest/components/TestCaseForm.test.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/components/DataQuality/AddDataQualityTest/components/TestCaseForm.test.tsx @@ -21,6 +21,7 @@ import React, { forwardRef } from 'react'; import { act } from 'react-dom/test-utils'; import { ProfilerDashboardType } from '../../../../enums/table.enum'; import { MOCK_TABLE } from '../../../../mocks/TableData.mock'; +import { getListTestDefinitions } from '../../../../rest/testAPI'; import TestCaseForm from './TestCaseForm'; const mockProps = { @@ -74,9 +75,10 @@ const mockTestDefinition = { }, ], }; +const mockUseHistory = { push: jest.fn() }; jest.mock('react-router-dom', () => ({ - useHistory: jest.fn(), + useHistory: jest.fn().mockImplementation(() => mockUseHistory), useParams: jest.fn().mockImplementation(() => mockParams), })); jest.mock('../../../../utils/DataQuality/DataQualityUtils', () => { @@ -186,6 +188,19 @@ describe('TestCaseForm', () => { }); }); + it("should call getListTestDefinitions when test type is 'Table'", async () => { + await act(async () => { + render(); + }); + + expect(getListTestDefinitions).toHaveBeenCalledWith({ + entityType: 'TABLE', + limit: 50, + supportedDataType: undefined, + testPlatform: 'OpenMetadata', + }); + }); + // column test case it("should show column section when test type is 'Column'", async () => { mockParams.dashboardType = ProfilerDashboardType.COLUMN; @@ -194,6 +209,44 @@ describe('TestCaseForm', () => { }); expect(await screen.findByTestId('column')).toBeInTheDocument(); + expect(getListTestDefinitions).not.toHaveBeenCalled(); + }); + + it('should call getListTestDefinitions when column value change', async () => { + mockParams.dashboardType = ProfilerDashboardType.COLUMN; + + await act(async () => { + render(); + }); + + const column = await findByRole( + await screen.findByTestId('column'), + 'combobox' + ); + await act(async () => { + userEvent.click(column); + }); + + expect(column).toBeInTheDocument(); + + await waitForElement(() => screen.findByText('last_name')); + + await act(async () => { + userEvent.click(await screen.findByText('last_name')); + }); + + expect(mockUseHistory.push).toHaveBeenCalledWith({ + search: + 'activeColumnFqn=sample_data.ecommerce_db.shopify.dim_address.last_name', + }); + expect(getListTestDefinitions).toHaveBeenCalledWith({ + entityType: 'COLUMN', + limit: 50, + supportedDataType: 'VARCHAR', + testPlatform: 'OpenMetadata', + }); + + mockParams.dashboardType = ProfilerDashboardType.TABLE; }); it('should show compute row count field, if supportsRowLevelPassedFailed is true in test definition', async () => { diff --git a/openmetadata-ui/src/main/resources/ui/src/components/DataQuality/AddDataQualityTest/components/TestCaseForm.tsx b/openmetadata-ui/src/main/resources/ui/src/components/DataQuality/AddDataQualityTest/components/TestCaseForm.tsx index 93b88aa3197..edcaac79d77 100644 --- a/openmetadata-ui/src/main/resources/ui/src/components/DataQuality/AddDataQualityTest/components/TestCaseForm.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/components/DataQuality/AddDataQualityTest/components/TestCaseForm.tsx @@ -87,6 +87,7 @@ const TestCaseForm: React.FC = ({ initialValue?.testDefinition ); const [testCases, setTestCases] = useState([]); + const [currentColumnType, setCurrentColumnType] = useState(); const columnName = Form.useWatch('column', form); @@ -105,17 +106,13 @@ const TestCaseForm: React.FC = ({ }, ]; - const fetchAllTestDefinitions = async () => { + const fetchAllTestDefinitions = async (columnType?: string) => { try { const { data } = await getListTestDefinitions({ limit: PAGE_SIZE_LARGE, entityType: isColumnFqn ? EntityType.Column : EntityType.Table, testPlatform: TestPlatform.OpenMetadata, - supportedDataType: isColumnFqn - ? table.columns.find( - (column) => column.fullyQualifiedName === columnName - )?.dataType - : undefined, + supportedDataType: columnType, }); const updatedData = data.filter((definition) => { if (definition.fullyQualifiedName === TABLE_DIFF) { @@ -227,6 +224,9 @@ const TestCaseForm: React.FC = ({ const handleValueChange: FormProps['onValuesChange'] = (value) => { if (value.testTypeId) { setSelectedTestType(value.testTypeId); + } else if (value.column) { + form.setFieldsValue({ testTypeId: undefined }); + setSelectedTestType(undefined); } }; @@ -237,10 +237,14 @@ const TestCaseForm: React.FC = ({ }; useEffect(() => { - fetchAllTestDefinitions(); const selectedColumn = table.columns.find( (column) => column.name === columnName ); + + if (selectedColumn?.dataType !== currentColumnType) { + fetchAllTestDefinitions(selectedColumn?.dataType); + setCurrentColumnType(selectedColumn?.dataType); + } if (selectedColumn) { history.push({ search: Qs.stringify({ @@ -254,6 +258,9 @@ const TestCaseForm: React.FC = ({ if (isEmpty(testCases)) { fetchAllTestCases(); } + if (!isColumnFqn) { + fetchAllTestDefinitions(); + } form.setFieldsValue({ testName: replaceAllSpacialCharWith_(initialValue?.name ?? ''), testTypeId: initialValue?.testDefinition,