Skip to content

Commit

Permalink
fix(bar): fix broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wyze committed Jun 28, 2021
1 parent d508d10 commit bc1eb1b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 25 deletions.
2 changes: 2 additions & 0 deletions packages/bar/src/compute/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ export const filterNullValues = <RawDatum extends Record<string, unknown>>(data:
}
return acc
}, {}) as Exclude<RawDatum, null | undefined | false | '' | 0>

export const coerceValue = <T>(value: T) => [value, Number(value)] as const
10 changes: 5 additions & 5 deletions packages/bar/src/compute/grouped.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BarDatum, BarSvgProps, ComputedDatum } from '../types'
import { OrdinalColorScale } from '@nivo/colors'
import { Scale, ScaleBand } from '@nivo/scales'
import { coerceValue, filterNullValues, getIndexScale, normalizeData } from './common'
import { computeScale } from '@nivo/scales'
import { getIndexScale, filterNullValues, normalizeData } from './common'

type Params<RawDatum, XScaleInput, YScaleInput> = {
data: RawDatum[]
Expand Down Expand Up @@ -49,14 +49,14 @@ const generateVerticalGroupedBars = <RawDatum extends Record<string, unknown>>(
const bars = flatten(
keys.map((key, i) =>
range(0, xScale.domain().length).map(index => {
const value = Number(data[index][key])
const [rawValue, value] = coerceValue(data[index][key])
const indexValue = getIndex(data[index])
const x = (xScale(indexValue) ?? 0) + barWidth * i + innerPadding * i
const y = getY(value)
const barHeight = getHeight(value, y)
const barData = {
id: key,
value,
value: rawValue === null ? rawValue : value,
index,
indexValue,
data: cleanedData[index],
Expand Down Expand Up @@ -103,14 +103,14 @@ const generateHorizontalGroupedBars = <RawDatum extends Record<string, unknown>>
const bars = flatten(
keys.map((key, i) =>
range(0, yScale.domain().length).map(index => {
const value = Number(data[index][key])
const [rawValue, value] = coerceValue(data[index][key])
const indexValue = getIndex(data[index])
const x = getX(value)
const y = (yScale(indexValue) ?? 0) + barHeight * i + innerPadding * i
const barWidth = getWidth(value, x)
const barData = {
id: key,
value,
value: rawValue === null ? rawValue : value,
index,
indexValue,
data: cleanedData[index],
Expand Down
8 changes: 5 additions & 3 deletions packages/bar/src/compute/stacked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BarDatum, BarSvgProps, ComputedDatum } from '../types'
import { OrdinalColorScale } from '@nivo/colors'
import { Scale, ScaleBand, computeScale } from '@nivo/scales'
import { Series, SeriesPoint, stack, stackOffsetDiverging } from 'd3-shape'
import { getIndexScale, filterNullValues, normalizeData } from './common'
import { coerceValue, filterNullValues, getIndexScale, normalizeData } from './common'

type StackDatum<RawDatum> = SeriesPoint<RawDatum>

Expand Down Expand Up @@ -43,10 +43,11 @@ const generateVerticalStackedBars = <RawDatum extends Record<string, unknown>>(
const x = xScale(getIndex(d.data)) ?? 0
const y = (getY(d) ?? 0) + innerPadding * 0.5
const barHeight = getHeight(d, y) - innerPadding
const [rawValue, value] = coerceValue(d.data[stackedDataItem.key])

const barData = {
id: stackedDataItem.key,
value: Number(d.data[stackedDataItem.key]),
value: rawValue === null ? rawValue : value,
index: i,
indexValue: index,
data: filterNullValues(d.data),
Expand Down Expand Up @@ -93,10 +94,11 @@ const generateHorizontalStackedBars = <RawDatum extends Record<string, unknown>>
const y = yScale(getIndex(d.data)) ?? 0
const x = (getX(d) ?? 0) + innerPadding * 0.5
const barWidth = getWidth(d, x) - innerPadding
const [rawValue, value] = coerceValue(d.data[stackedDataItem.key])

const barData = {
id: stackedDataItem.key,
value: Number(d.data[stackedDataItem.key]),
value: rawValue === null ? rawValue : value,
index: i,
indexValue: index,
data: filterNullValues(d.data),
Expand Down
2 changes: 1 addition & 1 deletion packages/bar/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type BarDatumWithColor = BarDatum & {

export type ComputedDatum<RawDatum> = {
id: string | number
value: number
value: number | null
index: number
indexValue: string | number
data: Exclude<RawDatum, null | undefined | false | '' | 0>
Expand Down
2 changes: 1 addition & 1 deletion packages/bar/tests/Bar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useState } from 'react'
import renderer from 'react-test-renderer'
import { mount } from 'enzyme'
import { LegendSvg, LegendSvgItem } from '@nivo/legends'
import Bar from '../src/Bar'
import { Bar } from '../src'

it('should render a basic bar chart', () => {
const component = renderer.create(
Expand Down
30 changes: 15 additions & 15 deletions packages/bar/tests/__snapshots__/Bar.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ exports[`should allow grouped horizontal layout 1`] = `
transform="translate(0, 203)"
>
<rect
fill="#e8c1a0"
fill="rgba(232, 193, 160, 1)"
height={86}
onClick={[Function]}
onMouseEnter={[Function]}
Expand All @@ -769,7 +769,7 @@ exports[`should allow grouped horizontal layout 1`] = `
transform="translate(0, 107)"
>
<rect
fill="#e8c1a0"
fill="rgba(232, 193, 160, 1)"
height={86}
onClick={[Function]}
onMouseEnter={[Function]}
Expand All @@ -786,7 +786,7 @@ exports[`should allow grouped horizontal layout 1`] = `
transform="translate(0, 11)"
>
<rect
fill="#e8c1a0"
fill="rgba(232, 193, 160, 1)"
height={86}
onClick={[Function]}
onMouseEnter={[Function]}
Expand Down Expand Up @@ -1673,7 +1673,7 @@ exports[`should allow grouped mode 1`] = `
transform="translate(17, 200)"
>
<rect
fill="#e8c1a0"
fill="rgba(232, 193, 160, 1)"
height={100}
onClick={[Function]}
onMouseEnter={[Function]}
Expand All @@ -1690,7 +1690,7 @@ exports[`should allow grouped mode 1`] = `
transform="translate(178, 100)"
>
<rect
fill="#e8c1a0"
fill="rgba(232, 193, 160, 1)"
height={200}
onClick={[Function]}
onMouseEnter={[Function]}
Expand All @@ -1707,7 +1707,7 @@ exports[`should allow grouped mode 1`] = `
transform="translate(339, 0)"
>
<rect
fill="#e8c1a0"
fill="rgba(232, 193, 160, 1)"
height={300}
onClick={[Function]}
onMouseEnter={[Function]}
Expand Down Expand Up @@ -2477,7 +2477,7 @@ exports[`should allow horizontal layout 1`] = `
transform="translate(0, 203)"
>
<rect
fill="#e8c1a0"
fill="rgba(232, 193, 160, 1)"
height={86}
onClick={[Function]}
onMouseEnter={[Function]}
Expand All @@ -2494,7 +2494,7 @@ exports[`should allow horizontal layout 1`] = `
transform="translate(0, 107)"
>
<rect
fill="#e8c1a0"
fill="rgba(232, 193, 160, 1)"
height={86}
onClick={[Function]}
onMouseEnter={[Function]}
Expand All @@ -2511,7 +2511,7 @@ exports[`should allow horizontal layout 1`] = `
transform="translate(0, 11)"
>
<rect
fill="#e8c1a0"
fill="rgba(232, 193, 160, 1)"
height={86}
onClick={[Function]}
onMouseEnter={[Function]}
Expand Down Expand Up @@ -3398,7 +3398,7 @@ exports[`should allow to disable labels 1`] = `
transform="translate(17, 200)"
>
<rect
fill="#e8c1a0"
fill="rgba(232, 193, 160, 1)"
height={100}
onClick={[Function]}
onMouseEnter={[Function]}
Expand All @@ -3415,7 +3415,7 @@ exports[`should allow to disable labels 1`] = `
transform="translate(178, 100)"
>
<rect
fill="#e8c1a0"
fill="rgba(232, 193, 160, 1)"
height={200}
onClick={[Function]}
onMouseEnter={[Function]}
Expand All @@ -3432,7 +3432,7 @@ exports[`should allow to disable labels 1`] = `
transform="translate(339, 0)"
>
<rect
fill="#e8c1a0"
fill="rgba(232, 193, 160, 1)"
height={300}
onClick={[Function]}
onMouseEnter={[Function]}
Expand Down Expand Up @@ -4319,7 +4319,7 @@ exports[`should render a basic bar chart 1`] = `
transform="translate(17, 200)"
>
<rect
fill="#e8c1a0"
fill="rgba(232, 193, 160, 1)"
height={100}
onClick={[Function]}
onMouseEnter={[Function]}
Expand Down Expand Up @@ -4352,7 +4352,7 @@ exports[`should render a basic bar chart 1`] = `
transform="translate(178, 100)"
>
<rect
fill="#e8c1a0"
fill="rgba(232, 193, 160, 1)"
height={200}
onClick={[Function]}
onMouseEnter={[Function]}
Expand Down Expand Up @@ -4385,7 +4385,7 @@ exports[`should render a basic bar chart 1`] = `
transform="translate(339, 0)"
>
<rect
fill="#e8c1a0"
fill="rgba(232, 193, 160, 1)"
height={300}
onClick={[Function]}
onMouseEnter={[Function]}
Expand Down

0 comments on commit bc1eb1b

Please sign in to comment.