[Lens] fix passing 0 as static value (#118032)

* [Lens] fix passing 0 as static value

* allow computed static_value to be passed

* Update x-pack/plugins/lens/public/xy_visualization/reference_line_helpers.tsx

Co-authored-by: Marco Liberati <dej611@users.noreply.github.com>

* ci fix

Co-authored-by: Marco Liberati <dej611@users.noreply.github.com>
This commit is contained in:
Marta Bondyra 2021-11-11 08:26:48 +01:00 committed by GitHub
parent f555c737d0
commit b87852071b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 11 deletions

View File

@ -1696,7 +1696,7 @@ describe('IndexPattern Data Source', () => {
isBucketed: false,
label: 'Static value: 0',
operationType: 'static_value',
params: { value: 0 },
params: { value: '0' },
references: [],
scale: 'ratio',
},

View File

@ -338,6 +338,36 @@ describe('static_value', () => {
expect(input.prop('value')).toEqual('23');
});
it('should allow 0 as initial value', () => {
const updateLayerSpy = jest.fn();
const zeroLayer = {
...layer,
columns: {
...layer.columns,
col2: {
...layer.columns.col2,
operationType: 'static_value',
references: [],
params: {
value: '0',
},
},
},
} as IndexPatternLayer;
const instance = shallow(
<ParamEditor
{...defaultProps}
layer={zeroLayer}
updateLayer={updateLayerSpy}
columnId="col2"
currentColumn={zeroLayer.columns.col2 as StaticValueIndexPatternColumn}
/>
);
const input = instance.find('[data-test-subj="lns-indexPattern-static_value-input"]');
expect(input.prop('value')).toEqual('0');
});
it('should update state on change', async () => {
const updateLayerSpy = jest.fn();
const instance = mount(

View File

@ -95,7 +95,7 @@ export const staticValueOperation: OperationDefinition<
arguments: {
id: [columnId],
name: [label || defaultLabel],
expression: [isValidNumber(params.value) ? params.value! : String(defaultValue)],
expression: [String(isValidNumber(params.value) ? params.value! : defaultValue)],
},
},
];
@ -118,7 +118,7 @@ export const staticValueOperation: OperationDefinition<
operationType: 'static_value',
isBucketed: false,
scale: 'ratio',
params: { ...previousParams, value: previousParams.value ?? String(defaultValue) },
params: { ...previousParams, value: String(previousParams.value ?? defaultValue) },
references: [],
};
},

View File

@ -120,6 +120,32 @@ describe('reference_line helpers', () => {
).toBe(100);
});
it('should return 0 as result of calculation', () => {
expect(
getStaticValue(
[
{
layerId: 'id-a',
seriesType: 'area',
layerType: 'data',
accessors: ['a'],
yConfig: [{ forAccessor: 'a', axisMode: 'right' }],
} as XYLayerConfig,
],
'yRight',
{
activeData: getActiveData([
{
id: 'id-a',
rows: [{ a: -30 }, { a: 10 }],
},
]),
},
hasAllNumberHistogram
)
).toBe(0);
});
it('should work for no yConfig defined and fallback to left axis', () => {
expect(
getStaticValue(

View File

@ -104,15 +104,14 @@ export function getStaticValue(
) {
return fallbackValue;
}
return (
computeStaticValueForGroup(
filteredLayers,
accessors,
activeData,
groupId !== 'x', // histogram axis should compute the min based on the current data
groupId !== 'x'
) || fallbackValue
const computedValue = computeStaticValueForGroup(
filteredLayers,
accessors,
activeData,
groupId !== 'x', // histogram axis should compute the min based on the current data
groupId !== 'x'
);
return computedValue ?? fallbackValue;
}
function getAccessorCriteriaForGroup(