Skip to content

Commit

Permalink
[Lens] Fix formula test type issue (elastic#178507)
Browse files Browse the repository at this point in the history
## Summary

Fix elastic#176343 


### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
  • Loading branch information
dej611 authored Mar 13, 2024
1 parent 02efdf1 commit 7c43577
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { insertOrReplaceFormulaColumn } from './parse';
import type { FormBasedLayer } from '../../../types';
import { IndexPattern } from '../../../../../types';
import { TermsIndexPatternColumn } from '../terms';
import { MovingAverageIndexPatternColumn } from '../calculations';
import type { MovingAverageIndexPatternColumn } from '../calculations';
import { StaticValueIndexPatternColumn } from '../static_value';
import { getFilter } from '../helpers';
import { createOperationDefinitionMock } from './mocks/operation_mocks';
Expand All @@ -43,6 +43,7 @@ const operationDefinitionMap: Record<string, GenericOperationDefinition> = {
terms: createOperationDefinitionMock('terms', {}, { scale: 'ordinal' }),
sum: createOperationDefinitionMock('sum', { filterable: true }),
last_value: createOperationDefinitionMock('last_value', {
input: 'field',
getPossibleOperationForField: jest.fn(({ type }) => ({
scale: type === 'string' ? 'ordinal' : 'ratio',
isBucketed: false,
Expand All @@ -54,14 +55,14 @@ const operationDefinitionMap: Record<string, GenericOperationDefinition> = {
filterable: true,
canReduceTimeRange: true,
}),
derivative: createOperationDefinitionMock('derivative', { input: 'fullReference' }),
moving_average: createOperationDefinitionMock('moving_average', {
// @ts-expect-error upgrade typescript v4.9.5
derivative: createOperationDefinitionMock('derivative', {
input: 'fullReference',
}),
moving_average: createOperationDefinitionMock<MovingAverageIndexPatternColumn>('moving_average', {
input: 'fullReference',
operationParams: [{ name: 'window', type: 'number', required: true }],
filterable: true,
getErrorMessage: jest.fn(() => ['mock error']),
// @ts-expect-error upgrade typescript v4.9.5
buildColumn: ({ referenceIds }, columnsParams) => ({
label: 'moving_average',
dataType: 'number',
Expand All @@ -74,7 +75,9 @@ const operationDefinitionMap: Record<string, GenericOperationDefinition> = {
filter: getFilter(undefined, columnsParams),
}),
}),
cumulative_sum: createOperationDefinitionMock('cumulative_sum', { input: 'fullReference' }),
cumulative_sum: createOperationDefinitionMock('cumulative_sum', {
input: 'fullReference',
}),
interval: createOperationDefinitionMock('interval', {
input: 'managedReference',
usedInMath: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ type OperationByInputType<Input extends GenericOperationDefinition['input']> = E
{ input: Input }
>;

export function createOperationDefinitionMock(
export function createOperationDefinitionMock<T extends GenericIndexPatternColumn>(
operation: string,
{
input = 'field',
getErrorMessage,
buildColumn,
...params
}: Partial<GenericOperationDefinition> = {},
}: Partial<GenericOperationDefinition<T>> = {},
{
label = operation,
dataType = 'number',
Expand Down Expand Up @@ -81,13 +81,11 @@ export function createOperationDefinitionMock(
return {
buildColumn:
buildColumn ??
jest.fn(({ referenceIds }, columnParams: PartialColumnParams) => ({
jest.fn(({ referenceIds }, columnParams: PartialColumnParams | undefined) => ({
references: referenceIds,
filter: getFilter(undefined, columnParams),
...sharedColumnParams,
})),
onFieldChange: jest.fn(),
toEsAggsFn: jest.fn(),
getPossibleOperation: jest.fn(() => ({
scale: 'ratio',
dataType: 'number',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,11 +731,13 @@ export type OperationType = string;
* This is an operation definition of an unspecified column out of all possible
* column types.
*/
export type GenericOperationDefinition =
| OperationDefinition<BaseIndexPatternColumn, 'field'>
| OperationDefinition<BaseIndexPatternColumn, 'none'>
| OperationDefinition<BaseIndexPatternColumn, 'fullReference'>
| OperationDefinition<BaseIndexPatternColumn, 'managedReference'>;
export type GenericOperationDefinition<
ColumnType extends BaseIndexPatternColumn = BaseIndexPatternColumn
> =
| OperationDefinition<ColumnType, 'field'>
| OperationDefinition<ColumnType, 'none'>
| OperationDefinition<ColumnType, 'fullReference'>
| OperationDefinition<ColumnType, 'managedReference'>;

/**
* List of all available operation definitions
Expand Down

0 comments on commit 7c43577

Please sign in to comment.