/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. * * Any modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ /* * Licensed to Elasticsearch B.V. under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch B.V. licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import { isErrorEmbeddable, ReferenceOrValueEmbeddable, ErrorEmbeddable, IContainer, ViewMode, } from '../../../../embeddable/public'; import { CONTACT_CARD_EMBEDDABLE, ContactCardEmbeddableFactory, ContactCardEmbeddable, ContactCardEmbeddableInput, ContactCardEmbeddableOutput, } from '../../../../embeddable/public/lib/test_samples'; import { embeddablePluginMock } from '../../../../embeddable/public/mocks'; import { DashboardContainer } from '../embeddable'; import { getSampleDashboardInput } from '../test_helpers'; import { coreMock } from '../../../../../core/public/mocks'; import { CoreStart } from 'opensearch-dashboards/public'; import { LibraryNotificationAction } from '.'; const { setup, doStart } = embeddablePluginMock.createInstance(); setup.registerEmbeddableFactory( CONTACT_CARD_EMBEDDABLE, new ContactCardEmbeddableFactory((() => null) as any, {} as any) ); const start = doStart(); let container: DashboardContainer; let embeddable: ContactCardEmbeddable & ReferenceOrValueEmbeddable; let coreStart: CoreStart; beforeEach(async () => { coreStart = coreMock.createStart(); const containerOptions = { ExitFullScreenButton: () => null, SavedObjectFinder: () => null, application: {} as any, embeddable: start, inspector: {} as any, notifications: {} as any, overlays: coreStart.overlays, savedObjectMetaData: {} as any, uiActions: {} as any, }; container = new DashboardContainer(getSampleDashboardInput(), containerOptions); const contactCardEmbeddable = await container.addNewEmbeddable< ContactCardEmbeddableInput, ContactCardEmbeddableOutput, ContactCardEmbeddable >(CONTACT_CARD_EMBEDDABLE, { firstName: 'opensearchDashboards', }); if (isErrorEmbeddable(contactCardEmbeddable)) { throw new Error('Failed to create embeddable'); } embeddable = embeddablePluginMock.mockRefOrValEmbeddable< ContactCardEmbeddable, ContactCardEmbeddableInput >(contactCardEmbeddable, { mockedByReferenceInput: { savedObjectId: 'testSavedObjectId', id: contactCardEmbeddable.id }, mockedByValueInput: { firstName: 'opensearchDashboards', id: contactCardEmbeddable.id }, }); embeddable.updateInput({ viewMode: ViewMode.EDIT }); }); test('Notification is incompatible with Error Embeddables', async () => { const action = new LibraryNotificationAction(); const errorEmbeddable = new ErrorEmbeddable( 'Wow what an awful error', { id: ' 404' }, embeddable.getRoot() as IContainer ); expect(await action.isCompatible({ embeddable: errorEmbeddable })).toBe(false); }); test('Notification is shown when embeddable on dashboard has reference type input', async () => { const action = new LibraryNotificationAction(); embeddable.updateInput(await embeddable.getInputAsRefType()); expect(await action.isCompatible({ embeddable })).toBe(true); }); test('Notification is not shown when embeddable input is by value', async () => { const action = new LibraryNotificationAction(); embeddable.updateInput(await embeddable.getInputAsValueType()); expect(await action.isCompatible({ embeddable })).toBe(false); }); test('Notification is not shown when view mode is set to view', async () => { const action = new LibraryNotificationAction(); embeddable.updateInput(await embeddable.getInputAsRefType()); embeddable.updateInput({ viewMode: ViewMode.VIEW }); expect(await action.isCompatible({ embeddable })).toBe(false); });