|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +import { isErrorEmbeddable, IContainer } from '../../embeddable_plugin'; |
| 20 | +import { DashboardContainer, DashboardPanelState } from '../embeddable'; |
| 21 | +import { getSampleDashboardInput, getSampleDashboardPanel } from '../test_helpers'; |
| 22 | +import { |
| 23 | + CONTACT_CARD_EMBEDDABLE, |
| 24 | + ContactCardEmbeddableFactory, |
| 25 | + ContactCardEmbeddable, |
| 26 | + ContactCardEmbeddableInput, |
| 27 | + ContactCardEmbeddableOutput, |
| 28 | +} from '../../embeddable_plugin_test_samples'; |
| 29 | +import { coreMock } from '../../../../../core/public/mocks'; |
| 30 | +import { CoreStart } from 'kibana/public'; |
| 31 | +import { ClonePanelAction } from '.'; |
| 32 | + |
| 33 | +// eslint-disable-next-line |
| 34 | +import { embeddablePluginMock } from 'src/plugins/embeddable/public/mocks'; |
| 35 | + |
| 36 | +const { setup, doStart } = embeddablePluginMock.createInstance(); |
| 37 | +setup.registerEmbeddableFactory( |
| 38 | + CONTACT_CARD_EMBEDDABLE, |
| 39 | + new ContactCardEmbeddableFactory((() => null) as any, {} as any) |
| 40 | +); |
| 41 | +const start = doStart(); |
| 42 | + |
| 43 | +let container: DashboardContainer; |
| 44 | +let embeddable: ContactCardEmbeddable; |
| 45 | +let coreStart: CoreStart; |
| 46 | +beforeEach(async () => { |
| 47 | + coreStart = coreMock.createStart(); |
| 48 | + coreStart.savedObjects.client = { |
| 49 | + ...coreStart.savedObjects.client, |
| 50 | + get: jest.fn().mockImplementation(() => ({ attributes: { title: 'Holy moly' } })), |
| 51 | + find: jest.fn().mockImplementation(() => ({ total: 15 })), |
| 52 | + create: jest.fn().mockImplementation(() => ({ id: 'brandNewSavedObject' })), |
| 53 | + }; |
| 54 | + |
| 55 | + const options = { |
| 56 | + ExitFullScreenButton: () => null, |
| 57 | + SavedObjectFinder: () => null, |
| 58 | + application: {} as any, |
| 59 | + embeddable: start, |
| 60 | + inspector: {} as any, |
| 61 | + notifications: {} as any, |
| 62 | + overlays: coreStart.overlays, |
| 63 | + savedObjectMetaData: {} as any, |
| 64 | + uiActions: {} as any, |
| 65 | + }; |
| 66 | + const input = getSampleDashboardInput({ |
| 67 | + panels: { |
| 68 | + '123': getSampleDashboardPanel<ContactCardEmbeddableInput>({ |
| 69 | + explicitInput: { firstName: 'Kibanana', id: '123' }, |
| 70 | + type: CONTACT_CARD_EMBEDDABLE, |
| 71 | + }), |
| 72 | + }, |
| 73 | + }); |
| 74 | + container = new DashboardContainer(input, options); |
| 75 | + |
| 76 | + const contactCardEmbeddable = await container.addNewEmbeddable< |
| 77 | + ContactCardEmbeddableInput, |
| 78 | + ContactCardEmbeddableOutput, |
| 79 | + ContactCardEmbeddable |
| 80 | + >(CONTACT_CARD_EMBEDDABLE, { |
| 81 | + firstName: 'Kibana', |
| 82 | + }); |
| 83 | + |
| 84 | + if (isErrorEmbeddable(contactCardEmbeddable)) { |
| 85 | + throw new Error('Failed to create embeddable'); |
| 86 | + } else { |
| 87 | + embeddable = contactCardEmbeddable; |
| 88 | + } |
| 89 | +}); |
| 90 | + |
| 91 | +test('Clone adds a new embeddable', async () => { |
| 92 | + const dashboard = embeddable.getRoot() as IContainer; |
| 93 | + const originalPanelCount = Object.keys(dashboard.getInput().panels).length; |
| 94 | + const originalPanelKeySet = new Set(Object.keys(dashboard.getInput().panels)); |
| 95 | + const action = new ClonePanelAction(coreStart); |
| 96 | + await action.execute({ embeddable }); |
| 97 | + expect(Object.keys(container.getInput().panels).length).toEqual(originalPanelCount + 1); |
| 98 | + const newPanelId = Object.keys(container.getInput().panels).find( |
| 99 | + key => !originalPanelKeySet.has(key) |
| 100 | + ); |
| 101 | + expect(newPanelId).toBeDefined(); |
| 102 | + const newPanel = container.getInput().panels[newPanelId!]; |
| 103 | + expect(newPanel.type).toEqual(embeddable.type); |
| 104 | +}); |
| 105 | + |
| 106 | +test('Clones an embeddable without a saved object ID', async () => { |
| 107 | + const dashboard = embeddable.getRoot() as IContainer; |
| 108 | + const panel = dashboard.getInput().panels[embeddable.id] as DashboardPanelState; |
| 109 | + const action = new ClonePanelAction(coreStart); |
| 110 | + // @ts-ignore |
| 111 | + const newPanel = await action.cloneEmbeddable(panel, embeddable.type); |
| 112 | + expect(newPanel.type).toEqual(embeddable.type); |
| 113 | +}); |
| 114 | + |
| 115 | +test('Clones an embeddable with a saved object ID', async () => { |
| 116 | + const dashboard = embeddable.getRoot() as IContainer; |
| 117 | + const panel = dashboard.getInput().panels[embeddable.id] as DashboardPanelState; |
| 118 | + panel.explicitInput.savedObjectId = 'holySavedObjectBatman'; |
| 119 | + const action = new ClonePanelAction(coreStart); |
| 120 | + // @ts-ignore |
| 121 | + const newPanel = await action.cloneEmbeddable(panel, embeddable.type); |
| 122 | + expect(coreStart.savedObjects.client.get).toHaveBeenCalledTimes(1); |
| 123 | + expect(coreStart.savedObjects.client.find).toHaveBeenCalledTimes(1); |
| 124 | + expect(coreStart.savedObjects.client.create).toHaveBeenCalledTimes(1); |
| 125 | + expect(newPanel.type).toEqual(embeddable.type); |
| 126 | +}); |
| 127 | + |
| 128 | +test('Gets a unique title ', async () => { |
| 129 | + coreStart.savedObjects.client.find = jest.fn().mockImplementation(({ search }) => { |
| 130 | + if (search === '"testFirstTitle"') return { total: 1 }; |
| 131 | + else if (search === '"testSecondTitle"') return { total: 41 }; |
| 132 | + else if (search === '"testThirdTitle"') return { total: 90 }; |
| 133 | + }); |
| 134 | + const action = new ClonePanelAction(coreStart); |
| 135 | + // @ts-ignore |
| 136 | + expect(await action.getUniqueTitle('testFirstTitle', embeddable.type)).toEqual( |
| 137 | + 'testFirstTitle (copy)' |
| 138 | + ); |
| 139 | + // @ts-ignore |
| 140 | + expect(await action.getUniqueTitle('testSecondTitle (copy 39)', embeddable.type)).toEqual( |
| 141 | + 'testSecondTitle (copy 40)' |
| 142 | + ); |
| 143 | + // @ts-ignore |
| 144 | + expect(await action.getUniqueTitle('testSecondTitle (copy 20)', embeddable.type)).toEqual( |
| 145 | + 'testSecondTitle (copy 40)' |
| 146 | + ); |
| 147 | + // @ts-ignore |
| 148 | + expect(await action.getUniqueTitle('testThirdTitle', embeddable.type)).toEqual( |
| 149 | + 'testThirdTitle (copy 89)' |
| 150 | + ); |
| 151 | + // @ts-ignore |
| 152 | + expect(await action.getUniqueTitle('testThirdTitle (copy 10000)', embeddable.type)).toEqual( |
| 153 | + 'testThirdTitle (copy 89)' |
| 154 | + ); |
| 155 | +}); |
0 commit comments