|
| 1 | +import { useChartLegendRegistration } from './useChartLegendRegistration'; |
| 2 | +import { renderHook } from '@testing-library/react-hooks'; |
| 3 | +import { Serie } from '@scality/core-ui/dist/components/linetimeseriechart/linetimeseriechart.component'; |
| 4 | + |
| 5 | +// Mock the useChartLegend hook |
| 6 | +const mockRegister = jest.fn(); |
| 7 | +jest.mock( |
| 8 | + '@scality/core-ui/dist/components/chartlegend/ChartLegendWrapper', |
| 9 | + () => ({ |
| 10 | + useChartLegend: () => ({ |
| 11 | + register: mockRegister, |
| 12 | + }), |
| 13 | + }), |
| 14 | +); |
| 15 | + |
| 16 | +describe('useChartLegendRegistration', () => { |
| 17 | + beforeEach(() => { |
| 18 | + mockRegister.mockClear(); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('Non-symmetrical series', () => { |
| 22 | + it('should register chart with series names when series is provided', () => { |
| 23 | + const mockSeries: Serie[] = [ |
| 24 | + { resource: 'cpu-usage', data: [], getTooltipLabel: () => 'CPU Usage' }, |
| 25 | + { |
| 26 | + resource: 'memory-usage', |
| 27 | + data: [], |
| 28 | + getTooltipLabel: () => 'Memory Usage', |
| 29 | + }, |
| 30 | + ]; |
| 31 | + |
| 32 | + renderHook(() => |
| 33 | + useChartLegendRegistration({ |
| 34 | + chartId: 'test-chart', |
| 35 | + series: mockSeries, |
| 36 | + isSymmetrical: false, |
| 37 | + }), |
| 38 | + ); |
| 39 | + |
| 40 | + expect(mockRegister).toHaveBeenCalledWith('test-chart', [ |
| 41 | + 'cpu-usage', |
| 42 | + 'memory-usage', |
| 43 | + ]); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should register chart with empty array when series is empty', () => { |
| 47 | + renderHook(() => |
| 48 | + useChartLegendRegistration({ |
| 49 | + chartId: 'test-chart', |
| 50 | + series: [], |
| 51 | + isSymmetrical: false, |
| 52 | + }), |
| 53 | + ); |
| 54 | + |
| 55 | + expect(mockRegister).toHaveBeenCalledWith('test-chart', []); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should not register when series is null', () => { |
| 59 | + renderHook(() => |
| 60 | + useChartLegendRegistration({ |
| 61 | + chartId: 'test-chart', |
| 62 | + series: null, |
| 63 | + isSymmetrical: false, |
| 64 | + }), |
| 65 | + ); |
| 66 | + |
| 67 | + expect(mockRegister).not.toHaveBeenCalled(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should include additional names when provided', () => { |
| 71 | + const mockSeries: Serie[] = [ |
| 72 | + { resource: 'cpu-usage', data: [], getTooltipLabel: () => 'CPU Usage' }, |
| 73 | + ]; |
| 74 | + |
| 75 | + renderHook(() => |
| 76 | + useChartLegendRegistration({ |
| 77 | + chartId: 'test-chart', |
| 78 | + series: mockSeries, |
| 79 | + isSymmetrical: false, |
| 80 | + additionalNames: ['custom-metric', 'another-metric'], |
| 81 | + }), |
| 82 | + ); |
| 83 | + |
| 84 | + expect(mockRegister).toHaveBeenCalledWith('test-chart', [ |
| 85 | + 'cpu-usage', |
| 86 | + 'custom-metric', |
| 87 | + 'another-metric', |
| 88 | + ]); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('Symmetrical series', () => { |
| 93 | + it('should register chart with above and below series names', () => { |
| 94 | + const mockSymmetricalSeries = { |
| 95 | + above: [ |
| 96 | + { |
| 97 | + resource: 'network-in', |
| 98 | + data: [], |
| 99 | + getTooltipLabel: () => 'Network In', |
| 100 | + }, |
| 101 | + { |
| 102 | + resource: 'disk-read', |
| 103 | + data: [], |
| 104 | + getTooltipLabel: () => 'Disk Read', |
| 105 | + }, |
| 106 | + ], |
| 107 | + below: [ |
| 108 | + { |
| 109 | + resource: 'network-out', |
| 110 | + data: [], |
| 111 | + getTooltipLabel: () => 'Network Out', |
| 112 | + }, |
| 113 | + { |
| 114 | + resource: 'disk-write', |
| 115 | + data: [], |
| 116 | + getTooltipLabel: () => 'Disk Write', |
| 117 | + }, |
| 118 | + ], |
| 119 | + }; |
| 120 | + |
| 121 | + renderHook(() => |
| 122 | + useChartLegendRegistration({ |
| 123 | + chartId: 'symmetrical-chart', |
| 124 | + series: mockSymmetricalSeries, |
| 125 | + isSymmetrical: true, |
| 126 | + }), |
| 127 | + ); |
| 128 | + |
| 129 | + expect(mockRegister).toHaveBeenCalledWith('symmetrical-chart', [ |
| 130 | + 'network-in', |
| 131 | + 'disk-read', |
| 132 | + 'network-out', |
| 133 | + 'disk-write', |
| 134 | + ]); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should register chart with only above series when below is empty', () => { |
| 138 | + const mockSymmetricalSeries = { |
| 139 | + above: [ |
| 140 | + { |
| 141 | + resource: 'network-in', |
| 142 | + data: [], |
| 143 | + getTooltipLabel: () => 'Network In', |
| 144 | + }, |
| 145 | + ], |
| 146 | + below: [], |
| 147 | + }; |
| 148 | + |
| 149 | + renderHook(() => |
| 150 | + useChartLegendRegistration({ |
| 151 | + chartId: 'symmetrical-chart', |
| 152 | + series: mockSymmetricalSeries, |
| 153 | + isSymmetrical: true, |
| 154 | + }), |
| 155 | + ); |
| 156 | + |
| 157 | + expect(mockRegister).toHaveBeenCalledWith('symmetrical-chart', [ |
| 158 | + 'network-in', |
| 159 | + ]); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should register chart with only below series when above is empty', () => { |
| 163 | + const mockSymmetricalSeries = { |
| 164 | + above: [], |
| 165 | + below: [ |
| 166 | + { |
| 167 | + resource: 'network-out', |
| 168 | + data: [], |
| 169 | + getTooltipLabel: () => 'Network Out', |
| 170 | + }, |
| 171 | + ], |
| 172 | + }; |
| 173 | + |
| 174 | + renderHook(() => |
| 175 | + useChartLegendRegistration({ |
| 176 | + chartId: 'symmetrical-chart', |
| 177 | + series: mockSymmetricalSeries, |
| 178 | + isSymmetrical: true, |
| 179 | + }), |
| 180 | + ); |
| 181 | + |
| 182 | + expect(mockRegister).toHaveBeenCalledWith('symmetrical-chart', [ |
| 183 | + 'network-out', |
| 184 | + ]); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should not register when both above and below are empty', () => { |
| 188 | + const mockSymmetricalSeries = { |
| 189 | + above: [], |
| 190 | + below: [], |
| 191 | + }; |
| 192 | + |
| 193 | + renderHook(() => |
| 194 | + useChartLegendRegistration({ |
| 195 | + chartId: 'symmetrical-chart', |
| 196 | + series: mockSymmetricalSeries, |
| 197 | + isSymmetrical: true, |
| 198 | + }), |
| 199 | + ); |
| 200 | + |
| 201 | + expect(mockRegister).toHaveBeenCalledWith('symmetrical-chart', []); |
| 202 | + }); |
| 203 | + |
| 204 | + it('should not register when series is null', () => { |
| 205 | + renderHook(() => |
| 206 | + useChartLegendRegistration({ |
| 207 | + chartId: 'symmetrical-chart', |
| 208 | + series: null, |
| 209 | + isSymmetrical: true, |
| 210 | + }), |
| 211 | + ); |
| 212 | + |
| 213 | + expect(mockRegister).not.toHaveBeenCalled(); |
| 214 | + }); |
| 215 | + |
| 216 | + it('should include additional names with symmetrical series', () => { |
| 217 | + const mockSymmetricalSeries = { |
| 218 | + above: [ |
| 219 | + { |
| 220 | + resource: 'network-in', |
| 221 | + data: [], |
| 222 | + getTooltipLabel: () => 'Network In', |
| 223 | + }, |
| 224 | + ], |
| 225 | + below: [ |
| 226 | + { |
| 227 | + resource: 'network-out', |
| 228 | + data: [], |
| 229 | + getTooltipLabel: () => 'Network Out', |
| 230 | + }, |
| 231 | + ], |
| 232 | + }; |
| 233 | + |
| 234 | + renderHook(() => |
| 235 | + useChartLegendRegistration({ |
| 236 | + chartId: 'symmetrical-chart', |
| 237 | + series: mockSymmetricalSeries, |
| 238 | + isSymmetrical: true, |
| 239 | + additionalNames: ['total-bandwidth'], |
| 240 | + }), |
| 241 | + ); |
| 242 | + |
| 243 | + expect(mockRegister).toHaveBeenCalledWith('symmetrical-chart', [ |
| 244 | + 'network-in', |
| 245 | + 'network-out', |
| 246 | + 'total-bandwidth', |
| 247 | + ]); |
| 248 | + }); |
| 249 | + }); |
| 250 | + |
| 251 | + describe('Effect dependencies', () => { |
| 252 | + it('should re-register when chartId changes', () => { |
| 253 | + const mockSeries: Serie[] = [ |
| 254 | + { resource: 'cpu-usage', data: [], getTooltipLabel: () => 'CPU Usage' }, |
| 255 | + ]; |
| 256 | + |
| 257 | + const { rerender } = renderHook( |
| 258 | + ({ chartId }) => |
| 259 | + useChartLegendRegistration({ |
| 260 | + chartId, |
| 261 | + series: mockSeries, |
| 262 | + isSymmetrical: false, |
| 263 | + }), |
| 264 | + { initialProps: { chartId: 'chart-1' } }, |
| 265 | + ); |
| 266 | + |
| 267 | + expect(mockRegister).toHaveBeenCalledWith('chart-1', ['cpu-usage']); |
| 268 | + |
| 269 | + rerender({ chartId: 'chart-2' }); |
| 270 | + |
| 271 | + expect(mockRegister).toHaveBeenCalledWith('chart-2', ['cpu-usage']); |
| 272 | + expect(mockRegister).toHaveBeenCalledTimes(2); |
| 273 | + }); |
| 274 | + |
| 275 | + it('should re-register when series changes', () => { |
| 276 | + const initialSeries: Serie[] = [ |
| 277 | + { resource: 'cpu-usage', data: [], getTooltipLabel: () => 'CPU Usage' }, |
| 278 | + ]; |
| 279 | + const updatedSeries: Serie[] = [ |
| 280 | + { resource: 'cpu-usage', data: [], getTooltipLabel: () => 'CPU Usage' }, |
| 281 | + { |
| 282 | + resource: 'memory-usage', |
| 283 | + data: [], |
| 284 | + getTooltipLabel: () => 'Memory Usage', |
| 285 | + }, |
| 286 | + ]; |
| 287 | + |
| 288 | + const { rerender } = renderHook( |
| 289 | + ({ series }) => |
| 290 | + useChartLegendRegistration({ |
| 291 | + chartId: 'test-chart', |
| 292 | + series, |
| 293 | + isSymmetrical: false, |
| 294 | + }), |
| 295 | + { initialProps: { series: initialSeries } }, |
| 296 | + ); |
| 297 | + |
| 298 | + expect(mockRegister).toHaveBeenCalledWith('test-chart', ['cpu-usage']); |
| 299 | + |
| 300 | + rerender({ series: updatedSeries }); |
| 301 | + |
| 302 | + expect(mockRegister).toHaveBeenCalledWith('test-chart', [ |
| 303 | + 'cpu-usage', |
| 304 | + 'memory-usage', |
| 305 | + ]); |
| 306 | + expect(mockRegister).toHaveBeenCalledTimes(2); |
| 307 | + }); |
| 308 | + |
| 309 | + it('should re-register when additionalNames changes', () => { |
| 310 | + const mockSeries: Serie[] = [ |
| 311 | + { resource: 'cpu-usage', data: [], getTooltipLabel: () => 'CPU Usage' }, |
| 312 | + ]; |
| 313 | + |
| 314 | + const { rerender } = renderHook( |
| 315 | + ({ additionalNames }) => |
| 316 | + useChartLegendRegistration({ |
| 317 | + chartId: 'test-chart', |
| 318 | + series: mockSeries, |
| 319 | + isSymmetrical: false, |
| 320 | + additionalNames, |
| 321 | + }), |
| 322 | + { initialProps: { additionalNames: ['metric-1'] } }, |
| 323 | + ); |
| 324 | + |
| 325 | + expect(mockRegister).toHaveBeenCalledWith('test-chart', [ |
| 326 | + 'cpu-usage', |
| 327 | + 'metric-1', |
| 328 | + ]); |
| 329 | + |
| 330 | + rerender({ additionalNames: ['metric-1', 'metric-2'] }); |
| 331 | + |
| 332 | + expect(mockRegister).toHaveBeenCalledWith('test-chart', [ |
| 333 | + 'cpu-usage', |
| 334 | + 'metric-1', |
| 335 | + 'metric-2', |
| 336 | + ]); |
| 337 | + expect(mockRegister).toHaveBeenCalledTimes(2); |
| 338 | + }); |
| 339 | + }); |
| 340 | +}); |
0 commit comments