Skip to content

Commit

Permalink
Fix jest test, update telemetry mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
sulemanof committed Jun 17, 2020
1 parent 2966bba commit c1b56af
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 33 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();
});
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;
}

0 comments on commit c1b56af

Please sign in to comment.