|
| 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, ReferenceOrValueEmbeddable } from '../../embeddable_plugin'; |
| 20 | +import { DashboardContainer } from '../embeddable'; |
| 21 | +import { getSampleDashboardInput } 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 { UnlinkFromLibraryAction } from '.'; |
| 32 | +import { embeddablePluginMock } from 'src/plugins/embeddable/public/mocks'; |
| 33 | +import { ViewMode } from '../../../../embeddable/public'; |
| 34 | + |
| 35 | +const { setup, doStart } = embeddablePluginMock.createInstance(); |
| 36 | +setup.registerEmbeddableFactory( |
| 37 | + CONTACT_CARD_EMBEDDABLE, |
| 38 | + new ContactCardEmbeddableFactory((() => null) as any, {} as any) |
| 39 | +); |
| 40 | +const start = doStart(); |
| 41 | + |
| 42 | +let container: DashboardContainer; |
| 43 | +let embeddable: ContactCardEmbeddable & ReferenceOrValueEmbeddable; |
| 44 | +let coreStart: CoreStart; |
| 45 | +beforeEach(async () => { |
| 46 | + coreStart = coreMock.createStart(); |
| 47 | + |
| 48 | + const containerOptions = { |
| 49 | + ExitFullScreenButton: () => null, |
| 50 | + SavedObjectFinder: () => null, |
| 51 | + application: {} as any, |
| 52 | + embeddable: start, |
| 53 | + inspector: {} as any, |
| 54 | + notifications: {} as any, |
| 55 | + overlays: coreStart.overlays, |
| 56 | + savedObjectMetaData: {} as any, |
| 57 | + uiActions: {} as any, |
| 58 | + }; |
| 59 | + |
| 60 | + container = new DashboardContainer(getSampleDashboardInput(), containerOptions); |
| 61 | + |
| 62 | + const contactCardEmbeddable = await container.addNewEmbeddable< |
| 63 | + ContactCardEmbeddableInput, |
| 64 | + ContactCardEmbeddableOutput, |
| 65 | + ContactCardEmbeddable |
| 66 | + >(CONTACT_CARD_EMBEDDABLE, { |
| 67 | + firstName: 'Kibanana', |
| 68 | + }); |
| 69 | + |
| 70 | + if (isErrorEmbeddable(contactCardEmbeddable)) { |
| 71 | + throw new Error('Failed to create embeddable'); |
| 72 | + } |
| 73 | + embeddable = embeddablePluginMock.mockRefOrValEmbeddable< |
| 74 | + ContactCardEmbeddable, |
| 75 | + ContactCardEmbeddableInput |
| 76 | + >(contactCardEmbeddable, { |
| 77 | + mockedByReferenceInput: { savedObjectId: 'testSavedObjectId', id: contactCardEmbeddable.id }, |
| 78 | + mockedByValueInput: { firstName: 'Kibanana', id: contactCardEmbeddable.id }, |
| 79 | + }); |
| 80 | + embeddable.updateInput({ viewMode: ViewMode.EDIT }); |
| 81 | +}); |
| 82 | + |
| 83 | +test('Unlink is compatible when embeddable on dashboard has reference type input', async () => { |
| 84 | + const action = new UnlinkFromLibraryAction(); |
| 85 | + embeddable.updateInput(await embeddable.getInputAsRefType()); |
| 86 | + expect(await action.isCompatible({ embeddable })).toBe(true); |
| 87 | +}); |
| 88 | + |
| 89 | +test('Unlink is not compatible when embeddable input is by value', async () => { |
| 90 | + const action = new UnlinkFromLibraryAction(); |
| 91 | + embeddable.updateInput(await embeddable.getInputAsValueType()); |
| 92 | + expect(await action.isCompatible({ embeddable })).toBe(false); |
| 93 | +}); |
| 94 | + |
| 95 | +test('Unlink is not compatible when view mode is set to view', async () => { |
| 96 | + const action = new UnlinkFromLibraryAction(); |
| 97 | + embeddable.updateInput(await embeddable.getInputAsRefType()); |
| 98 | + embeddable.updateInput({ viewMode: ViewMode.VIEW }); |
| 99 | + expect(await action.isCompatible({ embeddable })).toBe(false); |
| 100 | +}); |
| 101 | + |
| 102 | +test('Unlink is not compatible when embeddable is not in a dashboard container', async () => { |
| 103 | + let orphanContactCard = await container.addNewEmbeddable< |
| 104 | + ContactCardEmbeddableInput, |
| 105 | + ContactCardEmbeddableOutput, |
| 106 | + ContactCardEmbeddable |
| 107 | + >(CONTACT_CARD_EMBEDDABLE, { |
| 108 | + firstName: 'Orphan', |
| 109 | + }); |
| 110 | + orphanContactCard = embeddablePluginMock.mockRefOrValEmbeddable< |
| 111 | + ContactCardEmbeddable, |
| 112 | + ContactCardEmbeddableInput |
| 113 | + >(orphanContactCard, { |
| 114 | + mockedByReferenceInput: { savedObjectId: 'test', id: orphanContactCard.id }, |
| 115 | + mockedByValueInput: { firstName: 'Kibanana', id: orphanContactCard.id }, |
| 116 | + }); |
| 117 | + const action = new UnlinkFromLibraryAction(); |
| 118 | + expect(await action.isCompatible({ embeddable: orphanContactCard })).toBe(false); |
| 119 | +}); |
| 120 | + |
| 121 | +test('Unlink replaces embeddableId but retains panel count', async () => { |
| 122 | + const dashboard = embeddable.getRoot() as IContainer; |
| 123 | + const originalPanelCount = Object.keys(dashboard.getInput().panels).length; |
| 124 | + const originalPanelKeySet = new Set(Object.keys(dashboard.getInput().panels)); |
| 125 | + const action = new UnlinkFromLibraryAction(); |
| 126 | + await action.execute({ embeddable }); |
| 127 | + expect(Object.keys(container.getInput().panels).length).toEqual(originalPanelCount); |
| 128 | + |
| 129 | + const newPanelId = Object.keys(container.getInput().panels).find( |
| 130 | + (key) => !originalPanelKeySet.has(key) |
| 131 | + ); |
| 132 | + expect(newPanelId).toBeDefined(); |
| 133 | + const newPanel = container.getInput().panels[newPanelId!]; |
| 134 | + expect(newPanel.type).toEqual(embeddable.type); |
| 135 | +}); |
| 136 | + |
| 137 | +test('Unlink unwraps all attributes from savedObject', async () => { |
| 138 | + const complicatedAttributes = { |
| 139 | + attribute1: 'The best attribute', |
| 140 | + attribute2: 22, |
| 141 | + attribute3: ['array', 'of', 'strings'], |
| 142 | + attribute4: { nestedattribute: 'hello from the nest' }, |
| 143 | + }; |
| 144 | + |
| 145 | + embeddable = embeddablePluginMock.mockRefOrValEmbeddable<ContactCardEmbeddable>(embeddable, { |
| 146 | + mockedByReferenceInput: { savedObjectId: 'testSavedObjectId', id: embeddable.id }, |
| 147 | + mockedByValueInput: { attributes: complicatedAttributes, id: embeddable.id }, |
| 148 | + }); |
| 149 | + const dashboard = embeddable.getRoot() as IContainer; |
| 150 | + const originalPanelKeySet = new Set(Object.keys(dashboard.getInput().panels)); |
| 151 | + const action = new UnlinkFromLibraryAction(); |
| 152 | + await action.execute({ embeddable }); |
| 153 | + const newPanelId = Object.keys(container.getInput().panels).find( |
| 154 | + (key) => !originalPanelKeySet.has(key) |
| 155 | + ); |
| 156 | + expect(newPanelId).toBeDefined(); |
| 157 | + const newPanel = container.getInput().panels[newPanelId!]; |
| 158 | + expect(newPanel.type).toEqual(embeddable.type); |
| 159 | + expect(newPanel.explicitInput.attributes).toEqual(complicatedAttributes); |
| 160 | +}); |
0 commit comments