Skip to content

Commit

Permalink
test: add and improve convertSeries and getOutlierSeries tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhermePrandi committed Oct 30, 2024
1 parent e9e6356 commit 2cbe158
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 16 deletions.
118 changes: 102 additions & 16 deletions src/components/Chart/convertSeries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@ import { convertSeries } from './convertSeries'
import { ChartSeries, ReferenceAreaWithPercents } from './model'

describe('convert series to Recharts format', () => {
const stringDomain = ['cat 1', 'cat 2', 'cat 3', 'cat 4', 'cat 5']
const numericDomainPoints = [0, 10, 20, 30, 40]
const seriesData: ChartSeries<string>[] = [
{
name: 'Test',
data: [1, 2, 3, 4, 5],
},
{
name: 'Test2',
data: [5, 4, 3, 2, 1],
},
]

it('should convert categories correctly', () => {
const series: ChartSeries<string>[] = [
{
name: 'Test',
data: [1, 2, 3, 4, 5],
},
{
name: 'Test2',
data: [5, 4, 3, 2, 1],
},
]
const series = seriesData

const domain = ['cat 1', 'cat 2', 'cat 3', 'cat 4', 'cat 5']
const domain = stringDomain
const domainPoints = domain

const seriesConverted = convertSeries(series, domainPoints, undefined)

expect(seriesConverted).toMatchObject([
{ x: 'cat 1', Test: 1, Test2: 5 },
{ x: 'cat 2', Test: 2, Test2: 4 },
Expand All @@ -27,18 +32,47 @@ describe('convert series to Recharts format', () => {
])
})

it('should convert category reference areas correctly', () => {
it('should convert categories correctly handling outliers', () => {
const series: ChartSeries<string>[] = [
{
name: 'Test',
data: [1, 2, 3, 4, 5],
data: [10, 20, 30, 40, 50],
},
{
name: 'Test2',
data: [5, 4, 3, 2, 1],
data: [50, 40, 30, 20, 10],
},
]
const outlierSeries: ChartSeries<string>[] = [
{
name: 'outliersTest',
data: [0, 0, 0, 0, 1],
},
{
name: 'outliersTest2',
data: [1, 0, 0, 0, 0],
},
]

const domain = stringDomain
const domainPoints = domain

const seriesHasOutliers = (seriesIndex: number, dataIndex: number) => !!outlierSeries[seriesIndex]?.data[dataIndex]

const seriesConverted = convertSeries(series, domainPoints, undefined, seriesHasOutliers, 46)

expect(seriesConverted).toMatchObject([
{ x: 'cat 1', Test: 10, Test2: 46, outliersTest2: { series: 'Test2', value: 50 } },
{ x: 'cat 2', Test: 20, Test2: 40 },
{ x: 'cat 3', Test: 30, Test2: 30 },
{ x: 'cat 4', Test: 40, Test2: 20 },
{ x: 'cat 5', Test: 46, Test2: 10, outliersTest: { series: 'Test', value: 50 } },
])
})

it('should convert category reference areas correctly', () => {
const series = seriesData

const referenceAreas: ReferenceAreaWithPercents<string>[] = [
{
name: 'ref1',
Expand All @@ -49,10 +83,11 @@ describe('convert series to Recharts format', () => {
],
},
]
const domain = ['cat 1', 'cat 2', 'cat 3', 'cat 4', 'cat 5']
const domain = stringDomain
const domainPoints = domain

const seriesConverted = convertSeries(series, domainPoints, referenceAreas)

expect(seriesConverted).toMatchObject([
{ x: 'cat 1', Test: 1, Test2: 5, ref1: 0 },
{ x: 'cat 2', Test: 2, Test2: 4, ref1: 120 },
Expand Down Expand Up @@ -81,9 +116,10 @@ describe('convert series to Recharts format', () => {
},
]

const domainPoints = [0, 10, 20, 30, 40]
const domainPoints = numericDomainPoints

const seriesConverted = convertSeries(series, domainPoints, undefined)

expect(seriesConverted).toMatchObject([
{ x: 0, Test: 1, Test2: 5 },
{ x: 10, Test: 2, Test2: 4 },
Expand All @@ -94,6 +130,55 @@ describe('convert series to Recharts format', () => {
])
})

it('should convert categories correctly handling outliers', () => {
const series: ChartSeries<number>[] = [
{
name: 'Test',
data: [10, 20, 30, 40, 50],
},
{
name: 'Test2',
data: [50, 40, 30, 20, 10],
},
{
name: 'Test3',
data: [
{ x: 20, y: 20 },
{ x: 15, y: 15 },
],
},
]
const outlierSeries: ChartSeries<number>[] = [
{
name: 'outliersTest',
data: [0, 0, 0, 0, 1],
},
{
name: 'outliersTest2',
data: [1, 0, 0, 0, 0],
},
{
name: 'outliersTest3',
data: [0, 0],
},
]

const domainPoints = numericDomainPoints

const seriesHasOutliers = (seriesIndex: number, dataIndex: number) => !!outlierSeries[seriesIndex]?.data[dataIndex]

const seriesConverted = convertSeries(series, domainPoints, undefined, seriesHasOutliers, 46)

expect(seriesConverted).toMatchObject([
{ x: 0, Test: 10, Test2: 46, outliersTest2: { series: 'Test2', value: 50 } },
{ x: 10, Test: 20, Test2: 40 },
{ x: 15, Test3: 15 },
{ x: 20, Test: 30, Test2: 30, Test3: 20 },
{ x: 30, Test: 40, Test2: 20 },
{ x: 40, Test: 46, Test2: 10, outliersTest: { series: 'Test', value: 50 } },
])
})

it('should convert numeric series reference areas correctly', () => {
const series: ChartSeries<number>[] = [
{
Expand All @@ -120,6 +205,7 @@ describe('convert series to Recharts format', () => {
const domainPoints = [20, 30, 40, 50, 60]

const seriesConverted = convertSeries(series, domainPoints, referenceAreas)

expect(seriesConverted).toMatchObject([
{ x: 20, Test: 1, Test2: 5, ref1: 0 },
{ x: 30, Test: 2, Test2: 4, ref1: 60 },
Expand Down
89 changes: 89 additions & 0 deletions src/components/Chart/getOutlierSeries.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { splitOutlierSeries } from './getOutlierSeries'
import { AxisDomain } from './model'

describe('splitOutlierSeries', () => {
const yDomain: AxisDomain = { init: 0, end: 100, step: 20 }
const xDomain: AxisDomain = { init: 0, end: 5, step: 1 }
const domainPoints = [0, 1, 2, 3, 4, 5]
const chartSeries = [{ name: 'test', data: [20, 50, 120] }]

it.each(['auto', 'expand-domain'] as const)(
'should get outlier series from series with outlier when request outliers based on the suportOutlier param',
(outliers: 'auto' | 'expand-domain') => {
const series = chartSeries

const { rangedSeries, outlierSeries } = splitOutlierSeries(series, xDomain, domainPoints, yDomain, outliers)

expect(outlierSeries[0]).toHaveProperty('name', 'outlier' + series[0].name)
expect(outlierSeries[0]).toHaveProperty('dataKey', 'outlier')
expect(outlierSeries[0]).toHaveProperty('data', [false, false, outliers === 'auto'])
expect(rangedSeries[0]).toHaveProperty('name', series[0].name)
expect(rangedSeries[0]).toHaveProperty('dataKey', series[0].name)
expect(rangedSeries[0]).toHaveProperty('data', series[0].data)
}
)

it('should handle series with values inside and outside yDomain when outliers is set to auto', () => {
const series = chartSeries

const { outlierSeries, rangedSeries, hasOutliers } = splitOutlierSeries(
series,
xDomain,
domainPoints,
yDomain,
'auto'
)

expect(outlierSeries[0].data).toEqual([false, false, true])
expect(rangedSeries[0].data).toEqual([20, 50, 120])
expect(hasOutliers).toBe(true)
})

it('should consider all data within range when outliers is set to expand-domain', () => {
const series = chartSeries

const { outlierSeries, rangedSeries, hasOutliers } = splitOutlierSeries(
series,
xDomain,
domainPoints,
yDomain,
'expand-domain'
)

expect(outlierSeries[0].data).toEqual([false, false, false])
expect(rangedSeries[0].data).toEqual([20, 50, 120])
expect(hasOutliers).toBe(false)
})

it('should return hasOutliers as false if no values exceed yDomain', () => {
const series = [{ name: 'noOutliers', data: [10, 20, 30] }]

const { outlierSeries, rangedSeries, hasOutliers } = splitOutlierSeries(
series,
xDomain,
domainPoints,
yDomain,
'auto'
)

expect(outlierSeries[0].data).toEqual([false, false, false])
expect(rangedSeries[0].data).toEqual([10, 20, 30])
expect(hasOutliers).toBe(false)
})

it('should handle series with empty data', () => {
const series = [{ name: 'emptySeries', data: [] }]

const { outlierSeries, rangedSeries, hasOutliers } = splitOutlierSeries(
series,
xDomain,
domainPoints,
yDomain,
'auto'
)

expect(outlierSeries[0].data).toEqual([])
expect(rangedSeries[0].data).toEqual([])
expect(hasOutliers).toBe(false)
})
})

0 comments on commit 2cbe158

Please sign in to comment.