Skip to content

Commit

Permalink
Refactor FleetStartServices mock
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-tavares committed Nov 24, 2020
1 parent e6e8eb9 commit d013587
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@

import { createMemoryHistory } from 'history';
import React, { memo } from 'react';
import { render as reactRender, RenderOptions, RenderResult } from '@testing-library/react';
import { render as reactRender, RenderOptions, RenderResult, act } from '@testing-library/react';
import { ScopedHistory } from '../../../../../../../src/core/public';
import { coreMock } from '../../../../../../../src/core/public/mocks';
import { IStorage, Storage } from '../../../../../../../src/plugins/kibana_utils/public';
import { FleetAppContext } from '../app';
import { FleetConfigType, FleetStart, FleetStartServices } from '../../../plugin';
import { createStartDepsMock } from './plugin_dependencies';
import { createConfigurationMock } from './plugin_configuration';
import { UIExtensionsStorage } from '../types';
import { createStartMock } from './plugin_interfaces';
import { MockedKeys } from '../../../../../../../packages/kbn-utility-types/jest/index';
import { setHttpClient } from '../hooks/use_request';
import { createStartServices } from './fleet_start_services';

type UiRender = (ui: React.ReactElement, options?: RenderOptions) => RenderResult;

Expand All @@ -32,41 +28,10 @@ export interface TestRenderer {
render: UiRender;
}

const createMockStore = (): MockedKeys<IStorage> => {
let store: Record<string, any> = {};
return {
getItem: jest.fn().mockImplementation((key) => store[key]),
setItem: jest.fn().mockImplementation((key, value) => (store[key] = value)),
removeItem: jest.fn().mockImplementation((key: string) => delete store[key]),
clear: jest.fn().mockImplementation(() => (store = {})),
};
};

const configureStartServices = (services: FleetStartServices): void => {
// Store the http for use by useRequest
setHttpClient(services.http);

// Set Fleet available capabilities
services.application.capabilities = {
...services.application.capabilities,
fleet: {
read: true,
write: true,
},
};
};

export const createTestRendererMock = (): TestRenderer => {
const basePath = '/mock';
const extensions: UIExtensionsStorage = {};
const startServices: FleetStartServices = {
...coreMock.createStart({ basePath }),
...createStartDepsMock(),
storage: new Storage(createMockStore()),
};

configureStartServices(startServices);

const startServices = createStartServices(basePath);
const testRendererMocks: TestRenderer = {
history: new ScopedHistory(createMemoryHistory({ initialEntries: [basePath] }), basePath),
startServices,
Expand All @@ -88,10 +53,15 @@ export const createTestRendererMock = (): TestRenderer => {
);
}),
render: (ui, options) => {
return reactRender(ui, {
wrapper: testRendererMocks.AppWrapper,
...options,
let renderResponse: RenderResult;
act(() => {
renderResponse = reactRender(ui, {
wrapper: testRendererMocks.AppWrapper,
...options,
});
});
// @ts-ignore
return renderResponse;
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import { I18nProvider } from '@kbn/i18n/react';
import { coreMock } from '../../../../../../../src/core/public/mocks';
import { createStartDepsMock } from './plugin_dependencies';
import { IStorage, Storage } from '../../../../../../../src/plugins/kibana_utils/public';
import { MockedKeys } from '../../../../../../../packages/kbn-utility-types/jest/index';
import { setHttpClient } from '../hooks/use_request';
import { MockedFleetStartServices } from './types';

// Taken from core. See: src/plugins/kibana_utils/public/storage/storage.test.ts
const createMockStore = (): MockedKeys<IStorage> => {
let store: Record<string, any> = {};
return {
getItem: jest.fn().mockImplementation((key) => store[key]),
setItem: jest.fn().mockImplementation((key, value) => (store[key] = value)),
removeItem: jest.fn().mockImplementation((key: string) => delete store[key]),
clear: jest.fn().mockImplementation(() => (store = {})),
};
};

const configureStartServices = (services: MockedFleetStartServices): void => {
// Store the http for use by useRequest
setHttpClient(services.http);

// Set Fleet available capabilities
services.application.capabilities = {
...services.application.capabilities,
fleet: {
read: true,
write: true,
},
};

// Setup the `i18n.Context` component
services.i18n.Context.mockImplementation(({ children }: { children: React.ReactNode }) => (
<I18nProvider>{children}</I18nProvider>
));
};

export const createStartServices = (basePath: string = '/mock'): MockedFleetStartServices => {
const startServices: MockedFleetStartServices = {
...coreMock.createStart({ basePath }),
...createStartDepsMock(),
storage: new Storage(createMockStore()) as jest.Mocked<Storage>,
};

configureStartServices(startServices);

return startServices;
};
2 changes: 2 additions & 0 deletions x-pack/plugins/fleet/public/applications/fleet/mock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export * from './create_test_renderer';
export * from './plugin_configuration';
export * from './plugin_dependencies';
export * from './plugin_interfaces';
export * from './fleet_start_services';
export * from './types';
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { IngestManagerSetupDeps, IngestManagerStartDeps } from '../../../plugin';
import { dataPluginMock } from '../../../../../../../src/plugins/data/public/mocks';
import { licensingMock } from '../../../../../licensing/public/mocks';
import { homePluginMock } from '../../../../../../../src/plugins/home/public/mocks';
import { MockedFleetSetupDeps, MockedFleetStartDeps } from './types';

export const createSetupDepsMock = (): IngestManagerSetupDeps => {
export const createSetupDepsMock = (): MockedFleetSetupDeps => {
return {
licensing: licensingMock.createSetup(),
data: dataPluginMock.createSetupContract(),
home: homePluginMock.createSetupContract(),
};
};

export const createStartDepsMock = (): IngestManagerStartDeps => {
export const createStartDepsMock = (): MockedFleetStartDeps => {
return {
data: dataPluginMock.createStartContract(),
};
Expand Down
14 changes: 14 additions & 0 deletions x-pack/plugins/fleet/public/applications/fleet/mock/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { MockedKeys } from '../../../../../../../packages/kbn-utility-types/jest/index';
import { FleetSetupDeps, FleetStartDeps, FleetStartServices } from '../../../plugin';

export type MockedFleetStartServices = MockedKeys<FleetStartServices>;

export type MockedFleetSetupDeps = MockedKeys<FleetSetupDeps>;

export type MockedFleetStartDeps = MockedKeys<FleetStartDeps>;
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('when on integration detail', () => {

beforeEach(() => {
testRenderer = createTestRendererMock();
testRenderer.history.push(pagePathGetters.integration_details({ pkgkey: 'ngnix' }));
testRenderer.history.push(pagePathGetters.integration_details({ pkgkey: 'ngnix-99.0.0' }));
});

describe('and a custom UI extension is NOT registered', () => {
Expand Down

0 comments on commit d013587

Please sign in to comment.