diff --git a/src/plugins/dashboard/public/services/dashboard_content_management/lib/load_dashboard_state.test.ts b/src/plugins/dashboard/public/services/dashboard_content_management/lib/load_dashboard_state.test.ts new file mode 100644 index 0000000000000..b2289ac5ca975 --- /dev/null +++ b/src/plugins/dashboard/public/services/dashboard_content_management/lib/load_dashboard_state.test.ts @@ -0,0 +1,81 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { registry } from '../../plugin_services.stub'; +import { pluginServices } from '../../plugin_services'; +import { getSampleDashboardInput } from '../../../mocks'; +import { loadDashboardState } from './load_dashboard_state'; +import { dashboardContentManagementCache } from '../dashboard_content_management_service'; + +pluginServices.setRegistry(registry.start({})); +const { data, embeddable, contentManagement, savedObjectsTagging } = pluginServices.getServices(); + +const allServices = { + data, + embeddable, + contentManagement, + savedObjectsTagging, +}; + +describe('Load dashboard state', () => { + it('should return cached result if available', async () => { + dashboardContentManagementCache.fetchDashboard = jest.fn().mockImplementation((id: string) => { + return { + item: { + id, + version: 1, + references: [], + type: 'dashboard', + attributes: { + kibanaSavedObjectMeta: { searchSourceJSON: '' }, + title: 'Test dashboard', + }, + }, + meta: {}, + }; + }); + dashboardContentManagementCache.addDashboard = jest.fn(); + contentManagement.client.get = jest.fn(); + + const { id } = getSampleDashboardInput(); + const result = await loadDashboardState({ + id, + ...allServices, + }); + expect(dashboardContentManagementCache.fetchDashboard).toBeCalled(); + expect(dashboardContentManagementCache.addDashboard).not.toBeCalled(); + expect(contentManagement.client.get).not.toBeCalled(); + expect(result).toMatchObject({ + dashboardId: id, + dashboardFound: true, + dashboardInput: { + title: 'Test dashboard', + }, + }); + }); + + it('should not add to cache for alias redirect result', async () => { + dashboardContentManagementCache.fetchDashboard = jest.fn().mockImplementation(() => undefined); + dashboardContentManagementCache.addDashboard = jest.fn(); + contentManagement.client.get = jest.fn().mockImplementation(({ id }) => { + return Promise.resolve({ + item: { id }, + meta: { + outcome: 'aliasMatch', + }, + }); + }); + const { id } = getSampleDashboardInput(); + await loadDashboardState({ + id, + ...allServices, + }); + expect(dashboardContentManagementCache.fetchDashboard).toBeCalled(); + expect(dashboardContentManagementCache.addDashboard).not.toBeCalled(); + }); +}); diff --git a/src/plugins/dashboard/public/services/dashboard_content_management/lib/load_dashboard_state.ts b/src/plugins/dashboard/public/services/dashboard_content_management/lib/load_dashboard_state.ts index c022e05d91d3d..5b748c740cea4 100644 --- a/src/plugins/dashboard/public/services/dashboard_content_management/lib/load_dashboard_state.ts +++ b/src/plugins/dashboard/public/services/dashboard_content_management/lib/load_dashboard_state.ts @@ -63,8 +63,8 @@ export const loadDashboardState = async ({ /** * Load the saved object from Content Management */ - let rawDashboardContent; - let resolveMeta; + let rawDashboardContent: DashboardCrudTypes['GetOut']['item']; + let resolveMeta: DashboardCrudTypes['GetOut']['meta']; const cachedDashboard = dashboardContentManagementCache.fetchDashboard(id); if (cachedDashboard) { @@ -81,8 +81,15 @@ export const loadDashboardState = async ({ throw new SavedObjectNotFound(DASHBOARD_CONTENT_ID, id); }); - dashboardContentManagementCache.addDashboard(result); ({ item: rawDashboardContent, meta: resolveMeta } = result); + const { outcome: loadOutcome } = resolveMeta; + if (loadOutcome !== 'aliasMatch') { + /** + * Only add the dashboard to the cache if it does not require a redirect - otherwise, the meta + * alias info gets cached and prevents the dashboard contents from being updated + */ + dashboardContentManagementCache.addDashboard(result); + } } if (!rawDashboardContent || !rawDashboardContent.version) {