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

[Vis Augmenter Add UT for few fns #4516

Merged
merged 2 commits into from
Jul 10, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### 🔩 Tests

- [Vis Augmenter Add UT for few fns ([#4516](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/4516))

## [2.8.0 - 2023-06-06](https://github.com/opensearch-project/OpenSearch-Dashboards/releases/tag/2.8.0)

### Deprecations
Expand Down
27 changes: 27 additions & 0 deletions src/plugins/vis_augmenter/public/utils/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,33 @@ describe('utils', () => {
} as SavedObjectOpenSearchDashboardsServicesWithAugmentVis);
expect((await getAugmentVisSavedObjs(visId1, loader)).length).toEqual(0);
});
it('throws error when feature setting is disabled', async () => {
uiSettingsMock.get.mockImplementation((key: string) => {
return false;
});
const loader = createSavedAugmentVisLoader({
savedObjectsClient: getMockAugmentVisSavedObjectClient([]),
} as SavedObjectOpenSearchDashboardsServicesWithAugmentVis);
try {
await getAugmentVisSavedObjs(visId1, loader);
} catch (e: any) {
expect(
e.message.includes(
'Visualization augmentation is disabled, please enable visualization:enablePluginAugmentation.'
)
);
}
});
it('returns no matching saved objs when loader throws error', async () => {
const loader = createSavedAugmentVisLoader({
savedObjectsClient: {
findAll: () => {
return new Error();
},
},
} as SavedObjectOpenSearchDashboardsServicesWithAugmentVis);
expect((await getAugmentVisSavedObjs(visId3, loader)).length).toEqual(0);
});
it('returns one matching saved obj', async () => {
const loader = createSavedAugmentVisLoader({
savedObjectsClient: getMockAugmentVisSavedObjectClient([obj1]),
Expand Down
10 changes: 3 additions & 7 deletions src/plugins/vis_augmenter/public/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const getAugmentVisSavedObjs = async (
visId: string | undefined,
loader: SavedAugmentVisLoader | undefined,
uiSettings?: IUiSettingsClient | undefined
): Promise<ISavedAugmentVis[]> => {
): Promise<ISavedAugmentVis[] | Error> => {
// Using optional services provided, or the built-in services from this plugin
const config = uiSettings !== undefined ? uiSettings : getUISettings();
const isAugmentationEnabled = config.get(PLUGIN_AUGMENTATION_ENABLE_SETTING);
Expand All @@ -69,12 +69,8 @@ export const getAugmentVisSavedObjs = async (
'Visualization augmentation is disabled, please enable visualization:enablePluginAugmentation.'
);
}
try {
const allSavedObjects = await getAllAugmentVisSavedObjs(loader);
return allSavedObjects.filter((hit: ISavedAugmentVis) => hit.visId === visId);
} catch (e) {
return [] as ISavedAugmentVis[];
}
const allSavedObjects = await getAllAugmentVisSavedObjs(loader);
return allSavedObjects.filter((hit: ISavedAugmentVis) => hit.visId === visId);
};

/**
Expand Down
145 changes: 144 additions & 1 deletion src/plugins/vis_augmenter/public/vega/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
OpenSearchDashboardsDatatable,
OpenSearchDashboardsDatatableColumn,
} from '../../../expressions/public';
import { YAxisConfig } from '../../../vis_type_vega/public';
import {
enableVisLayersInSpecConfig,
isVisLayerColumn,
Expand All @@ -16,8 +17,18 @@ import {
addPointInTimeEventsLayersToTable,
addPointInTimeEventsLayersToSpec,
generateVisLayerTooltipFields,
addVisEventSignalsToSpecConfig,
calculateYAxisPadding,
augmentEventChartSpec,
} from './helpers';
import { VIS_LAYER_COLUMN_TYPE, VisLayerTypes, PointInTimeEventsVisLayer, VisLayer } from '../';
import {
VIS_LAYER_COLUMN_TYPE,
VisLayerTypes,
PointInTimeEventsVisLayer,
VisLayer,
VisFlyoutContext,
VisAugmenterEmbeddableConfig,
} from '../';
import {
TEST_DATATABLE_MULTIPLE_VIS_LAYERS,
TEST_DATATABLE_NO_VIS_LAYERS,
Expand Down Expand Up @@ -460,4 +471,136 @@ describe('helpers', function () {
expect(returnSpec).toEqual(expectedSpec);
});
});

describe('addVisEventSignalsToSpecConfig()', () => {
it('vis event signal is added for point in time annotations', () => {
const startSpec = {
kibana: {},
};
const endSpec = addVisEventSignalsToSpecConfig(startSpec);
expect(endSpec.kibana.signals.POINT_IN_TIME_ANNOTATION.length).toBeGreaterThan(0);
});
it('tooltip configuration is specified', () => {
const startSpec = {
kibana: {},
};
const endSpec = addVisEventSignalsToSpecConfig(startSpec);
expect(endSpec.tooltips).not.toBeNull();
});
});

describe('calculateYAxisPadding()', () => {
it('calculation includes sum of all y axis fields', () => {
const sampleConfig = {
minExtent: 1,
offset: 1,
translate: 1,
domainWidth: 1,
labelPadding: 1,
titlePadding: 1,
tickOffset: 1,
tickSize: 1,
} as YAxisConfig;
// derived from each value in sample config + default padding of 3
expect(calculateYAxisPadding(sampleConfig)).toEqual(11);
});
it('calculation defaults to 0 for missing y axis fields', () => {
const sampleConfig = {
minExtent: 1,
} as YAxisConfig;
// derived from each value in sample config + default padding of 3
expect(calculateYAxisPadding(sampleConfig)).toEqual(4);
});
});

describe('augmentEventChartSpec()', () => {
it('not in flyout - no change', () => {
const config = {
inFlyout: false,
flyoutContext: VisFlyoutContext.BASE_VIS,
} as VisAugmenterEmbeddableConfig;
const origSpec = {
config: { some: 'config' },
vconcat: [{ base: 'vis' }, { event: 'vis' }],
};

expect(augmentEventChartSpec(config, origSpec)).toEqual(origSpec);
});
it('in flyout + base vis context', () => {
const config = {
inFlyout: true,
flyoutContext: VisFlyoutContext.BASE_VIS,
} as VisAugmenterEmbeddableConfig;
const origSpec = {
config: { some: 'config' },
vconcat: [{ base: 'vis' }, { event: 'vis' }],
};

const returnSpec = augmentEventChartSpec(config, origSpec) as any;
// legend should be forced to the top
expect(returnSpec.config.legend.orient).toEqual('top');
// y-axis should be a set, consistent, nonzero width
expect(returnSpec.config.axisY.minExtent).toBeGreaterThan(0);
expect(returnSpec.config.axisY.maxExtent).toBeGreaterThan(0);
expect(returnSpec.config.axisY.offset).toEqual(0);
expect(returnSpec.vconcat.length).toEqual(2);
});
it('in flyout + event vis context', () => {
const config = {
inFlyout: true,
flyoutContext: VisFlyoutContext.EVENT_VIS,
} as VisAugmenterEmbeddableConfig;
const origSpec = {
config: { some: 'config' },
vconcat: [
{ base: 'vis' },
{
event: 'vis',
encoding: {
x: {
axis: {
some: 'field',
},
},
},
mark: {
some: 'other-field',
},
},
],
};

const returnSpec = augmentEventChartSpec(config, origSpec) as any;
const xAxisConfig = returnSpec.vconcat[0].encoding.x.axis;
// y-axis should be a set, consistent, nonzero width
expect(returnSpec.config.axisY.minExtent).toBeGreaterThan(0);
expect(returnSpec.config.axisY.maxExtent).toBeGreaterThan(0);
expect(returnSpec.config.axisY.offset).toEqual(0);
// x-axis should be hidden
expect(xAxisConfig.grid).toEqual(false);
expect(xAxisConfig.ticks).toEqual(false);
expect(xAxisConfig.labels).toEqual(false);
expect(xAxisConfig.title).toEqual(null);
expect(returnSpec.vconcat.length).toEqual(1);
});
it('in flyout + timeline vis context', () => {
const config = {
inFlyout: true,
flyoutContext: VisFlyoutContext.TIMELINE_VIS,
} as VisAugmenterEmbeddableConfig;
const origSpec = {
config: { some: 'config' },
vconcat: [{ base: 'vis' }, { event: 'vis' }],
};

const returnSpec = augmentEventChartSpec(config, origSpec) as any;
// y-axis should be a set, consistent, nonzero width
expect(returnSpec.config.axisY.minExtent).toBeGreaterThan(0);
expect(returnSpec.config.axisY.maxExtent).toBeGreaterThan(0);
expect(returnSpec.config.axisY.offset).toEqual(0);
// should have transform param set on the event chart
expect(returnSpec.vconcat[0].transform.length).toBeGreaterThan(0);
expect(returnSpec.vconcat.length).toEqual(1);
});
});
});
5 changes: 0 additions & 5 deletions src/plugins/vis_augmenter/public/vega/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import moment from 'moment';
import { cloneDeep, isEmpty, get } from 'lodash';
import { Item } from 'vega';
import { YAxisConfig } from 'src/plugins/vis_type_vega/public';
import {
OpenSearchDashboardsDatatable,
Expand Down Expand Up @@ -408,10 +407,6 @@ export const addPointInTimeEventsLayersToSpec = (
return newSpec;
};

export const isPointInTimeAnnotation = (item?: Item | null) => {
return item?.datum?.annotationType === VisAnnotationType.POINT_IN_TIME_ANNOTATION;
};

// This is the total y-axis padding such that if this is added to the "padding" value of the view, if there is no axis,
// it will align values on the x-axis
export const calculateYAxisPadding = (config: YAxisConfig): number => {
Expand Down