Skip to content

Commit

Permalink
✅ Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dej611 committed Jan 17, 2023
1 parent d951be2 commit 4c9ff39
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
20 changes: 20 additions & 0 deletions x-pack/plugins/lens/public/datasources/form_based/mocks.ts
Expand Up @@ -81,6 +81,26 @@ export const createMockedIndexPattern = (someProps?: Partial<IndexPattern>): Ind
lang: 'painless' as const,
script: '1234',
},
{
name: 'runtime-keyword',
displayName: 'Runtime keyword field',
type: 'string',
searchable: true,
aggregatable: true,
runtime: true,
lang: 'painless' as const,
script: 'emit("123")',
},
{
name: 'runtime-number',
displayName: 'Runtime number field',
type: 'number',
searchable: true,
aggregatable: true,
runtime: true,
lang: 'painless' as const,
script: 'emit(123)',
},
];
return {
id: '1',
Expand Down
Expand Up @@ -290,6 +290,44 @@ describe('last_value', () => {
).params.showArrayValues
).toBeTruthy();
});

it('should set show array values if field is runtime and not of type number', () => {
const oldColumn: LastValueIndexPatternColumn = {
operationType: 'last_value',
sourceField: 'bytes',
label: 'Last value of bytes',
isBucketed: false,
dataType: 'number',
params: {
sortField: 'datefield',
showArrayValues: false,
},
};
const indexPattern = createMockedIndexPattern();
const field = indexPattern.fields.find((i) => i.name === 'runtime-keyword')!;

expect(
lastValueOperation.onFieldChange(oldColumn, field).params.showArrayValues
).toBeTruthy();
});

it('should not set show array values if field is runtime and of type number', () => {
const oldColumn: LastValueIndexPatternColumn = {
operationType: 'last_value',
sourceField: 'bytes',
label: 'Last value of bytes',
isBucketed: false,
dataType: 'number',
params: {
sortField: 'datefield',
showArrayValues: false,
},
};
const indexPattern = createMockedIndexPattern();
const field = indexPattern.fields.find((i) => i.name === 'runtime-number')!;

expect(lastValueOperation.onFieldChange(oldColumn, field).params.showArrayValues).toBeFalsy();
});
});

describe('getPossibleOperationForField', () => {
Expand Down Expand Up @@ -480,11 +518,19 @@ describe('last_value', () => {
);
});

it('should set showArrayValues if field is scripted or comes from existing params', () => {
it('should set showArrayValues if field is scripted, non-numeric runtime or comes from existing params', () => {
const indexPattern = createMockedIndexPattern();

const scriptedField = indexPattern.fields.find((field) => field.scripted);
const nonScriptedField = indexPattern.fields.find((field) => !field.scripted);
const runtimeKeywordField = indexPattern.fields.find(
(field) => field.runtime && field.type !== 'number'
);
const runtimeNumericField = indexPattern.fields.find(
(field) => field.runtime && field.type === 'number'
);
const nonScriptedField = indexPattern.fields.find(
(field) => !field.scripted && !field.runtime
);

const localLayer = {
columns: {
Expand All @@ -508,6 +554,22 @@ describe('last_value', () => {
}).params.showArrayValues
).toBeTruthy();

expect(
lastValueOperation.buildColumn({
indexPattern,
layer: localLayer,
field: runtimeKeywordField!,
}).params.showArrayValues
).toBeTruthy();

expect(
lastValueOperation.buildColumn({
indexPattern,
layer: localLayer,
field: runtimeNumericField!,
}).params.showArrayValues
).toBeFalsy();

expect(
lastValueOperation.buildColumn(
{
Expand Down

0 comments on commit 4c9ff39

Please sign in to comment.