Skip to content

Commit cd8c708

Browse files
authored
Kibana app migration: Centralize home dependencies (#48618)
1 parent 88f0eba commit cd8c708

19 files changed

+233
-169
lines changed

src/legacy/core_plugins/kibana/public/home/components/add_data.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import React from 'react';
2121
import PropTypes from 'prop-types';
2222
import classNames from 'classnames';
2323
import { injectI18n, FormattedMessage } from '@kbn/i18n/react';
24-
import chrome from 'ui/chrome';
24+
import { getServices } from '../kibana_services';
2525

2626
import {
2727
EuiButton,
@@ -38,10 +38,9 @@ import {
3838
EuiFlexGrid,
3939
} from '@elastic/eui';
4040

41-
/* istanbul ignore next */
42-
const basePath = chrome.getBasePath();
4341

4442
const AddDataUi = ({ apmUiEnabled, isNewKibanaInstance, intl, mlEnabled }) => {
43+
const basePath = getServices().getBasePath();
4544
const renderCards = () => {
4645
const apmData = {
4746
title: intl.formatMessage({

src/legacy/core_plugins/kibana/public/home/components/add_data.test.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,20 @@
2020
import React from 'react';
2121
import { AddData } from './add_data';
2222
import { shallowWithIntl } from 'test_utils/enzyme_helpers';
23-
import chrome from 'ui/chrome';
23+
import { getServices } from '../kibana_services';
2424

25-
jest.mock(
26-
'ui/chrome',
27-
() => ({
25+
jest.mock('../kibana_services', () =>{
26+
const mock = {
2827
getBasePath: jest.fn(() => 'path'),
29-
}),
30-
{ virtual: true }
31-
);
28+
};
29+
return {
30+
getServices: () => mock,
31+
};
32+
});
33+
34+
beforeEach(() => {
35+
jest.clearAllMocks();
36+
});
3237

3338
test('render', () => {
3439
const component = shallowWithIntl(<AddData.WrappedComponent
@@ -37,7 +42,7 @@ test('render', () => {
3742
isNewKibanaInstance={false}
3843
/>);
3944
expect(component).toMatchSnapshot(); // eslint-disable-line
40-
expect(chrome.getBasePath).toHaveBeenCalledTimes(1);
45+
expect(getServices().getBasePath).toHaveBeenCalledTimes(1);
4146
});
4247

4348
test('mlEnabled', () => {
@@ -47,7 +52,7 @@ test('mlEnabled', () => {
4752
isNewKibanaInstance={false}
4853
/>);
4954
expect(component).toMatchSnapshot(); // eslint-disable-line
50-
expect(chrome.getBasePath).toHaveBeenCalledTimes(1);
55+
expect(getServices().getBasePath).toHaveBeenCalledTimes(1);
5156
});
5257

5358
test('apmUiEnabled', () => {
@@ -57,7 +62,7 @@ test('apmUiEnabled', () => {
5762
isNewKibanaInstance={false}
5863
/>);
5964
expect(component).toMatchSnapshot(); // eslint-disable-line
60-
expect(chrome.getBasePath).toHaveBeenCalledTimes(1);
65+
expect(getServices().getBasePath).toHaveBeenCalledTimes(1);
6166
});
6267

6368
test('isNewKibanaInstance', () => {
@@ -67,5 +72,5 @@ test('isNewKibanaInstance', () => {
6772
isNewKibanaInstance={true}
6873
/>);
6974
expect(component).toMatchSnapshot(); // eslint-disable-line
70-
expect(chrome.getBasePath).toHaveBeenCalledTimes(1);
75+
expect(getServices().getBasePath).toHaveBeenCalledTimes(1);
7176
});

src/legacy/core_plugins/kibana/public/home/components/home.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import PropTypes from 'prop-types';
2222
import { Synopsis } from './synopsis';
2323
import { AddData } from './add_data';
2424
import { FormattedMessage } from '@kbn/i18n/react';
25-
import chrome from 'ui/chrome';
2625

2726
import {
2827
EuiButton,
@@ -40,14 +39,18 @@ import {
4039

4140
import { Welcome } from './welcome';
4241
import { FeatureCatalogueCategory } from 'ui/registry/feature_catalogue';
42+
import { getServices } from '../kibana_services';
4343

4444
const KEY_ENABLE_WELCOME = 'home:welcome:show';
4545

4646
export class Home extends Component {
4747
constructor(props) {
4848
super(props);
4949

50-
const isWelcomeEnabled = !(chrome.getInjected('disableWelcomeScreen') || props.localStorage.getItem(KEY_ENABLE_WELCOME) === 'false');
50+
const isWelcomeEnabled = !(
51+
getServices().getInjected('disableWelcomeScreen') ||
52+
props.localStorage.getItem(KEY_ENABLE_WELCOME) === 'false'
53+
);
5154

5255
this.state = {
5356
// If welcome is enabled, we wait for loading to complete

src/legacy/core_plugins/kibana/public/home/components/home.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ import { shallow } from 'enzyme';
2525
import { Home } from './home';
2626
import { FeatureCatalogueCategory } from 'ui/registry/feature_catalogue';
2727

28+
jest.mock('../kibana_services', () =>({
29+
getServices: () => ({
30+
getBasePath: () => 'path',
31+
getInjected: () => ''
32+
})
33+
}));
34+
2835
describe('home', () => {
2936
let defaultProps;
3037

src/legacy/core_plugins/kibana/public/home/components/home.test.mocks.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
* under the License.
1818
*/
1919

20-
import { notificationServiceMock, overlayServiceMock } from '../../../../../../core/public/mocks';
20+
import {
21+
notificationServiceMock,
22+
overlayServiceMock,
23+
httpServiceMock,
24+
injectedMetadataServiceMock,
25+
} from '../../../../../../core/public/mocks';
2126

2227
jest.doMock('ui/new_platform', () => {
2328
return {
@@ -29,22 +34,9 @@ jest.doMock('ui/new_platform', () => {
2934
npStart: {
3035
core: {
3136
overlays: overlayServiceMock.createStartContract(),
37+
http: httpServiceMock.createStartContract({ basePath: 'path' }),
38+
injectedMetadata: injectedMetadataServiceMock.createStartContract(),
3239
},
3340
},
3441
};
3542
});
36-
37-
jest.doMock(
38-
'ui/chrome',
39-
() => ({
40-
getBasePath: jest.fn(() => 'path'),
41-
getInjected: jest.fn(() => ''),
42-
}),
43-
{ virtual: true }
44-
);
45-
46-
jest.doMock('ui/capabilities', () => ({
47-
catalogue: {},
48-
management: {},
49-
navLinks: {},
50-
}));

src/legacy/core_plugins/kibana/public/home/components/home_app.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,32 @@ import { Tutorial } from './tutorial/tutorial';
2626
import {
2727
HashRouter as Router,
2828
Switch,
29-
Route
29+
Route,
3030
} from 'react-router-dom';
3131
import { getTutorial } from '../load_tutorials';
3232
import { replaceTemplateStrings } from './tutorial/replace_template_strings';
33-
import { telemetryOptInProvider, shouldShowTelemetryOptIn } from '../kibana_services';
34-
import chrome from 'ui/chrome';
33+
import {
34+
getServices
35+
} from '../kibana_services';
3536

3637
export function HomeApp({ directories }) {
37-
const isCloudEnabled = chrome.getInjected('isCloudEnabled', false);
38-
const apmUiEnabled = chrome.getInjected('apmUiEnabled', true);
39-
const mlEnabled = chrome.getInjected('mlEnabled', false);
40-
const savedObjectsClient = chrome.getSavedObjectsClient();
38+
const {
39+
telemetryOptInProvider,
40+
shouldShowTelemetryOptIn,
41+
getInjected,
42+
savedObjectsClient,
43+
getBasePath,
44+
addBasePath,
45+
} = getServices();
46+
47+
const isCloudEnabled = getInjected('isCloudEnabled', false);
48+
const apmUiEnabled = getInjected('apmUiEnabled', true);
49+
const mlEnabled = getInjected('mlEnabled', false);
4150

4251
const renderTutorialDirectory = (props) => {
4352
return (
4453
<TutorialDirectory
45-
addBasePath={chrome.addBasePath}
54+
addBasePath={addBasePath}
4655
openTab={props.match.params.tab}
4756
isCloudEnabled={isCloudEnabled}
4857
/>
@@ -52,7 +61,7 @@ export function HomeApp({ directories }) {
5261
const renderTutorial = (props) => {
5362
return (
5463
<Tutorial
55-
addBasePath={chrome.addBasePath}
64+
addBasePath={addBasePath}
5665
isCloudEnabled={isCloudEnabled}
5766
getTutorial={getTutorial}
5867
replaceTemplateStrings={replaceTemplateStrings}
@@ -77,21 +86,21 @@ export function HomeApp({ directories }) {
7786
path="/home/feature_directory"
7887
>
7988
<FeatureDirectory
80-
addBasePath={chrome.addBasePath}
89+
addBasePath={addBasePath}
8190
directories={directories}
8291
/>
8392
</Route>
8493
<Route
8594
path="/home"
8695
>
8796
<Home
88-
addBasePath={chrome.addBasePath}
97+
addBasePath={addBasePath}
8998
directories={directories}
9099
apmUiEnabled={apmUiEnabled}
91100
mlEnabled={mlEnabled}
92101
find={savedObjectsClient.find}
93102
localStorage={localStorage}
94-
urlBasePath={chrome.getBasePath()}
103+
urlBasePath={getBasePath()}
95104
shouldShowTelemetryOptIn={shouldShowTelemetryOptIn}
96105
setOptIn={telemetryOptInProvider.setOptIn}
97106
fetchTelemetry={telemetryOptInProvider.fetchExample}
@@ -111,6 +120,6 @@ HomeApp.propTypes = {
111120
icon: PropTypes.string.isRequired,
112121
path: PropTypes.string.isRequired,
113122
showOnHomePage: PropTypes.bool.isRequired,
114-
category: PropTypes.string.isRequired
123+
category: PropTypes.string.isRequired,
115124
})),
116125
};

src/legacy/core_plugins/kibana/public/home/components/sample_data_set_cards.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
UNINSTALLED_STATUS,
3232
} from './sample_data_set_card';
3333

34-
import { toastNotifications } from 'ui/notify';
34+
import { getServices } from '../kibana_services';
3535

3636
import {
3737
listSampleDataSets,
@@ -40,15 +40,14 @@ import {
4040
} from '../sample_data_client';
4141

4242
import { i18n } from '@kbn/i18n';
43-
import chrome from 'ui/chrome';
44-
45-
const IS_DARK_THEME = chrome.getUiSettingsClient().get('theme:darkMode');
4643

4744
export class SampleDataSetCards extends React.Component {
4845

4946
constructor(props) {
5047
super(props);
5148

49+
this.toastNotifications = getServices().toastNotifications;
50+
5251
this.state = {
5352
sampleDataSets: [],
5453
processingStatus: {},
@@ -70,7 +69,7 @@ export class SampleDataSetCards extends React.Component {
7069
try {
7170
sampleDataSets = await listSampleDataSets();
7271
} catch (fetchError) {
73-
toastNotifications.addDanger({
72+
this.toastNotifications.addDanger({
7473
title: i18n.translate('kbn.home.sampleDataSet.unableToLoadListErrorMessage', {
7574
defaultMessage: 'Unable to load sample data sets list' }
7675
),
@@ -109,7 +108,7 @@ export class SampleDataSetCards extends React.Component {
109108
processingStatus: { ...prevState.processingStatus, [id]: false }
110109
}));
111110
}
112-
toastNotifications.addDanger({
111+
this.toastNotifications.addDanger({
113112
title: i18n.translate('kbn.home.sampleDataSet.unableToInstallErrorMessage', {
114113
defaultMessage: 'Unable to install sample data set: {name}', values: { name: targetSampleDataSet.name } }
115114
),
@@ -130,7 +129,7 @@ export class SampleDataSetCards extends React.Component {
130129
}));
131130
}
132131

133-
toastNotifications.addSuccess({
132+
this.toastNotifications.addSuccess({
134133
title: i18n.translate('kbn.home.sampleDataSet.installedLabel', {
135134
defaultMessage: '{name} installed', values: { name: targetSampleDataSet.name } }
136135
),
@@ -155,7 +154,7 @@ export class SampleDataSetCards extends React.Component {
155154
processingStatus: { ...prevState.processingStatus, [id]: false }
156155
}));
157156
}
158-
toastNotifications.addDanger({
157+
this.toastNotifications.addDanger({
159158
title: i18n.translate('kbn.home.sampleDataSet.unableToUninstallErrorMessage', {
160159
defaultMessage: 'Unable to uninstall sample data set: {name}', values: { name: targetSampleDataSet.name } }
161160
),
@@ -176,7 +175,7 @@ export class SampleDataSetCards extends React.Component {
176175
}));
177176
}
178177

179-
toastNotifications.addSuccess({
178+
this.toastNotifications.addSuccess({
180179
title: i18n.translate('kbn.home.sampleDataSet.uninstalledLabel', {
181180
defaultMessage: '{name} uninstalled', values: { name: targetSampleDataSet.name } }
182181
),
@@ -185,7 +184,9 @@ export class SampleDataSetCards extends React.Component {
185184
}
186185

187186
lightOrDarkImage = (sampleDataSet) => {
188-
return IS_DARK_THEME && sampleDataSet.darkPreviewImagePath ? sampleDataSet.darkPreviewImagePath : sampleDataSet.previewImagePath;
187+
return getServices().uiSettings.get('theme:darkMode') && sampleDataSet.darkPreviewImagePath
188+
? sampleDataSet.darkPreviewImagePath
189+
: sampleDataSet.previewImagePath;
189190
}
190191

191192
render() {

src/legacy/core_plugins/kibana/public/home/components/sample_data_view_data_button.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ import {
2727
} from '@elastic/eui';
2828

2929
import { i18n } from '@kbn/i18n';
30-
import chrome from 'ui/chrome';
30+
import { getServices } from '../kibana_services';
3131

3232
export class SampleDataViewDataButton extends React.Component {
33+
addBasePath = getServices().addBasePath;
3334

3435
state = {
3536
isPopoverOpen: false
@@ -56,7 +57,7 @@ export class SampleDataViewDataButton extends React.Component {
5657
datasetName: this.props.name,
5758
},
5859
});
59-
const dashboardPath = chrome.addBasePath(`/app/kibana#/dashboard/${this.props.overviewDashboard}`);
60+
const dashboardPath = this.addBasePath(`/app/kibana#/dashboard/${this.props.overviewDashboard}`);
6061

6162
if (this.props.appLinks.length === 0) {
6263
return (
@@ -79,7 +80,7 @@ export class SampleDataViewDataButton extends React.Component {
7980
size="m"
8081
/>
8182
),
82-
href: chrome.addBasePath(path)
83+
href: this.addBasePath(path)
8384
};
8485
});
8586
const panels = [

src/legacy/core_plugins/kibana/public/home/components/sample_data_view_data_button.test.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@
1717
* under the License.
1818
*/
1919

20-
jest.mock('ui/chrome', () => {
21-
return {
22-
addBasePath: (path) => {
23-
return `root${path}`;
24-
},
25-
};
26-
});
2720

2821
import React from 'react';
2922
import { shallow } from 'enzyme';
3023

3124
import { SampleDataViewDataButton } from './sample_data_view_data_button';
3225

26+
jest.mock('../kibana_services', () =>({
27+
getServices: () =>({
28+
addBasePath: path => `root${path}`
29+
})
30+
}));
31+
3332
test('should render simple button when appLinks is empty', () => {
3433
const component = shallow(<SampleDataViewDataButton
3534
id="ecommerce"

0 commit comments

Comments
 (0)