Skip to content

Commit

Permalink
add saved dashboard test
Browse files Browse the repository at this point in the history
Signed-off-by: Kawika Avilla <[email protected]>
  • Loading branch information
kavilla committed Jul 9, 2023
1 parent 2532263 commit ff6e3da
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { DashboardServices } from '../../../types';
import { SavedObjectDashboard } from '../../../saved_dashboards';
import { dashboardAppStateStub } from '../stubs';
import { createDashboardServicesMock } from '../mocks';
import { Dashboard } from '../../../dashboard';
import { convertToSerializedDashboard } from '../../../saved_dashboards/_saved_dashboard';

jest.mock('../create_dashboard_app_state');
jest.mock('../create_dashboard_container.tsx');
Expand All @@ -28,7 +30,6 @@ describe('useDashboardAppAndGlobalState', () => {
getState: stateContainerGetStateMock,
state$: new Observable(),
transitions: {
updateVisState: jest.fn(),
set: jest.fn(),
},
};
Expand All @@ -49,6 +50,8 @@ describe('useDashboardAppAndGlobalState', () => {
optionsJSON: JSON.stringify(dashboardAppStateStub.options),
},
} as unknown) as SavedObjectDashboard;
const dashboard = new Dashboard(convertToSerializedDashboard(savedDashboardInstance));

let mockServices: jest.Mocked<DashboardServices>;

beforeEach(() => {
Expand Down Expand Up @@ -77,6 +80,7 @@ describe('useDashboardAppAndGlobalState', () => {
services: mockServices,
eventEmitter,
savedDashboardInstance,
dashboard,
})
);

Expand Down Expand Up @@ -106,6 +110,7 @@ describe('useDashboardAppAndGlobalState', () => {
services: mockServices,
eventEmitter,
savedDashboardInstance,
dashboard,
})
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ describe('useEditorUpdates', () => {
let appState: DashboardAppStateContainer;
let savedDashboardInstance: SavedObjectDashboard;
let dashboard: Dashboard;
let timeRange: any;
let mockFilters: any;

beforeEach(() => {
unsubscribeStateUpdatesMock = jest.fn();
Expand All @@ -98,15 +96,6 @@ describe('useEditorUpdates', () => {
},
} as unknown) as SavedObjectDashboard;
dashboard = new Dashboard(convertToSerializedDashboard(savedDashboardInstance));
timeRange = {
from: 'now-15m',
to: 'now',
};
mockFilters = ['mockFilters'];
const mockQuery = {
query: '',
language: 'kuery',
};
});

test('should set up current app state and render the editor', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { renderHook } from '@testing-library/react-hooks';
import { EventEmitter } from 'events';

import { useSavedDashboardInstance } from './use_saved_dashboard_instance';
import { DashboardServices } from '../../../types';
import { SavedObjectDashboard } from '../../../saved_dashboards';
import { dashboardAppStateStub } from '../stubs';
import { createDashboardServicesMock } from '../mocks';
import { Dashboard } from '../../../dashboard';
import { convertToSerializedDashboard } from '../../../saved_dashboards/_saved_dashboard';

// TODO: needs more setState tests on update
describe('useSavedDashboardInstance', () => {
const eventEmitter = new EventEmitter();
let mockServices: jest.Mocked<DashboardServices>;
let isChromeVisible: boolean | undefined;
let dashboardIdFromUrl: string | undefined;
let savedDashboardInstance: SavedObjectDashboard;
let dashboard: Dashboard;

beforeEach(() => {
mockServices = createDashboardServicesMock();
isChromeVisible = true;
dashboardIdFromUrl = '1234';

savedDashboardInstance = ({
...dashboardAppStateStub,
...{
getQuery: () => dashboardAppStateStub.query,
getFilters: () => dashboardAppStateStub.filters,
optionsJSON: JSON.stringify(dashboardAppStateStub.options),
},
} as unknown) as SavedObjectDashboard;
dashboard = new Dashboard(convertToSerializedDashboard(savedDashboardInstance));
});

describe('should not set saved dashboard instance', () => {
test('if id ref is blank and dashboardIdFromUrl not updated', () => {
dashboardIdFromUrl = undefined;

const { result } = renderHook(() =>
useSavedDashboardInstance({
services: mockServices,
eventEmitter,
isChromeVisible,
dashboardIdFromUrl,
})
);

expect(result.current).toEqual({});
});

test('if chrome is not visible', () => {
isChromeVisible = undefined;

const { result } = renderHook(() =>
useSavedDashboardInstance({
services: mockServices,
eventEmitter,
isChromeVisible,
dashboardIdFromUrl,
})
);

expect(result.current).toEqual({});
});
});
});

0 comments on commit ff6e3da

Please sign in to comment.