Skip to content

Commit aaa8c22

Browse files
committed
unit test
1 parent ea13879 commit aaa8c22

File tree

2 files changed

+145
-15
lines changed

2 files changed

+145
-15
lines changed

x-pack/plugins/security_solution/public/network/components/embeddables/embedded_map.test.tsx

+139-10
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,169 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { shallow } from 'enzyme';
7+
import { mount, ReactWrapper, shallow } from 'enzyme';
88
import React from 'react';
9+
import * as redux from 'react-redux';
10+
import { act } from 'react-dom/test-utils';
911

1012
import '../../../common/mock/match_media';
1113
import { useIndexPatterns } from '../../../common/hooks/use_index_patterns';
1214
import { TestProviders } from '../../../common/mock';
1315

1416
import { EmbeddedMapComponent } from './embedded_map';
17+
import { createEmbeddable } from './embedded_map_helpers';
1518

1619
const mockUseIndexPatterns = useIndexPatterns as jest.Mock;
1720
jest.mock('../../../common/hooks/use_index_patterns');
1821
mockUseIndexPatterns.mockImplementation(() => [true, []]);
1922

2023
jest.mock('../../../common/lib/kibana');
24+
jest.mock('./embedded_map_helpers', () => ({
25+
createEmbeddable: jest.fn(),
26+
}));
27+
jest.mock('../../../common/lib/kibana', () => {
28+
return {
29+
useKibana: jest.fn().mockReturnValue({
30+
services: {
31+
embeddable: {
32+
EmbeddablePanel: jest.fn(() => <div data-test-subj="EmbeddablePanel" />),
33+
},
34+
docLinks: { ELASTIC_WEBSITE_URL: 'ELASTIC_WEBSITE_URL' },
35+
},
36+
}),
37+
};
38+
});
39+
40+
jest.mock('./index_patterns_missing_prompt', () => {
41+
return {
42+
IndexPatternsMissingPrompt: jest.fn(() => <div data-test-subj="IndexPatternsMissingPrompt" />),
43+
};
44+
});
2145

