Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down