-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add different signature for each series type hook
- Loading branch information
Showing
19 changed files
with
471 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { renderHook } from '@mui/internal-test-utils'; | ||
import { expect } from 'chai'; | ||
import { stub, restore } from 'sinon'; | ||
import { useBarSeries } from './useBarSeries'; | ||
import * as series from './useSeries'; | ||
import { SeriesId } from '../models/seriesType/common'; | ||
import { FormattedSeries } from '../context/SeriesProvider'; | ||
|
||
describe('useBarSeries', () => { | ||
const defaultProps = { | ||
valueFormatter: (v: any) => v, | ||
color: 'red', | ||
layout: 'vertical', | ||
type: 'bar', | ||
stackedData: [] as [number, number][], | ||
} as const; | ||
|
||
const mockSeries: FormattedSeries = { | ||
bar: { | ||
series: { | ||
'1': { ...defaultProps, id: '1', data: [1, 2, 3] }, | ||
'2': { ...defaultProps, id: '2', data: [4, 5, 6] }, | ||
}, | ||
seriesOrder: ['1', '2'], | ||
stackingGroups: [], | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
stub(series, 'useSeries').returns(mockSeries); | ||
}); | ||
|
||
afterEach(() => { | ||
restore(); | ||
}); | ||
|
||
it('should return all bar series when no seriesIds are provided', () => { | ||
const { result } = renderHook(() => useBarSeries()); | ||
expect(result.current).to.deep.equal(mockSeries.bar); | ||
}); | ||
|
||
it('should return the specific bar series when a single seriesId is provided', () => { | ||
const { result } = renderHook(() => useBarSeries('1' as SeriesId)); | ||
expect(result.current).to.deep.equal(mockSeries!.bar!.series['1']); | ||
}); | ||
|
||
it('should return the specific bar series when multiple seriesIds are provided', () => { | ||
const { result } = renderHook(() => useBarSeries('1' as SeriesId, '2' as SeriesId)); | ||
expect(result.current).to.deep.equal([ | ||
mockSeries!.bar!.series['1'], | ||
mockSeries!.bar!.series['2'], | ||
]); | ||
}); | ||
|
||
it('should filter out undefined series when invalid seriesIds are provided', () => { | ||
const { result } = renderHook(() => useBarSeries('1' as SeriesId, '3' as SeriesId)); | ||
expect(result.current).to.deep.equal([mockSeries!.bar!.series['1']]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { FormattedSeries } from '../context/SeriesProvider'; | ||
import { SeriesId } from '../models/seriesType/common'; | ||
import { ChartSeriesDefaultized } from '../models/seriesType/config'; | ||
import { useSeries } from './useSeries'; | ||
|
||
/** | ||
* Get access to the internal state of bar series. | ||
* The returned object contains: | ||
* - series: a mapping from ids to series attributes. | ||
* - seriesOrder: the array of series ids. | ||
* @returns {{ series: Record<SeriesId, DefaultizedBarSeriesType>; seriesOrder: SeriesId[]; } | undefined} barSeries | ||
*/ | ||
export function useBarSeries(): FormattedSeries['bar']; | ||
/** | ||
* Get access to the internal state of bar series. | ||
* | ||
* @param {SeriesId} seriesId The id of the series to get. | ||
* @returns {ChartSeriesDefaultized<'bar'> | undefined} barSeries | ||
*/ | ||
export function useBarSeries(seriesId: SeriesId): ChartSeriesDefaultized<'bar'>; | ||
/** | ||
* Get access to the internal state of bar series. | ||
* | ||
* @param {SeriesId[]} seriesIds The ids of the series to get. Order is preserved. | ||
* @returns {ChartSeriesDefaultized<'bar'>[] | undefined} barSeries | ||
*/ | ||
export function useBarSeries(...seriesIds: SeriesId[]): ChartSeriesDefaultized<'bar'>[]; | ||
export function useBarSeries(...seriesIds: SeriesId[]): any { | ||
const series = useSeries(); | ||
|
||
return React.useMemo( | ||
() => { | ||
if (seriesIds.length === 0) { | ||
return series.bar; | ||
} | ||
|
||
if (seriesIds.length === 1) { | ||
return series?.bar?.series[seriesIds[0]]; | ||
} | ||
|
||
return seriesIds.map((id) => series?.bar?.series[id]).filter(Boolean); | ||
}, | ||
// DANGER: Ensure that the dependencies array is correct. | ||
// eslint-disable-next-line react-compiler/react-compiler | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[series.bar, ...seriesIds], | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { renderHook } from '@mui/internal-test-utils'; | ||
import { expect } from 'chai'; | ||
import { stub, restore } from 'sinon'; | ||
import { useLineSeries } from './useLineSeries'; | ||
import * as series from './useSeries'; | ||
import { SeriesId } from '../models/seriesType/common'; | ||
import { FormattedSeries } from '../context/SeriesProvider'; | ||
|
||
describe('useLineSeries', () => { | ||
const defaultProps = { | ||
valueFormatter: (v: any) => v, | ||
color: 'red', | ||
layout: 'vertical', | ||
type: 'line', | ||
stackedData: [] as [number, number][], | ||
} as const; | ||
|
||
const mockSeries: FormattedSeries = { | ||
line: { | ||
series: { | ||
'1': { ...defaultProps, id: '1', data: [1, 2, 3] }, | ||
'2': { ...defaultProps, id: '2', data: [4, 5, 6] }, | ||
}, | ||
seriesOrder: ['1', '2'], | ||
stackingGroups: [], | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
stub(series, 'useSeries').returns(mockSeries); | ||
}); | ||
|
||
afterEach(() => { | ||
restore(); | ||
}); | ||
|
||
it('should return all line series when no seriesIds are provided', () => { | ||
const { result } = renderHook(() => useLineSeries()); | ||
expect(result.current).to.deep.equal(mockSeries.line); | ||
}); | ||
|
||
it('should return the specific line series when a single seriesId is provided', () => { | ||
const { result } = renderHook(() => useLineSeries('1' as SeriesId)); | ||
expect(result.current).to.deep.equal(mockSeries!.line!.series['1']); | ||
}); | ||
|
||
it('should return the specific line series when multiple seriesIds are provided', () => { | ||
const { result } = renderHook(() => useLineSeries('1' as SeriesId, '2' as SeriesId)); | ||
expect(result.current).to.deep.equal([ | ||
mockSeries!.line!.series['1'], | ||
mockSeries!.line!.series['2'], | ||
]); | ||
}); | ||
|
||
it('should filter out undefined series when invalid seriesIds are provided', () => { | ||
const { result } = renderHook(() => useLineSeries('1' as SeriesId, '3' as SeriesId)); | ||
expect(result.current).to.deep.equal([mockSeries!.line!.series['1']]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { FormattedSeries } from '../context/SeriesProvider'; | ||
import { SeriesId } from '../models/seriesType/common'; | ||
import { ChartSeriesDefaultized } from '../models/seriesType/config'; | ||
import { useSeries } from './useSeries'; | ||
|
||
/** | ||
* Get access to the internal state of line series. | ||
* The returned object contains: | ||
* - series: a mapping from ids to series attributes. | ||
* - seriesOrder: the array of series ids. | ||
* @returns {{ series: Record<SeriesId, DefaultizedLineSeriesType>; seriesOrder: SeriesId[]; } | undefined} lineSeries | ||
*/ | ||
export function useLineSeries(): FormattedSeries['line']; | ||
/** | ||
* Get access to the internal state of line series. | ||
* | ||
* @param {SeriesId} seriesId The id of the series to get. | ||
* @returns {ChartSeriesDefaultized<'line'> | undefined} lineSeries | ||
*/ | ||
export function useLineSeries(seriesId: SeriesId): ChartSeriesDefaultized<'line'>; | ||
/** | ||
* Get access to the internal state of line series. | ||
* | ||
* @param {SeriesId[]} seriesIds The ids of the series to get. Order is preserved. | ||
* @returns {ChartSeriesDefaultized<'line'>[] | undefined} lineSeries | ||
*/ | ||
export function useLineSeries(...seriesIds: SeriesId[]): ChartSeriesDefaultized<'line'>[]; | ||
export function useLineSeries(...seriesIds: SeriesId[]): any { | ||
const series = useSeries(); | ||
|
||
return React.useMemo( | ||
() => { | ||
if (seriesIds.length === 0) { | ||
return series.line; | ||
} | ||
|
||
if (seriesIds.length === 1) { | ||
return series?.line?.series[seriesIds[0]]; | ||
} | ||
|
||
return seriesIds.map((id) => series?.line?.series[id]).filter(Boolean); | ||
}, | ||
// DANGER: Ensure that the dependencies array is correct. | ||
// eslint-disable-next-line react-compiler/react-compiler | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[series.line, ...seriesIds], | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { renderHook } from '@mui/internal-test-utils'; | ||
import { expect } from 'chai'; | ||
import { stub, restore } from 'sinon'; | ||
import { usePieSeries } from './usePieSeries'; | ||
import * as series from './useSeries'; | ||
import { SeriesId } from '../models/seriesType/common'; | ||
import { FormattedSeries } from '../context/SeriesProvider'; | ||
|
||
describe('usePieSeries', () => { | ||
const defaultProps = { | ||
valueFormatter: (v: any) => v, | ||
color: 'red', | ||
layout: 'vertical', | ||
type: 'pie', | ||
stackedData: [] as [number, number][], | ||
} as const; | ||
|
||
const dataExample = { | ||
id: 1, | ||
value: 10, | ||
startAngle: 0, | ||
endAngle: 1, | ||
color: 'red', | ||
formattedValue: '10', | ||
index: 0, | ||
padAngle: 0, | ||
} as const; | ||
|
||
const mockSeries: FormattedSeries = { | ||
pie: { | ||
series: { | ||
'1': { ...defaultProps, id: '1', data: [dataExample] }, | ||
'2': { ...defaultProps, id: '2', data: [dataExample] }, | ||
}, | ||
seriesOrder: ['1', '2'], | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
stub(series, 'useSeries').returns(mockSeries); | ||
}); | ||
|
||
afterEach(() => { | ||
restore(); | ||
}); | ||
|
||
it('should return all pie series when no seriesIds are provided', () => { | ||
const { result } = renderHook(() => usePieSeries()); | ||
expect(result.current).to.deep.equal(mockSeries.pie); | ||
}); | ||
|
||
it('should return the specific pie series when a single seriesId is provided', () => { | ||
const { result } = renderHook(() => usePieSeries('1' as SeriesId)); | ||
expect(result.current).to.deep.equal(mockSeries!.pie!.series['1']); | ||
}); | ||
|
||
it('should return the specific pie series when multiple seriesIds are provided', () => { | ||
const { result } = renderHook(() => usePieSeries('1' as SeriesId, '2' as SeriesId)); | ||
expect(result.current).to.deep.equal([ | ||
mockSeries!.pie!.series['1'], | ||
mockSeries!.pie!.series['2'], | ||
]); | ||
}); | ||
|
||
it('should filter out undefined series when invalid seriesIds are provided', () => { | ||
const { result } = renderHook(() => usePieSeries('1' as SeriesId, '3' as SeriesId)); | ||
expect(result.current).to.deep.equal([mockSeries!.pie!.series['1']]); | ||
}); | ||
}); |
Oops, something went wrong.