Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lens] Fix formula test type issue #178507

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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