|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { AxisId, DefaultizedZoomOptions, ZoomData } from '@mui/x-charts/internals'; |
| 3 | +import { initializeZoomData } from './useChartProZoom'; |
| 4 | + |
| 5 | +describe('initializeZoomData', () => { |
| 6 | + const defaultZoomOptions: DefaultizedZoomOptions = { |
| 7 | + axisId: 'x', |
| 8 | + axisDirection: 'x', |
| 9 | + minStart: 0, |
| 10 | + maxEnd: 100, |
| 11 | + step: 5, |
| 12 | + minSpan: 10, |
| 13 | + maxSpan: 100, |
| 14 | + panning: true, |
| 15 | + filterMode: 'keep', |
| 16 | + }; |
| 17 | + |
| 18 | + it('should initialize zoom data for all axes based on provided options when no zoom data is given', () => { |
| 19 | + const options = { |
| 20 | + x: { ...defaultZoomOptions, axisId: 'x', minStart: 0, maxEnd: 100 }, |
| 21 | + y: { ...defaultZoomOptions, axisDirection: 'y', axisId: 'y', minStart: 10, maxEnd: 50 }, |
| 22 | + } satisfies Record<AxisId, DefaultizedZoomOptions>; |
| 23 | + |
| 24 | + const result = initializeZoomData(options); |
| 25 | + |
| 26 | + expect(result).to.deep.equal([ |
| 27 | + { axisId: 'x', start: 0, end: 100 }, |
| 28 | + { axisId: 'y', start: 10, end: 50 }, |
| 29 | + ]); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should override options with provided zoom data when matching axisId exists', () => { |
| 33 | + const options = { |
| 34 | + x: { ...defaultZoomOptions, axisId: 'x', minStart: 0, maxEnd: 100 }, |
| 35 | + y: { ...defaultZoomOptions, axisDirection: 'y', axisId: 'y', minStart: 10, maxEnd: 50 }, |
| 36 | + } satisfies Record<AxisId, DefaultizedZoomOptions>; |
| 37 | + |
| 38 | + const zoomData: readonly ZoomData[] = [{ axisId: 'x', start: 20, end: 60 }]; |
| 39 | + const result = initializeZoomData(options, zoomData); |
| 40 | + |
| 41 | + expect(result).to.deep.equal([ |
| 42 | + { axisId: 'x', start: 20, end: 60 }, |
| 43 | + { axisId: 'y', start: 10, end: 50 }, |
| 44 | + ]); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should ignore zoom data if its axisId is not present in options', () => { |
| 48 | + const options = { |
| 49 | + x: { ...defaultZoomOptions, axisId: 'x', minStart: 0, maxEnd: 100 }, |
| 50 | + } satisfies Record<AxisId, DefaultizedZoomOptions>; |
| 51 | + |
| 52 | + const zoomData: readonly ZoomData[] = [{ axisId: 'y', start: 10, end: 50 }]; |
| 53 | + const result = initializeZoomData(options, zoomData); |
| 54 | + |
| 55 | + expect(result).to.deep.equal([{ axisId: 'x', start: 0, end: 100 }]); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should handle an empty options object without errors', () => { |
| 59 | + const result = initializeZoomData({}); |
| 60 | + expect(result).to.deep.equal([]); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should handle undefined zoomData gracefully', () => { |
| 64 | + const options = { |
| 65 | + x: { ...defaultZoomOptions, axisId: 'x', minStart: 0, maxEnd: 100 }, |
| 66 | + } satisfies Record<AxisId, DefaultizedZoomOptions>; |
| 67 | + |
| 68 | + const result = initializeZoomData(options, undefined); |
| 69 | + |
| 70 | + expect(result).to.deep.equal([{ axisId: 'x', start: 0, end: 100 }]); |
| 71 | + }); |
| 72 | +}); |
0 commit comments