|
| 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 { NodeData } from '@antv/g6'; |
| 15 | +import { render, screen } from '@testing-library/react'; |
| 16 | +import React from 'react'; |
| 17 | +import CustomNode from './CustomNode'; |
| 18 | + |
| 19 | +jest.mock('@antv/g6', () => ({})); |
| 20 | + |
| 21 | +jest.mock('../../../utils/TableUtils', () => ({ |
| 22 | + getEntityIcon: jest.fn(() => <svg data-testid="entity-icon" />), |
| 23 | +})); |
| 24 | + |
| 25 | +jest.mock('@openmetadata/ui-core-components', () => { |
| 26 | + const R = require('react'); |
| 27 | + |
| 28 | + return { |
| 29 | + Box: ({ |
| 30 | + children, |
| 31 | + ...p |
| 32 | + }: React.PropsWithChildren<Record<string, unknown>>) => |
| 33 | + R.createElement('div', p, children), |
| 34 | + Typography: ({ |
| 35 | + children, |
| 36 | + 'data-testid': testId, |
| 37 | + style, |
| 38 | + ...p |
| 39 | + }: React.PropsWithChildren<{ |
| 40 | + 'data-testid'?: string; |
| 41 | + style?: React.CSSProperties; |
| 42 | + }>) => |
| 43 | + R.createElement('span', { 'data-testid': testId, style, ...p }, children), |
| 44 | + }; |
| 45 | +}); |
| 46 | + |
| 47 | +import { getNodeRenderKey } from '../../../utils/KnowledgeGraph.utils'; |
| 48 | +import { getEntityIcon } from '../../../utils/TableUtils'; |
| 49 | + |
| 50 | +function makeNodeData( |
| 51 | + overrides: Record<string, unknown> = {}, |
| 52 | + id = 'node-1' |
| 53 | +): NodeData { |
| 54 | + return { |
| 55 | + id, |
| 56 | + data: { |
| 57 | + label: 'TestNode', |
| 58 | + type: 'table', |
| 59 | + ...overrides, |
| 60 | + }, |
| 61 | + } as NodeData; |
| 62 | +} |
| 63 | + |
| 64 | +function renderCustomNode(nodeData: NodeData) { |
| 65 | + return render( |
| 66 | + <CustomNode |
| 67 | + nodeData={nodeData} |
| 68 | + nodeRenderKey={getNodeRenderKey(nodeData)} |
| 69 | + /> |
| 70 | + ); |
| 71 | +} |
| 72 | + |
| 73 | +describe('CustomNode', () => { |
| 74 | + beforeEach(() => { |
| 75 | + jest.clearAllMocks(); |
| 76 | + (getEntityIcon as jest.Mock).mockReturnValue( |
| 77 | + <svg data-testid="entity-icon" /> |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('Basic rendering', () => { |
| 82 | + it('renders without crashing with minimal props', () => { |
| 83 | + renderCustomNode(makeNodeData()); |
| 84 | + |
| 85 | + expect(screen.getByTestId('node-TestNode')).toBeInTheDocument(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('renders label from nodeData.data.label', () => { |
| 89 | + renderCustomNode(makeNodeData({ label: 'MyTable' })); |
| 90 | + |
| 91 | + expect(screen.getByTestId('label')).toHaveTextContent('MyTable'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('renders type text in type-tag', () => { |
| 95 | + renderCustomNode(makeNodeData({ type: 'pipeline' })); |
| 96 | + |
| 97 | + expect(screen.getByTestId('type-tag')).toHaveTextContent('pipeline'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('sets data-node-id attribute to nodeData.id', () => { |
| 101 | + renderCustomNode(makeNodeData({}, 'abc-123')); |
| 102 | + |
| 103 | + expect(screen.getByTestId('node-TestNode')).toHaveAttribute( |
| 104 | + 'data-node-id', |
| 105 | + 'abc-123' |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it('sets data-testid to "node-{label}" on root div', () => { |
| 110 | + renderCustomNode(makeNodeData({ label: 'SomeLabel' })); |
| 111 | + |
| 112 | + expect(screen.getByTestId('node-SomeLabel')).toBeInTheDocument(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('exposes data-testid="label" on the label element', () => { |
| 116 | + renderCustomNode(makeNodeData()); |
| 117 | + |
| 118 | + expect(screen.getByTestId('label')).toBeInTheDocument(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('exposes data-testid="type-tag" on the type element', () => { |
| 122 | + renderCustomNode(makeNodeData()); |
| 123 | + |
| 124 | + expect(screen.getByTestId('type-tag')).toBeInTheDocument(); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + describe('Highlighted state', () => { |
| 129 | + it('does NOT add highlighted class when highlighted is undefined', () => { |
| 130 | + renderCustomNode(makeNodeData()); |
| 131 | + |
| 132 | + expect(screen.getByTestId('node-TestNode')).not.toHaveClass( |
| 133 | + 'highlighted' |
| 134 | + ); |
| 135 | + }); |
| 136 | + |
| 137 | + it('does NOT add highlighted class when highlighted is false', () => { |
| 138 | + renderCustomNode(makeNodeData({ highlighted: false })); |
| 139 | + |
| 140 | + expect(screen.getByTestId('node-TestNode')).not.toHaveClass( |
| 141 | + 'highlighted' |
| 142 | + ); |
| 143 | + }); |
| 144 | + |
| 145 | + it('DOES add highlighted class when highlighted is true', () => { |
| 146 | + renderCustomNode(makeNodeData({ highlighted: true })); |
| 147 | + |
| 148 | + expect(screen.getByTestId('node-TestNode')).toHaveClass('highlighted'); |
| 149 | + }); |
| 150 | + |
| 151 | + it('updates highlighted class when same node object is mutated and rerendered', () => { |
| 152 | + const nodeData = makeNodeData({ highlighted: false }); |
| 153 | + const { rerender } = renderCustomNode(nodeData); |
| 154 | + |
| 155 | + expect(screen.getByTestId('node-TestNode')).not.toHaveClass( |
| 156 | + 'highlighted' |
| 157 | + ); |
| 158 | + |
| 159 | + (nodeData.data as { highlighted?: boolean }).highlighted = true; |
| 160 | + rerender( |
| 161 | + <CustomNode |
| 162 | + nodeData={nodeData} |
| 163 | + nodeRenderKey={getNodeRenderKey(nodeData)} |
| 164 | + /> |
| 165 | + ); |
| 166 | + |
| 167 | + expect(screen.getByTestId('node-TestNode')).toHaveClass('highlighted'); |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + describe('Custom color styles', () => { |
| 172 | + it('applies colorMain and colorLight as inline style on type-tag when both provided', () => { |
| 173 | + renderCustomNode( |
| 174 | + makeNodeData({ |
| 175 | + colorMain: '#1677ff', |
| 176 | + colorLight: '#e6f4ff', |
| 177 | + }) |
| 178 | + ); |
| 179 | + |
| 180 | + const tag = screen.getByTestId('type-tag'); |
| 181 | + |
| 182 | + expect(tag).toHaveStyle({ color: '#1677ff', backgroundColor: '#e6f4ff' }); |
| 183 | + }); |
| 184 | + |
| 185 | + it('sets border:none on type-tag when both colors provided', () => { |
| 186 | + renderCustomNode( |
| 187 | + makeNodeData({ |
| 188 | + colorMain: '#1677ff', |
| 189 | + colorLight: '#e6f4ff', |
| 190 | + }) |
| 191 | + ); |
| 192 | + |
| 193 | + expect(screen.getByTestId('type-tag')).toHaveStyle({ border: 'none' }); |
| 194 | + }); |
| 195 | + |
| 196 | + it('does NOT apply inline style when only colorMain is provided', () => { |
| 197 | + renderCustomNode(makeNodeData({ colorMain: '#1677ff' })); |
| 198 | + |
| 199 | + expect(screen.getByTestId('type-tag')).not.toHaveStyle({ |
| 200 | + color: '#1677ff', |
| 201 | + }); |
| 202 | + }); |
| 203 | + |
| 204 | + it('does NOT apply inline style when only colorLight is provided', () => { |
| 205 | + renderCustomNode(makeNodeData({ colorLight: '#e6f4ff' })); |
| 206 | + |
| 207 | + expect(screen.getByTestId('type-tag')).not.toHaveStyle({ |
| 208 | + backgroundColor: '#e6f4ff', |
| 209 | + }); |
| 210 | + }); |
| 211 | + |
| 212 | + it('does NOT apply inline style when neither color is provided', () => { |
| 213 | + renderCustomNode(makeNodeData()); |
| 214 | + const tag = screen.getByTestId('type-tag'); |
| 215 | + |
| 216 | + expect(tag.getAttribute('style')).toBeFalsy(); |
| 217 | + }); |
| 218 | + }); |
| 219 | + |
| 220 | + describe('Icon rendering', () => { |
| 221 | + it('calls getEntityIcon with the node type string', () => { |
| 222 | + renderCustomNode(makeNodeData({ type: 'dashboard' })); |
| 223 | + |
| 224 | + expect(getEntityIcon).toHaveBeenCalledWith( |
| 225 | + 'dashboard', |
| 226 | + '', |
| 227 | + expect.objectContaining({ width: 12, height: 12 }) |
| 228 | + ); |
| 229 | + }); |
| 230 | + |
| 231 | + it('renders the icon returned by getEntityIcon', () => { |
| 232 | + renderCustomNode(makeNodeData()); |
| 233 | + |
| 234 | + expect(screen.getByTestId('entity-icon')).toBeInTheDocument(); |
| 235 | + }); |
| 236 | + }); |
| 237 | +}); |
0 commit comments