|
| 1 | +/* |
| 2 | + * Copyright 2026 Collate. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +import { fireEvent, render, screen, waitFor } from '@testing-library/react'; |
| 15 | +import { TABLE_CONSTRAINTS_TYPE_OPTIONS } from '../../../../constants/Table.constants'; |
| 16 | +import { |
| 17 | + ConstraintType, |
| 18 | + DataType, |
| 19 | + Table, |
| 20 | +} from '../../../../generated/entity/data/table'; |
| 21 | +import { searchQuery } from '../../../../rest/searchAPI'; |
| 22 | +import TableConstraintsModal from './TableConstraintsModal.component'; |
| 23 | + |
| 24 | +const mockOnSave = jest.fn().mockResolvedValue(undefined); |
| 25 | +const mockOnClose = jest.fn(); |
| 26 | +const mockSearchQuery = searchQuery as jest.MockedFunction<typeof searchQuery>; |
| 27 | + |
| 28 | +const mockTableDetails = { |
| 29 | + service: { |
| 30 | + name: 'repro_cluster_service', |
| 31 | + }, |
| 32 | + columns: [ |
| 33 | + { |
| 34 | + name: 'Entity_ID', |
| 35 | + dataType: DataType.Int, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: 'Entity_Type', |
| 39 | + dataType: DataType.Int, |
| 40 | + }, |
| 41 | + ], |
| 42 | +} as Table; |
| 43 | + |
| 44 | +const mockConstraint: Table['tableConstraints'] = [ |
| 45 | + { |
| 46 | + constraintType: ConstraintType.ClusterKey, |
| 47 | + columns: ['Entity_ID', 'Entity_Type'], |
| 48 | + }, |
| 49 | +]; |
| 50 | + |
| 51 | +jest.mock('../../../../utils/EntityUtils', () => ({ |
| 52 | + getBreadcrumbsFromFqn: jest |
| 53 | + .fn() |
| 54 | + .mockImplementation(() => [ |
| 55 | + { name: 'service' }, |
| 56 | + { name: 'database' }, |
| 57 | + { name: 'schema' }, |
| 58 | + { name: 'table' }, |
| 59 | + { name: 'column' }, |
| 60 | + ]), |
| 61 | +})); |
| 62 | + |
| 63 | +jest.mock('../../../../utils/ServiceUtils', () => ({ |
| 64 | + getServiceNameQueryFilter: jest.fn().mockImplementation(() => ''), |
| 65 | +})); |
| 66 | + |
| 67 | +jest.mock('../../../../utils/StringsUtils', () => ({ |
| 68 | + escapeESReservedCharacters: jest.fn().mockImplementation((value) => value), |
| 69 | + getEncodedFqn: jest.fn().mockImplementation((value) => value), |
| 70 | +})); |
| 71 | + |
| 72 | +jest.mock('../../../../utils/TableUtils', () => ({ |
| 73 | + createTableConstraintObject: jest |
| 74 | + .fn() |
| 75 | + .mockImplementation((constraints, type) => |
| 76 | + Array.isArray(constraints) && constraints.length > 0 |
| 77 | + ? [{ columns: constraints, constraintType: type }] |
| 78 | + : [] |
| 79 | + ), |
| 80 | + getColumnOptionsFromTableColumn: jest.fn().mockImplementation((columns) => |
| 81 | + (columns ?? []).map((column: { name: string }) => ({ |
| 82 | + label: column.name, |
| 83 | + value: column.name, |
| 84 | + })) |
| 85 | + ), |
| 86 | +})); |
| 87 | + |
| 88 | +jest.mock('../../../../rest/searchAPI', () => ({ |
| 89 | + searchQuery: jest.fn().mockResolvedValue({ hits: { hits: [] } }), |
| 90 | +})); |
| 91 | + |
| 92 | +describe('TableConstraintsModal', () => { |
| 93 | + beforeEach(() => { |
| 94 | + mockOnSave.mockClear(); |
| 95 | + mockOnClose.mockClear(); |
| 96 | + mockSearchQuery.mockClear(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('includes cluster key in available constraint types', () => { |
| 100 | + expect( |
| 101 | + TABLE_CONSTRAINTS_TYPE_OPTIONS.some( |
| 102 | + ({ value }) => value === ConstraintType.ClusterKey |
| 103 | + ) |
| 104 | + ).toBe(true); |
| 105 | + }); |
| 106 | + |
| 107 | + it('preserves cluster key constraints on save', async () => { |
| 108 | + render( |
| 109 | + <TableConstraintsModal |
| 110 | + constraint={mockConstraint} |
| 111 | + tableDetails={mockTableDetails} |
| 112 | + onClose={mockOnClose} |
| 113 | + onSave={mockOnSave} |
| 114 | + /> |
| 115 | + ); |
| 116 | + |
| 117 | + fireEvent.click(screen.getByTestId('save-btn')); |
| 118 | + |
| 119 | + await waitFor(() => { |
| 120 | + expect(mockOnSave).toHaveBeenCalledWith( |
| 121 | + expect.arrayContaining([ |
| 122 | + expect.objectContaining({ |
| 123 | + constraintType: ConstraintType.ClusterKey, |
| 124 | + columns: ['Entity_ID', 'Entity_Type'], |
| 125 | + }), |
| 126 | + ]) |
| 127 | + ); |
| 128 | + }); |
| 129 | + |
| 130 | + expect(mockSearchQuery).not.toHaveBeenCalled(); |
| 131 | + }); |
| 132 | +}); |
0 commit comments