2246
describe('EmbeddedMapComponent', () => {
23-
let setQuery: jest.Mock;
47+
const setQuery: jest.Mock = jest.fn();
48+
const mockSelector = {
49+
kibanaIndexPatterns: [
50+
{ id: '6f1eeb50-023d-11eb-bcb6-6ba0578012a9', title: 'filebeat-*' },
51+
{ id: '28995490-023d-11eb-bcb6-6ba0578012a9', title: 'auditbeat-*' },
52+
],
53+
sourcererScope: { selectedPatterns: ['filebeat-*', 'packetbeat-*'] },
54+
};
55+
const mockCreateEmbeddable = {
56+
destroyed: false,
57+
enhancements: { dynamicActions: {} },
58+
getActionContext: jest.fn(),
59+
getFilterActions: jest.fn(),
60+
id: '70969ddc-4d01-4048-8073-4ea63d595638',
61+
input: {
62+
viewMode: 'view',
63+
title: 'Source -> Destination Point-to-Point Map',
64+
id: '70969ddc-4d01-4048-8073-4ea63d595638',
65+
filters: Array(0),
66+
hidePanelTitles: true,
67+
},
68+
input$: {},
69+
isContainer: false,
70+
output: {},
71+
output$: {},
72+
parent: undefined,
73+
parentSubscription: undefined,
74+
renderComplete: {},
75+
runtimeId: 1,
76+
reload: jest.fn(),
77+
setLayerList: jest.fn(),
78+
setEventHandlers: jest.fn(),
79+
setRenderTooltipContent: jest.fn(),
80+
type: 'map',
81+
updateInput: jest.fn(),
82+
};
83+
const testProps = {
84+
endDate: '2019-08-28T05:50:57.877Z',
85+
filters: [],
86+
query: { query: '', language: 'kuery' },
87+
setQuery,
88+
startDate: '2019-08-28T05:50:47.877Z',
89+
};
2490

2591
beforeEach(() => {
26-
setQuery = jest.fn();
92+
setQuery.mockClear();
2793
});
2894

2995
test('renders correctly against snapshot', () => {
3096
const wrapper = shallow(
3197
<TestProviders>
32-
<EmbeddedMapComponent
33-
endDate="2019-08-28T05:50:57.877Z"
34-
filters={[]}
35-
query={{ query: '', language: 'kuery' }}
36-
setQuery={setQuery}
37-
startDate="2019-08-28T05:50:47.877Z"
38-
/>
98+
<EmbeddedMapComponent {...testProps} />
3999
</TestProviders>
40100
);
41101
expect(wrapper.find('EmbeddedMapComponent')).toMatchSnapshot();
42102
});
103+
104+
test('renders services.embeddable.EmbeddablePanel', async () => {
105+
const spy = jest.spyOn(redux, 'useSelector');
106+
spy.mockReturnValue(mockSelector);
107+
108+
(createEmbeddable as jest.Mock).mockResolvedValue(mockCreateEmbeddable);
109+
110+
let wrapper: ReactWrapper;
111+
await act(async () => {
112+
wrapper = mount(
113+
<TestProviders>
114+
<EmbeddedMapComponent {...testProps} />
115+
</TestProviders>
116+
);
117+
});
118+
119+
wrapper!.update();
120+
121+
expect(wrapper!.find('[data-test-subj="EmbeddablePanel"]').exists()).toEqual(true);
122+
expect(wrapper!.find('[data-test-subj="IndexPatternsMissingPrompt"]').exists()).toEqual(false);
123+
expect(wrapper!.find('[data-test-subj="loading-panel"]').exists()).toEqual(false);
124+
});
125+
126+
test('renders IndexPatternsMissingPrompt', async () => {
127+
const spy = jest.spyOn(redux, 'useSelector');
128+
spy.mockReturnValue({
129+
...mockSelector,
130+
kibanaIndexPatterns: [],
131+
});
132+
133+
(createEmbeddable as jest.Mock).mockResolvedValue(mockCreateEmbeddable);
134+
135+
let wrapper: ReactWrapper;
136+
await act(async () => {
137+
wrapper = mount(
138+
<TestProviders>
139+
<EmbeddedMapComponent {...testProps} />
140+
</TestProviders>
141+
);
142+
});
143+
144+
wrapper!.update();
145+
146+
expect(wrapper!.find('[data-test-subj="EmbeddablePanel"]').exists()).toEqual(false);
147+
expect(wrapper!.find('[data-test-subj="IndexPatternsMissingPrompt"]').exists()).toEqual(true);
148+
expect(wrapper!.find('[data-test-subj="loading-panel"]').exists()).toEqual(false);
149+
});
150+
151+
test('renders Loader', async () => {
152+
const spy = jest.spyOn(redux, 'useSelector');
153+
spy.mockReturnValue(mockSelector);
154+
155+
(createEmbeddable as jest.Mock).mockResolvedValue(null);
156+
157+
let wrapper: ReactWrapper;
158+
await act(async () => {
159+
wrapper = mount(
160+
<TestProviders>
161+
<EmbeddedMapComponent {...testProps} />
162+
</TestProviders>
163+
);
164+
});
165+
166+
wrapper!.update();
167+
168+
expect(wrapper!.find('[data-test-subj="EmbeddablePanel"]').exists()).toEqual(false);
169+
expect(wrapper!.find('[data-test-subj="IndexPatternsMissingPrompt"]').exists()).toEqual(false);
170+
expect(wrapper!.find('[data-test-subj="loading-panel"]').exists()).toEqual(true);
171+
});
43172
});

x-pack/plugins/security_solution/public/network/components/embeddables/embedded_map.tsx

+6-5
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ export const EmbeddedMapComponent = ({
118118
sourcererScope.selectedPatterns.includes(kip.title)
119119
);
120120
if (!deepEqual(newIndexPatterns, prevMapIndexPatterns)) {
121+
if (newIndexPatterns.length === 0) {
122+
setIsError(true);
123+
}
121124
return newIndexPatterns;
122125
}
123126
return prevMapIndexPatterns;
@@ -143,7 +146,6 @@ export const EmbeddedMapComponent = ({
143146
if (isSubscribed) {
144147
if (mapIndexPatterns.length === 0) {
145148
setIsIndexError(true);
146-
return;
147149
} else {
148150
setEmbeddable(embeddableObject);
149151
setIsIndexError(false);
@@ -156,7 +158,6 @@ export const EmbeddedMapComponent = ({
156158
}
157159
}
158160
}
159-
160161
if (embeddable == null && sourcererScope.selectedPatterns.length > 0) {
161162
setupEmbeddable();
162163
}
@@ -237,10 +238,10 @@ export const EmbeddedMapComponent = ({
237238
</InPortal>
238239

239240
<EmbeddableMap maintainRatio={!isIndexError}>
240-
{embeddable != null && !isIndexError ? (
241-
<services.embeddable.EmbeddablePanel embeddable={embeddable} />
242-
) : isIndexError ? (
241+
{isIndexError ? (
243242
<IndexPatternsMissingPrompt data-test-subj="missing-prompt" />
243+
) : embeddable != null ? (
244+
<services.embeddable.EmbeddablePanel embeddable={embeddable} />
244245
) : (
245246
<Loader data-test-subj="loading-panel" overlay size="xl" />
246247
)}

0 commit comments

Comments
 (0)