|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { Vis } from '../../../visualizations/public'; |
| 7 | +import { |
| 8 | + buildPipelineFromAugmentVisSavedObjs, |
| 9 | + getAugmentVisSavedObjs, |
| 10 | + isEligibleForVisLayers, |
| 11 | +} from './utils'; |
| 12 | +import { VisLayerTypes, ISavedAugmentVis, VisLayerExpressionFn } from '../types'; |
| 13 | +import { |
| 14 | + createSavedAugmentVisLoader, |
| 15 | + SavedObjectOpenSearchDashboardsServicesWithAugmentVis, |
| 16 | + getMockAugmentVisSavedObjectClient, |
| 17 | + generateAugmentVisSavedObject, |
| 18 | +} from '../saved_augment_vis'; |
| 19 | + |
| 20 | +describe('utils', () => { |
| 21 | + // TODO: redo / update this test suite when eligibility is finalized. |
| 22 | + // Tracked in https://github.com/opensearch-project/OpenSearch-Dashboards/issues/3268 |
| 23 | + describe('isEligibleForVisLayers', () => { |
| 24 | + it('vis is ineligible with invalid type', async () => { |
| 25 | + const vis = ({ |
| 26 | + params: { |
| 27 | + type: 'not-line', |
| 28 | + }, |
| 29 | + } as unknown) as Vis; |
| 30 | + expect(isEligibleForVisLayers(vis)).toEqual(false); |
| 31 | + }); |
| 32 | + it('vis is eligible with valid type', async () => { |
| 33 | + const vis = ({ |
| 34 | + params: { |
| 35 | + type: 'line', |
| 36 | + }, |
| 37 | + } as unknown) as Vis; |
| 38 | + expect(isEligibleForVisLayers(vis)).toEqual(true); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('getAugmentVisSavedObjs', () => { |
| 43 | + const fn = { |
| 44 | + type: VisLayerTypes.PointInTimeEvents, |
| 45 | + name: 'test-fn', |
| 46 | + args: { |
| 47 | + testArg: 'test-value', |
| 48 | + }, |
| 49 | + } as VisLayerExpressionFn; |
| 50 | + const visId1 = 'test-vis-id-1'; |
| 51 | + const visId2 = 'test-vis-id-2'; |
| 52 | + const visId3 = 'test-vis-id-3'; |
| 53 | + const obj1 = generateAugmentVisSavedObject('valid-obj-id-1', fn, visId1); |
| 54 | + const obj2 = generateAugmentVisSavedObject('valid-obj-id-2', fn, visId1); |
| 55 | + const obj3 = generateAugmentVisSavedObject('valid-obj-id-3', fn, visId2); |
| 56 | + |
| 57 | + it('returns no matching saved objs with filtering', async () => { |
| 58 | + const loader = createSavedAugmentVisLoader({ |
| 59 | + savedObjectsClient: getMockAugmentVisSavedObjectClient([obj1, obj2, obj3]), |
| 60 | + } as SavedObjectOpenSearchDashboardsServicesWithAugmentVis); |
| 61 | + expect((await getAugmentVisSavedObjs(visId3, loader)).length).toEqual(0); |
| 62 | + }); |
| 63 | + it('returns no matching saved objs when client returns empty list', async () => { |
| 64 | + const loader = createSavedAugmentVisLoader({ |
| 65 | + savedObjectsClient: getMockAugmentVisSavedObjectClient([]), |
| 66 | + } as SavedObjectOpenSearchDashboardsServicesWithAugmentVis); |
| 67 | + expect((await getAugmentVisSavedObjs(visId1, loader)).length).toEqual(0); |
| 68 | + }); |
| 69 | + it('returns one matching saved obj', async () => { |
| 70 | + const loader = createSavedAugmentVisLoader({ |
| 71 | + savedObjectsClient: getMockAugmentVisSavedObjectClient([obj1]), |
| 72 | + } as SavedObjectOpenSearchDashboardsServicesWithAugmentVis); |
| 73 | + expect((await getAugmentVisSavedObjs(visId1, loader)).length).toEqual(1); |
| 74 | + }); |
| 75 | + it('returns multiple matching saved objs without filtering', async () => { |
| 76 | + const loader = createSavedAugmentVisLoader({ |
| 77 | + savedObjectsClient: getMockAugmentVisSavedObjectClient([obj1, obj2]), |
| 78 | + } as SavedObjectOpenSearchDashboardsServicesWithAugmentVis); |
| 79 | + expect((await getAugmentVisSavedObjs(visId1, loader)).length).toEqual(2); |
| 80 | + }); |
| 81 | + it('returns multiple matching saved objs with filtering', async () => { |
| 82 | + const loader = createSavedAugmentVisLoader({ |
| 83 | + savedObjectsClient: getMockAugmentVisSavedObjectClient([obj1, obj2, obj3]), |
| 84 | + } as SavedObjectOpenSearchDashboardsServicesWithAugmentVis); |
| 85 | + expect((await getAugmentVisSavedObjs(visId1, loader)).length).toEqual(2); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('buildPipelineFromAugmentVisSavedObjs', () => { |
| 90 | + const obj1 = { |
| 91 | + title: 'obj1', |
| 92 | + pluginResourceId: 'obj-1-resource-id', |
| 93 | + visLayerExpressionFn: { |
| 94 | + type: VisLayerTypes.PointInTimeEvents, |
| 95 | + name: 'fn-1', |
| 96 | + args: { |
| 97 | + arg1: 'value-1', |
| 98 | + }, |
| 99 | + }, |
| 100 | + } as ISavedAugmentVis; |
| 101 | + const obj2 = { |
| 102 | + title: 'obj2', |
| 103 | + pluginResourceId: 'obj-2-resource-id', |
| 104 | + visLayerExpressionFn: { |
| 105 | + type: VisLayerTypes.PointInTimeEvents, |
| 106 | + name: 'fn-2', |
| 107 | + args: { |
| 108 | + arg2: 'value-2', |
| 109 | + }, |
| 110 | + }, |
| 111 | + } as ISavedAugmentVis; |
| 112 | + it('catches error with empty array', async () => { |
| 113 | + try { |
| 114 | + buildPipelineFromAugmentVisSavedObjs([]); |
| 115 | + } catch (e: any) { |
| 116 | + expect( |
| 117 | + e.message.includes( |
| 118 | + 'Expression function from augment-vis saved objects could not be generated' |
| 119 | + ) |
| 120 | + ); |
| 121 | + } |
| 122 | + }); |
| 123 | + it('builds with one saved obj', async () => { |
| 124 | + const str = buildPipelineFromAugmentVisSavedObjs([obj1]); |
| 125 | + expect(str).toEqual('fn-1 arg1="value-1"'); |
| 126 | + }); |
| 127 | + it('builds with multiple saved objs', async () => { |
| 128 | + const str = buildPipelineFromAugmentVisSavedObjs([obj1, obj2]); |
| 129 | + expect(str).toEqual(`fn-1 arg1="value-1"\n| fn-2 arg2="value-2"`); |
| 130 | + }); |
| 131 | + }); |
| 132 | +}); |
0 commit comments