|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import { |
| 8 | + ExploreDataContextMenuAction, |
| 9 | + ACTION_EXPLORE_DATA, |
| 10 | + Params, |
| 11 | + PluginDeps, |
| 12 | +} from './explore_data_context_menu_action'; |
| 13 | +import { coreMock } from '../../../../../../src/core/public/mocks'; |
| 14 | +import { UrlGeneratorContract } from '../../../../../../src/plugins/share/public'; |
| 15 | +import { i18n } from '@kbn/i18n'; |
| 16 | +import { |
| 17 | + VisualizeEmbeddableContract, |
| 18 | + VISUALIZE_EMBEDDABLE_TYPE, |
| 19 | +} from '../../../../../../src/plugins/visualizations/public'; |
| 20 | +import { ViewMode } from '../../../../../../src/plugins/embeddable/public'; |
| 21 | + |
| 22 | +const i18nTranslateSpy = (i18n.translate as unknown) as jest.SpyInstance; |
| 23 | + |
| 24 | +jest.mock('@kbn/i18n', () => ({ |
| 25 | + i18n: { |
| 26 | + translate: jest.fn((key, options) => options.defaultMessage), |
| 27 | + }, |
| 28 | +})); |
| 29 | + |
| 30 | +afterEach(() => { |
| 31 | + i18nTranslateSpy.mockClear(); |
| 32 | +}); |
| 33 | + |
| 34 | +const setup = () => { |
| 35 | + type UrlGenerator = UrlGeneratorContract<'DISCOVER_APP_URL_GENERATOR'>; |
| 36 | + |
| 37 | + const core = coreMock.createStart(); |
| 38 | + |
| 39 | + const urlGenerator: UrlGenerator = ({ |
| 40 | + id: ACTION_EXPLORE_DATA, |
| 41 | + createUrl: jest.fn(() => Promise.resolve('/xyz/app/discover/foo#bar')), |
| 42 | + } as unknown) as UrlGenerator; |
| 43 | + |
| 44 | + const plugins: PluginDeps = { |
| 45 | + discover: { |
| 46 | + urlGenerator, |
| 47 | + }, |
| 48 | + }; |
| 49 | + |
| 50 | + const params: Params = { |
| 51 | + start: () => ({ |
| 52 | + plugins, |
| 53 | + self: {}, |
| 54 | + core, |
| 55 | + }), |
| 56 | + }; |
| 57 | + const action = new ExploreDataContextMenuAction(params); |
| 58 | + |
| 59 | + const input = { |
| 60 | + viewMode: ViewMode.VIEW, |
| 61 | + }; |
| 62 | + |
| 63 | + const output = { |
| 64 | + indexPatterns: [ |
| 65 | + { |
| 66 | + id: 'index-ptr-foo', |
| 67 | + }, |
| 68 | + ], |
| 69 | + }; |
| 70 | + |
| 71 | + const embeddable: VisualizeEmbeddableContract = ({ |
| 72 | + type: VISUALIZE_EMBEDDABLE_TYPE, |
| 73 | + getInput: () => input, |
| 74 | + getOutput: () => output, |
| 75 | + } as unknown) as VisualizeEmbeddableContract; |
| 76 | + |
| 77 | + const context = { |
| 78 | + embeddable, |
| 79 | + }; |
| 80 | + |
| 81 | + return { core, plugins, urlGenerator, params, action, input, output, embeddable, context }; |
| 82 | +}; |
| 83 | + |
| 84 | +describe('"Explore underlying data" panel action', () => { |
| 85 | + test('action has Discover icon', () => { |
| 86 | + const { action } = setup(); |
| 87 | + expect(action.getIconType()).toBe('discoverApp'); |
| 88 | + }); |
| 89 | + |
| 90 | + test('title is "Explore underlying data"', () => { |
| 91 | + const { action } = setup(); |
| 92 | + expect(action.getDisplayName()).toBe('Explore underlying data'); |
| 93 | + }); |
| 94 | + |
| 95 | + test('translates title', () => { |
| 96 | + expect(i18nTranslateSpy).toHaveBeenCalledTimes(0); |
| 97 | + |
| 98 | + setup().action.getDisplayName(); |
| 99 | + |
| 100 | + expect(i18nTranslateSpy).toHaveBeenCalledTimes(1); |
| 101 | + expect(i18nTranslateSpy.mock.calls[0][0]).toBe( |
| 102 | + 'xpack.discover.FlyoutCreateDrilldownAction.displayName' |
| 103 | + ); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('isCompatible()', () => { |
| 107 | + test('returns true when all conditions are met', async () => { |
| 108 | + const { action, context } = setup(); |
| 109 | + |
| 110 | + const isCompatible = await action.isCompatible(context); |
| 111 | + |
| 112 | + expect(isCompatible).toBe(true); |
| 113 | + }); |
| 114 | + |
| 115 | + test('returns false when URL generator is not present', async () => { |
| 116 | + const { action, plugins, context } = setup(); |
| 117 | + (plugins.discover as any).urlGenerator = undefined; |
| 118 | + |
| 119 | + const isCompatible = await action.isCompatible(context); |
| 120 | + |
| 121 | + expect(isCompatible).toBe(false); |
| 122 | + }); |
| 123 | + |
| 124 | + test('returns false if embeddable is not Visualize embeddable', async () => { |
| 125 | + const { action, embeddable, context } = setup(); |
| 126 | + (embeddable as any).type = 'NOT_VISUALIZE_EMBEDDABLE'; |
| 127 | + |
| 128 | + const isCompatible = await action.isCompatible(context); |
| 129 | + |
| 130 | + expect(isCompatible).toBe(false); |
| 131 | + }); |
| 132 | + |
| 133 | + test('returns false if embeddable does not have index patterns', async () => { |
| 134 | + const { action, output, context } = setup(); |
| 135 | + delete output.indexPatterns; |
| 136 | + |
| 137 | + const isCompatible = await action.isCompatible(context); |
| 138 | + |
| 139 | + expect(isCompatible).toBe(false); |
| 140 | + }); |
| 141 | + |
| 142 | + test('returns false if embeddable index patterns are empty', async () => { |
| 143 | + const { action, output, context } = setup(); |
| 144 | + output.indexPatterns = []; |
| 145 | + |
| 146 | + const isCompatible = await action.isCompatible(context); |
| 147 | + |
| 148 | + expect(isCompatible).toBe(false); |
| 149 | + }); |
| 150 | + |
| 151 | + test('returns false if dashboard is in edit mode', async () => { |
| 152 | + const { action, input, context } = setup(); |
| 153 | + input.viewMode = ViewMode.EDIT; |
| 154 | + |
| 155 | + const isCompatible = await action.isCompatible(context); |
| 156 | + |
| 157 | + expect(isCompatible).toBe(false); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + describe('getHref()', () => { |
| 162 | + test('returns URL path generated by URL generator', async () => { |
| 163 | + const { action, context } = setup(); |
| 164 | + |
| 165 | + const href = await action.getHref(context); |
| 166 | + |
| 167 | + expect(href).toBe('/xyz/app/discover/foo#bar'); |
| 168 | + }); |
| 169 | + |
| 170 | + test('calls URL generator with right arguments', async () => { |
| 171 | + const { action, urlGenerator, context } = setup(); |
| 172 | + |
| 173 | + expect(urlGenerator.createUrl).toHaveBeenCalledTimes(0); |
| 174 | + |
| 175 | + await action.getHref(context); |
| 176 | + |
| 177 | + expect(urlGenerator.createUrl).toHaveBeenCalledTimes(1); |
| 178 | + expect(urlGenerator.createUrl).toHaveBeenCalledWith({ |
| 179 | + indexPatternId: 'index-ptr-foo', |
| 180 | + }); |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + describe('execute()', () => { |
| 185 | + test('calls platform SPA navigation method', async () => { |
| 186 | + const { action, context, core } = setup(); |
| 187 | + |
| 188 | + expect(core.application.navigateToApp).toHaveBeenCalledTimes(0); |
| 189 | + |
| 190 | + await action.execute(context); |
| 191 | + |
| 192 | + expect(core.application.navigateToApp).toHaveBeenCalledTimes(1); |
| 193 | + }); |
| 194 | + |
| 195 | + test('calls platform SPA navigation method with right arguments', async () => { |
| 196 | + const { action, context, core } = setup(); |
| 197 | + |
| 198 | + await action.execute(context); |
| 199 | + |
| 200 | + expect(core.application.navigateToApp).toHaveBeenCalledTimes(1); |
| 201 | + expect(core.application.navigateToApp.mock.calls[0]).toEqual([ |
| 202 | + 'discover', |
| 203 | + { |
| 204 | + path: '/foo#bar', |
| 205 | + }, |
| 206 | + ]); |
| 207 | + }); |
| 208 | + }); |
| 209 | +}); |
0 commit comments