Skip to content

Commit

Permalink
Fix ScopedHistory mock and adapt usages (#71404) (#71599)
Browse files Browse the repository at this point in the history
* Fix mock and adapt usages

* fix snapshots

* add comment about forcecast

* remove mock overrides
  • Loading branch information
pgayvallet authored Jul 14, 2020
1 parent 8e7c699 commit 3c24ff4
Show file tree
Hide file tree
Showing 26 changed files with 63 additions and 102 deletions.
13 changes: 7 additions & 6 deletions src/core/public/application/scoped_history.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
import { Location } from 'history';
import { ScopedHistory } from './scoped_history';

type ScopedHistoryMock = jest.Mocked<Pick<ScopedHistory, keyof ScopedHistory>>;
export type ScopedHistoryMock = jest.Mocked<ScopedHistory>;

const createMock = ({
pathname = '/',
search = '',
hash = '',
key,
state,
...overrides
}: Partial<Location & ScopedHistoryMock> = {}) => {
const mock: ScopedHistoryMock = {
}: Partial<Location> = {}) => {
const mock: jest.Mocked<Pick<ScopedHistory, keyof ScopedHistory>> = {
block: jest.fn(),
createHref: jest.fn(),
createSubHistory: jest.fn(),
Expand All @@ -39,7 +39,6 @@ const createMock = ({
listen: jest.fn(),
push: jest.fn(),
replace: jest.fn(),
...overrides,
action: 'PUSH',
length: 1,
location: {
Expand All @@ -51,7 +50,9 @@ const createMock = ({
},
};

return mock;
// jest.Mocked still expects private methods and properties to be present, even
// if not part of the public contract.
return mock as ScopedHistoryMock;
};

export const scopedHistoryMock = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { coreMock, scopedHistoryMock } from '../../../../../core/public/mocks';
import { EmbeddableStateTransfer } from '.';
import { ApplicationStart, ScopedHistory } from '../../../../../core/public';
import { ApplicationStart } from '../../../../../core/public';

function mockHistoryState(state: unknown) {
return scopedHistoryMock.create({ state });
Expand All @@ -46,10 +46,7 @@ describe('embeddable state transfer', () => {

it('can send an outgoing originating app state in append mode', async () => {
const historyMock = mockHistoryState({ kibanaIsNowForSports: 'extremeSportsKibana' });
stateTransfer = new EmbeddableStateTransfer(
application.navigateToApp,
(historyMock as unknown) as ScopedHistory
);
stateTransfer = new EmbeddableStateTransfer(application.navigateToApp, historyMock);
await stateTransfer.navigateToEditor(destinationApp, {
state: { originatingApp },
appendToExistingState: true,
Expand All @@ -74,10 +71,7 @@ describe('embeddable state transfer', () => {

it('can send an outgoing embeddable package state in append mode', async () => {
const historyMock = mockHistoryState({ kibanaIsNowForSports: 'extremeSportsKibana' });
stateTransfer = new EmbeddableStateTransfer(
application.navigateToApp,
(historyMock as unknown) as ScopedHistory
);
stateTransfer = new EmbeddableStateTransfer(application.navigateToApp, historyMock);
await stateTransfer.navigateToWithEmbeddablePackage(destinationApp, {
state: { type: 'coolestType', id: '150' },
appendToExistingState: true,
Expand All @@ -90,40 +84,28 @@ describe('embeddable state transfer', () => {

it('can fetch an incoming originating app state', async () => {
const historyMock = mockHistoryState({ originatingApp: 'extremeSportsKibana' });
stateTransfer = new EmbeddableStateTransfer(
application.navigateToApp,
(historyMock as unknown) as ScopedHistory
);
stateTransfer = new EmbeddableStateTransfer(application.navigateToApp, historyMock);
const fetchedState = stateTransfer.getIncomingEditorState();
expect(fetchedState).toEqual({ originatingApp: 'extremeSportsKibana' });
});

it('returns undefined with originating app state is not in the right shape', async () => {
const historyMock = mockHistoryState({ kibanaIsNowForSports: 'extremeSportsKibana' });
stateTransfer = new EmbeddableStateTransfer(
application.navigateToApp,
(historyMock as unknown) as ScopedHistory
);
stateTransfer = new EmbeddableStateTransfer(application.navigateToApp, historyMock);
const fetchedState = stateTransfer.getIncomingEditorState();
expect(fetchedState).toBeUndefined();
});

it('can fetch an incoming embeddable package state', async () => {
const historyMock = mockHistoryState({ type: 'skisEmbeddable', id: '123' });
stateTransfer = new EmbeddableStateTransfer(
application.navigateToApp,
(historyMock as unknown) as ScopedHistory
);
stateTransfer = new EmbeddableStateTransfer(application.navigateToApp, historyMock);
const fetchedState = stateTransfer.getIncomingEmbeddablePackage();
expect(fetchedState).toEqual({ type: 'skisEmbeddable', id: '123' });
});

it('returns undefined when embeddable package is not in the right shape', async () => {
const historyMock = mockHistoryState({ kibanaIsNowForSports: 'extremeSportsKibana' });
stateTransfer = new EmbeddableStateTransfer(
application.navigateToApp,
(historyMock as unknown) as ScopedHistory
);
stateTransfer = new EmbeddableStateTransfer(application.navigateToApp, historyMock);
const fetchedState = stateTransfer.getIncomingEmbeddablePackage();
expect(fetchedState).toBeUndefined();
});
Expand All @@ -135,10 +117,7 @@ describe('embeddable state transfer', () => {
test1: 'test1',
test2: 'test2',
});
stateTransfer = new EmbeddableStateTransfer(
application.navigateToApp,
(historyMock as unknown) as ScopedHistory
);
stateTransfer = new EmbeddableStateTransfer(application.navigateToApp, historyMock);
stateTransfer.getIncomingEmbeddablePackage({ keysToRemoveAfterFetch: ['type', 'id'] });
expect(historyMock.replace).toHaveBeenCalledWith(
expect.objectContaining({ state: { test1: 'test1', test2: 'test2' } })
Expand All @@ -152,10 +131,7 @@ describe('embeddable state transfer', () => {
test1: 'test1',
test2: 'test2',
});
stateTransfer = new EmbeddableStateTransfer(
application.navigateToApp,
(historyMock as unknown) as ScopedHistory
);
stateTransfer = new EmbeddableStateTransfer(application.navigateToApp, historyMock);
stateTransfer.getIncomingEmbeddablePackage();
expect(historyMock.location.state).toEqual({
type: 'skisEmbeddable',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
/* eslint-disable @kbn/eslint/no-restricted-paths */
import React from 'react';
import { LocationDescriptorObject } from 'history';
import { ScopedHistory } from 'kibana/public';
import { KibanaContextProvider } from '../../../../../../src/plugins/kibana_react/public';
import {
notificationServiceMock,
Expand Down Expand Up @@ -35,10 +34,10 @@ const httpServiceSetupMock = new HttpService().setup({
fatalErrors: fatalErrorsServiceMock.createSetupContract(),
});

const history = (scopedHistoryMock.create() as unknown) as ScopedHistory;
history.createHref = (location: LocationDescriptorObject) => {
const history = scopedHistoryMock.create();
history.createHref.mockImplementation((location: LocationDescriptorObject) => {
return `${location.pathname}?${location.search}`;
};
});

const appServices = {
breadcrumbs: breadcrumbService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

jest.mock('./account_management_page');

import { AppMount, AppNavLinkStatus, ScopedHistory } from 'src/core/public';
import { AppMount, AppNavLinkStatus } from 'src/core/public';
import { UserAPIClient } from '../management';
import { accountManagementApp } from './account_management_app';

Expand Down Expand Up @@ -54,7 +54,7 @@ describe('accountManagementApp', () => {
element: containerMock,
appBasePath: '',
onAppLeave: jest.fn(),
history: (scopedHistoryMock.create() as unknown) as ScopedHistory,
history: scopedHistoryMock.create(),
});

expect(coreStartMock.chrome.setBreadcrumbs).toHaveBeenCalledTimes(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

jest.mock('./access_agreement_page');

import { AppMount, ScopedHistory } from 'src/core/public';
import { AppMount } from 'src/core/public';
import { accessAgreementApp } from './access_agreement_app';

import { coreMock, scopedHistoryMock } from '../../../../../../src/core/public/mocks';
Expand Down Expand Up @@ -48,7 +48,7 @@ describe('accessAgreementApp', () => {
element: containerMock,
appBasePath: '',
onAppLeave: jest.fn(),
history: (scopedHistoryMock.create() as unknown) as ScopedHistory,
history: scopedHistoryMock.create(),
});

const mockRenderApp = jest.requireMock('./access_agreement_page').renderAccessAgreementPage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

jest.mock('./logged_out_page');

import { AppMount, ScopedHistory } from 'src/core/public';
import { AppMount } from 'src/core/public';
import { loggedOutApp } from './logged_out_app';

import { coreMock, scopedHistoryMock } from '../../../../../../src/core/public/mocks';
Expand Down Expand Up @@ -46,7 +46,7 @@ describe('loggedOutApp', () => {
element: containerMock,
appBasePath: '',
onAppLeave: jest.fn(),
history: (scopedHistoryMock.create() as unknown) as ScopedHistory,
history: scopedHistoryMock.create(),
});

const mockRenderApp = jest.requireMock('./logged_out_page').renderLoggedOutPage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

jest.mock('./login_page');

import { AppMount, ScopedHistory } from 'src/core/public';
import { AppMount } from 'src/core/public';
import { loginApp } from './login_app';

import { coreMock, scopedHistoryMock } from '../../../../../../src/core/public/mocks';
Expand Down Expand Up @@ -51,7 +51,7 @@ describe('loginApp', () => {
element: containerMock,
appBasePath: '',
onAppLeave: jest.fn(),
history: (scopedHistoryMock.create() as unknown) as ScopedHistory,
history: scopedHistoryMock.create(),
});

const mockRenderApp = jest.requireMock('./login_page').renderLoginPage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { AppMount, ScopedHistory } from 'src/core/public';
import { AppMount } from 'src/core/public';
import { logoutApp } from './logout_app';

import { coreMock, scopedHistoryMock } from '../../../../../../src/core/public/mocks';
Expand Down Expand Up @@ -52,7 +52,7 @@ describe('logoutApp', () => {
element: containerMock,
appBasePath: '',
onAppLeave: jest.fn(),
history: (scopedHistoryMock.create() as unknown) as ScopedHistory,
history: scopedHistoryMock.create(),
});

expect(window.sessionStorage.clear).toHaveBeenCalledTimes(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

jest.mock('./overwritten_session_page');

import { AppMount, ScopedHistory } from 'src/core/public';
import { AppMount } from 'src/core/public';
import { overwrittenSessionApp } from './overwritten_session_app';

import { coreMock, scopedHistoryMock } from '../../../../../../src/core/public/mocks';
Expand Down Expand Up @@ -53,7 +53,7 @@ describe('overwrittenSessionApp', () => {
element: containerMock,
appBasePath: '',
onAppLeave: jest.fn(),
history: (scopedHistoryMock.create() as unknown) as ScopedHistory,
history: scopedHistoryMock.create(),
});

const mockRenderApp = jest.requireMock('./overwritten_session_page')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
jest.mock('./api_keys_grid', () => ({
APIKeysGridPage: (props: any) => `Page: ${JSON.stringify(props)}`,
}));
import { ScopedHistory } from 'src/core/public';
import { apiKeysManagementApp } from './api_keys_management_app';
import { coreMock, scopedHistoryMock } from '../../../../../../src/core/public/mocks';

Expand Down Expand Up @@ -37,7 +36,7 @@ describe('apiKeysManagementApp', () => {
basePath: '/some-base-path',
element: container,
setBreadcrumbs,
history: (scopedHistoryMock.create() as unknown) as ScopedHistory,
history: scopedHistoryMock.create(),
});

expect(setBreadcrumbs).toHaveBeenCalledTimes(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { findTestSubject } from 'test_utils/find_test_subject';
// This is not required for the tests to pass, but it rather suppresses lengthy
// warnings in the console which adds unnecessary noise to the test output.
import 'test_utils/stub_web_worker';
import { ScopedHistory } from 'kibana/public';

import { EditRoleMappingPage } from '.';
import { NoCompatibleRealms, SectionLoading, PermissionDenied } from '../components';
Expand All @@ -28,7 +27,7 @@ import { rolesAPIClientMock } from '../../roles/roles_api_client.mock';
import { RoleComboBox } from '../../role_combo_box';

describe('EditRoleMappingPage', () => {
const history = (scopedHistoryMock.create() as unknown) as ScopedHistory;
const history = scopedHistoryMock.create();
let rolesAPI: PublicMethodsOf<RolesAPIClient>;

beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('RoleMappingsGridPage', () => {
let coreStart: CoreStart;

beforeEach(() => {
history = (scopedHistoryMock.create() as unknown) as ScopedHistory;
history = scopedHistoryMock.create();
coreStart = coreMock.createStart();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jest.mock('./edit_role_mapping', () => ({
EditRoleMappingPage: (props: any) => `Role Mapping Edit Page: ${JSON.stringify(props)}`,
}));

import { ScopedHistory } from 'src/core/public';
import { roleMappingsManagementApp } from './role_mappings_management_app';
import { coreMock, scopedHistoryMock } from '../../../../../../src/core/public/mocks';

Expand All @@ -26,7 +25,7 @@ async function mountApp(basePath: string, pathname: string) {
basePath,
element: container,
setBreadcrumbs,
history: (scopedHistoryMock.create({ pathname }) as unknown) as ScopedHistory,
history: scopedHistoryMock.create({ pathname }),
});

return { unmount, container, setBreadcrumbs };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ReactWrapper } from 'enzyme';
import React from 'react';
import { act } from '@testing-library/react';
import { mountWithIntl, nextTick } from 'test_utils/enzyme_helpers';
import { Capabilities, ScopedHistory } from 'src/core/public';
import { Capabilities } from 'src/core/public';
import { Feature } from '../../../../../features/public';
import { Role } from '../../../../common/model';
import { DocumentationLinksService } from '../documentation_links';
Expand Down Expand Up @@ -187,7 +187,7 @@ function getProps({
docLinks: new DocumentationLinksService(docLinks),
fatalErrors,
uiCapabilities: buildUICapabilities(canManageSpaces),
history: (scopedHistoryMock.create() as unknown) as ScopedHistory,
history: scopedHistoryMock.create(),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { coreMock, scopedHistoryMock } from '../../../../../../../src/core/publi
import { rolesAPIClientMock } from '../index.mock';
import { ReservedBadge, DisabledBadge } from '../../badges';
import { findTestSubject } from 'test_utils/find_test_subject';
import { ScopedHistory } from 'kibana/public';

const mock403 = () => ({ body: { statusCode: 403 } });

Expand All @@ -42,12 +41,12 @@ const waitForRender = async (

describe('<RolesGridPage />', () => {
let apiClientMock: jest.Mocked<PublicMethodsOf<RolesAPIClient>>;
let history: ScopedHistory;
let history: ReturnType<typeof scopedHistoryMock.create>;

beforeEach(() => {
history = (scopedHistoryMock.create({
createHref: jest.fn((location) => location.pathname!),
}) as unknown) as ScopedHistory;
history = scopedHistoryMock.create();
history.createHref.mockImplementation((location) => location.pathname!);

apiClientMock = rolesAPIClientMock.create();
apiClientMock.getRoles.mockResolvedValue([
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ jest.mock('./edit_role', () => ({
EditRolePage: (props: any) => `Role Edit Page: ${JSON.stringify(props)}`,
}));

import { ScopedHistory } from 'src/core/public';

import { rolesManagementApp } from './roles_management_app';

import { coreMock, scopedHistoryMock } from '../../../../../../src/core/public/mocks';
Expand All @@ -40,7 +38,7 @@ async function mountApp(basePath: string, pathname: string) {
basePath,
element: container,
setBreadcrumbs,
history: (scopedHistoryMock.create({ pathname }) as unknown) as ScopedHistory,
history: scopedHistoryMock.create({ pathname }),
});

return { unmount, container, setBreadcrumbs };
Expand Down
Loading

0 comments on commit 3c24ff4

Please sign in to comment.