Skip to content

Commit

Permalink
Fix home page loading if telemetry plugin disabled (#69394) (#69522)
Browse files Browse the repository at this point in the history
* Fix home page loading

* Fix jest test, update telemetry mocks
  • Loading branch information
sulemanof authored Jun 18, 2020
1 parent 216bfe3 commit 1b5c8c2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 35 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 9 additions & 26 deletions src/plugins/home/public/application/components/welcome.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,56 +30,39 @@ jest.mock('../kibana_services', () => ({
}));

test('should render a Welcome screen with the telemetry disclaimer', () => {
const telemetry = telemetryPluginMock.createSetupContract();
const component = shallow(
// @ts-ignore
<Welcome urlBasePath="/" onSkip={() => {}} telemetry={telemetry} />
);
const telemetry = telemetryPluginMock.createStartContract();
const component = shallow(<Welcome urlBasePath="/" onSkip={() => {}} telemetry={telemetry} />);

expect(component).toMatchSnapshot();
});

test('should render a Welcome screen with the telemetry disclaimer when optIn is true', () => {
const telemetry = telemetryPluginMock.createSetupContract();
const telemetry = telemetryPluginMock.createStartContract();
telemetry.telemetryService.getIsOptedIn = jest.fn().mockReturnValue(true);
const component = shallow(
// @ts-ignore
<Welcome urlBasePath="/" onSkip={() => {}} telemetry={telemetry} />
);
const component = shallow(<Welcome urlBasePath="/" onSkip={() => {}} telemetry={telemetry} />);

expect(component).toMatchSnapshot();
});

test('should render a Welcome screen with the telemetry disclaimer when optIn is false', () => {
const telemetry = telemetryPluginMock.createSetupContract();
const telemetry = telemetryPluginMock.createStartContract();
telemetry.telemetryService.getIsOptedIn = jest.fn().mockReturnValue(false);
const component = shallow(
// @ts-ignore
<Welcome urlBasePath="/" onSkip={() => {}} telemetry={telemetry} />
);
const component = shallow(<Welcome urlBasePath="/" onSkip={() => {}} telemetry={telemetry} />);

expect(component).toMatchSnapshot();
});

test('should render a Welcome screen with no telemetry disclaimer', () => {
// @ts-ignore
const component = shallow(
// @ts-ignore
<Welcome urlBasePath="/" onSkip={() => {}} telemetry={null} />
);
const component = shallow(<Welcome urlBasePath="/" onSkip={() => {}} />);

expect(component).toMatchSnapshot();
});

test('fires opt-in seen when mounted', () => {
const telemetry = telemetryPluginMock.createSetupContract();
const telemetry = telemetryPluginMock.createStartContract();
const mockSetOptedInNoticeSeen = jest.fn();
// @ts-ignore
telemetry.telemetryNotifications.setOptedInNoticeSeen = mockSetOptedInNoticeSeen;
shallow(
// @ts-ignore
<Welcome urlBasePath="/" onSkip={() => {}} telemetry={telemetry} />
);
shallow(<Welcome urlBasePath="/" onSkip={() => {}} telemetry={telemetry} />);

expect(mockSetOptedInNoticeSeen).toHaveBeenCalled();
});
7 changes: 5 additions & 2 deletions src/plugins/home/public/application/components/welcome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import { METRIC_TYPE } from '@kbn/analytics';
import { FormattedMessage } from '@kbn/i18n/react';
import { getServices } from '../kibana_services';
import { TelemetryPluginStart } from '../../../../telemetry/public';
import { PRIVACY_STATEMENT_URL } from '../../../../telemetry/common/constants';

import { SampleDataCard } from './sample_data';
interface Props {
Expand Down Expand Up @@ -162,7 +161,11 @@ export class Welcome extends React.Component<Props> {
id="home.dataManagementDisclaimerPrivacy"
defaultMessage="To learn about how usage data helps us manage and improve our products and services, see our "
/>
<EuiLink href={PRIVACY_STATEMENT_URL} target="_blank" rel="noopener">
<EuiLink
href={telemetry.telemetryConstants.getPrivacyStatementUrl()}
target="_blank"
rel="noopener"
>
<FormattedMessage
id="home.dataManagementDisclaimerPrivacyLink"
defaultMessage="Privacy Statement."
Expand Down
23 changes: 19 additions & 4 deletions src/plugins/telemetry/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { httpServiceMock } from '../../../core/public/http/http_service.mock';
import { notificationServiceMock } from '../../../core/public/notifications/notifications_service.mock';
import { TelemetryService } from './services/telemetry_service';
import { TelemetryNotifications } from './services/telemetry_notifications/telemetry_notifications';
import { TelemetryPluginStart, TelemetryPluginConfig } from './plugin';
import { TelemetryPluginStart, TelemetryPluginSetup, TelemetryPluginConfig } from './plugin';

// The following is to be able to access private methods
/* eslint-disable dot-notation */
Expand Down Expand Up @@ -77,20 +77,35 @@ export function mockTelemetryNotifications({
});
}

export type Setup = jest.Mocked<TelemetryPluginStart>;
export type Setup = jest.Mocked<TelemetryPluginSetup>;
export type Start = jest.Mocked<TelemetryPluginStart>;

export const telemetryPluginMock = {
createSetupContract,
createStartContract,
};

function createSetupContract(): Setup {
const telemetryService = mockTelemetryService();
const telemetryNotifications = mockTelemetryNotifications({ telemetryService });

const setupContract: Setup = {
telemetryService,
telemetryNotifications,
};

return setupContract;
}

function createStartContract(): Start {
const telemetryService = mockTelemetryService();
const telemetryNotifications = mockTelemetryNotifications({ telemetryService });

const startContract: Start = {
telemetryService,
telemetryNotifications,
telemetryConstants: {
getPrivacyStatementUrl: jest.fn(),
},
};

return startContract;
}
7 changes: 7 additions & 0 deletions src/plugins/telemetry/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
getTelemetrySendUsageFrom,
} from '../common/telemetry_config';
import { getNotifyUserAboutOptInDefault } from '../common/telemetry_config/get_telemetry_notify_user_about_optin_default';
import { PRIVACY_STATEMENT_URL } from '../common/constants';

export interface TelemetryPluginSetup {
telemetryService: TelemetryService;
Expand All @@ -46,6 +47,9 @@ export interface TelemetryPluginSetup {
export interface TelemetryPluginStart {
telemetryService: TelemetryService;
telemetryNotifications: TelemetryNotifications;
telemetryConstants: {
getPrivacyStatementUrl: () => string;
};
}

export interface TelemetryPluginConfig {
Expand Down Expand Up @@ -115,6 +119,9 @@ export class TelemetryPlugin implements Plugin<TelemetryPluginSetup, TelemetryPl
return {
telemetryService: this.telemetryService,
telemetryNotifications: this.telemetryNotifications,
telemetryConstants: {
getPrivacyStatementUrl: () => PRIVACY_STATEMENT_URL,
},
};
}

Expand Down

0 comments on commit 1b5c8c2

Please sign in to comment.