Skip to content

Commit

Permalink
stabilize annotation code (#137101)
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 authored Jul 26, 2022
1 parent d642f8f commit 7aad7b4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
export interface ReferenceLineAnnotationConfig {
id: string;
name?: string;
value: number;
value?: number;
nextValue?: number;
icon?: AvailableReferenceLineIcon;
lineWidth?: number;
Expand Down Expand Up @@ -103,7 +103,7 @@ export const ReferenceLineAnnotations: FC<Props> = ({
const dataValues = {
dataValue: value,
header: name,
details: formatter?.convert(value) || value.toString(),
details: formatter?.convert(value) || value?.toString(),
};

const line = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* Side Public License, v 1.
*/

import { LineAnnotation, RectAnnotation } from '@elastic/charts';
import { shallow } from 'enzyme';
import { Chart, LineAnnotation, RectAnnotation } from '@elastic/charts';
import { mount, shallow } from 'enzyme';
import React from 'react';
import { Datatable } from '@kbn/expressions-plugin/common';
import { FieldFormat } from '@kbn/field-formats-plugin/common';
Expand Down Expand Up @@ -131,6 +131,38 @@ describe('ReferenceLines', () => {
};
});

it('should not throw on null data', () => {
const position = getAxisFromId('yAccessorLeft');
const [layer] = createLayers([
{
forAccessor: `yAccessorLeftFirstId`,
position,
lineStyle: 'solid',
fill: 'above',
type: 'referenceLineDecorationConfig',
},
]);
expect(() =>
mount(
<Chart>
<ReferenceLines
{...defaultProps}
layers={[
{
...layer,
table: {
...layer.table,
rows: [{}],
columns: [{ ...layer.table.columns[0], meta: { type: 'number' } }],
},
},
]}
/>
</Chart>
)
).not.toThrow();
});

it.each([
['yAccessorLeft', 'above'],
['yAccessorLeft', 'below'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const getBottomRect = (
headerLabel: string | undefined,
isFillAbove: boolean,
formatter: FieldFormat | undefined,
currentValue: number,
currentValue?: number,
nextValue?: number
) => ({
coordinates: {
Expand All @@ -131,14 +131,14 @@ export const getBottomRect = (
y1: undefined,
},
header: headerLabel,
details: formatter?.convert(currentValue) || currentValue.toString(),
details: formatter?.convert(currentValue) || currentValue?.toString(),
});

export const getHorizontalRect = (
headerLabel: string | undefined,
isFillAbove: boolean,
formatter: FieldFormat | undefined,
currentValue: number,
currentValue?: number,
nextValue?: number
) => ({
coordinates: {
Expand All @@ -148,7 +148,7 @@ export const getHorizontalRect = (
y1: isFillAbove ? nextValue : currentValue,
},
header: headerLabel,
details: formatter?.convert(currentValue) || currentValue.toString(),
details: formatter?.convert(currentValue) || currentValue?.toString(),
});

const sortReferenceLinesByGroup = (referenceLines: ReferenceLineConfig[], group: FillStyle) => {
Expand Down

0 comments on commit 7aad7b4

Please sign in to comment.