Skip to content

Commit f79e0eb

Browse files
used coremock in application.test.tsx and plugin.test.tsx
Signed-off-by: [email protected] <[email protected]>
1 parent 9cdb9da commit f79e0eb

File tree

2 files changed

+34
-44
lines changed

2 files changed

+34
-44
lines changed

public/application.test.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import * as ReactDOM from 'react-dom';
77
import { renderApp } from './application';
8+
import { coreMock } from '../../../src/core/public/mocks';
89

910
jest.mock('react-dom', () => {
1011
const actualReactDOM = jest.requireActual('react-dom');
@@ -14,8 +15,9 @@ jest.mock('react-dom', () => {
1415
unmountComponentAtNode: jest.fn(),
1516
};
1617
});
18+
1719
describe('renderApp', () => {
18-
const coreMock = {};
20+
const coreMockStart = coreMock.createStart();
1921
const depsStartMock = {};
2022
const paramsMock = { element: document.createElement('div') };
2123
const dataSourceManagementMock = {};
@@ -25,7 +27,7 @@ describe('renderApp', () => {
2527
});
2628

2729
it('should unmount the component when the returned function is called', () => {
28-
const unmount = renderApp(coreMock, depsStartMock, paramsMock, dataSourceManagementMock);
30+
const unmount = renderApp(coreMockStart, depsStartMock, paramsMock, dataSourceManagementMock);
2931
unmount();
3032

3133
expect(ReactDOM.unmountComponentAtNode).toHaveBeenCalledWith(paramsMock.element);

public/plugin.test.tsx

+30-42
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import { CoreSetup, CoreStart } from '../../../src/core/public';
77
import { QueryInsightsDashboardsPlugin } from './plugin';
88
import { PLUGIN_NAME } from '../common';
9+
import { renderApp } from './application';
10+
import { coreMock } from '../../../src/core/public/mocks';
911

1012
jest.mock('./application', () => ({
1113
renderApp: jest.fn(),
@@ -16,34 +18,13 @@ describe('QueryInsightsDashboardsPlugin', () => {
1618
let coreSetupMock: jest.Mocked<CoreSetup>;
1719
let coreStartMock: jest.Mocked<CoreStart>;
1820
let registerMock: jest.Mock;
19-
let addNavLinksMock: jest.Mock;
2021

2122
beforeEach(() => {
22-
coreSetupMock = ({
23-
application: {
24-
register: jest.fn(),
25-
},
26-
chrome: {
27-
navGroup: {
28-
addNavLinksToGroup: jest.fn(),
29-
},
30-
},
31-
getStartServices: jest.fn().mockResolvedValue([coreStartMock, {}]),
32-
} as unknown) as jest.Mocked<CoreSetup>;
33-
34-
coreStartMock = ({
35-
http: {
36-
get: jest.fn(),
37-
put: jest.fn(),
38-
},
39-
uiSettings: {
40-
get: jest.fn().mockReturnValue(false),
41-
},
42-
} as unknown) as jest.Mocked<CoreStart>;
23+
coreSetupMock = coreMock.createSetup();
24+
coreStartMock = coreMock.createStart();
4325

4426
plugin = new QueryInsightsDashboardsPlugin();
4527
registerMock = coreSetupMock.application.register;
46-
addNavLinksMock = coreSetupMock.chrome.navGroup.addNavLinksToGroup;
4728
});
4829

4930
it('should register the application in setup', () => {
@@ -58,33 +39,40 @@ describe('QueryInsightsDashboardsPlugin', () => {
5839
label: expect.any(String),
5940
order: expect.any(Number),
6041
}),
61-
order: expect.any(Number),
6242
mount: expect.any(Function),
43+
order: expect.any(Number),
6344
description: expect.any(String),
6445
})
6546
);
6647
});
6748

68-
it('should add the navigation link to nav group', () => {
49+
it('should mount the application correctly', async () => {
6950
plugin.setup(coreSetupMock, {} as any);
7051

71-
expect(addNavLinksMock).toHaveBeenCalledWith(
72-
expect.objectContaining({
73-
id: expect.any(String),
74-
description: expect.any(String),
75-
}),
76-
expect.arrayContaining([
77-
expect.objectContaining({
78-
id: 'query-insights-dashboards',
79-
order: expect.any(Number),
80-
category: expect.objectContaining({
81-
euiIconType: expect.any(String),
82-
id: 'performance', // Adjusted to match received data
83-
label: expect.any(String),
84-
order: expect.any(Number),
85-
}),
86-
}),
87-
])
52+
const appRegistration = registerMock.mock.calls[0][0];
53+
expect(appRegistration).toBeDefined();
54+
55+
const paramsMock = { element: document.createElement('div') };
56+
const mountFunction = appRegistration.mount;
57+
58+
await mountFunction(paramsMock);
59+
60+
const depsMock = { dataSourceManagement: undefined };
61+
coreSetupMock.getStartServices.mockResolvedValue([coreStartMock, depsMock]);
62+
63+
await mountFunction(paramsMock);
64+
65+
expect(renderApp).toHaveBeenCalledWith(
66+
coreStartMock,
67+
depsMock,
68+
expect.objectContaining({ element: expect.any(HTMLElement) }),
69+
depsMock.dataSourceManagement
8870
);
8971
});
72+
73+
it('should return empty start and stop methods', () => {
74+
// Ensures `start` and `stop` do not introduce unwanted behavior
75+
expect(plugin.start(coreStartMock)).toEqual({});
76+
expect(plugin.stop()).toBeUndefined();
77+
});
9078
});

0 commit comments

Comments
 (0)