[Graph] remove warning and logs from console when testing (#113074)

* [Graph] simplify use_workspace_loader tests

* get rid of the warning about the same keys

* get rid the warning about not found method

* warning about the same keys

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Marta Bondyra 2021-09-27 11:33:33 +02:00 committed by GitHub
parent cfe084829f
commit 48be0ca3b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 24 deletions

View file

@ -24,6 +24,18 @@ import { createMockGraphStore } from '../../state_management/mocks';
import { Provider } from 'react-redux';
import { UrlTemplate } from '../../types';
jest.mock('@elastic/eui', () => {
const original = jest.requireActual('@elastic/eui');
return {
...original,
htmlIdGenerator: (fn: unknown) => {
let counter = 0;
return () => String(counter++);
},
};
});
describe('settings', () => {
let store: GraphStore;
let dispatchSpy: jest.Mock;

View file

@ -4,15 +4,13 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React from 'react';
import { mount } from 'enzyme';
import { useWorkspaceLoader, UseWorkspaceLoaderProps } from './use_workspace_loader';
import { coreMock } from 'src/core/public/mocks';
import { spacesPluginMock } from '../../../spaces/public/mocks';
import { createMockGraphStore } from '../state_management/mocks';
import { Workspace } from '../types';
import { SavedObjectsClientCommon } from 'src/plugins/data/common';
import { act } from 'react-dom/test-utils';
import { renderHook, act, RenderHookOptions } from '@testing-library/react-hooks';
jest.mock('react-router-dom', () => {
const useLocation = () => ({
@ -41,20 +39,6 @@ const mockSavedObjectsClient = {
find: jest.fn().mockResolvedValue({ title: 'test' }),
} as unknown as SavedObjectsClientCommon;
async function setup(props: UseWorkspaceLoaderProps) {
const returnVal = {};
function TestComponent() {
Object.assign(returnVal, useWorkspaceLoader(props));
return null;
}
await act(async () => {
const promise = Promise.resolve();
mount(<TestComponent />);
await act(() => promise);
});
return returnVal;
}
describe('use_workspace_loader', () => {
const defaultProps = {
workspaceRef: { current: {} as Workspace },
@ -62,13 +46,16 @@ describe('use_workspace_loader', () => {
savedObjectsClient: mockSavedObjectsClient,
coreStart: coreMock.createStart(),
spaces: spacesPluginMock.createStartContract(),
};
} as unknown as UseWorkspaceLoaderProps;
it('should not redirect if outcome is exactMatch', async () => {
await act(async () => {
await setup(defaultProps as unknown as UseWorkspaceLoaderProps);
renderHook(
() => useWorkspaceLoader(defaultProps),
defaultProps as RenderHookOptions<UseWorkspaceLoaderProps>
);
});
expect(defaultProps.spaces.ui.redirectLegacyUrl).not.toHaveBeenCalled();
expect(defaultProps.spaces?.ui.redirectLegacyUrl).not.toHaveBeenCalled();
expect(defaultProps.store.dispatch).toHaveBeenCalled();
});
it('should redirect if outcome is aliasMatch', async () => {
@ -83,11 +70,15 @@ describe('use_workspace_loader', () => {
alias_target_id: 'aliasTargetId',
}),
},
};
} as unknown as UseWorkspaceLoaderProps;
await act(async () => {
await setup(props as unknown as UseWorkspaceLoaderProps);
renderHook(
() => useWorkspaceLoader(props),
props as RenderHookOptions<UseWorkspaceLoaderProps>
);
});
expect(props.spaces.ui.redirectLegacyUrl).toHaveBeenCalledWith(
expect(props.spaces?.ui.redirectLegacyUrl).toHaveBeenCalledWith(
'#/workspace/aliasTargetId?query={}',
'Graph'
);

View file

@ -42,6 +42,7 @@ export function createMockGraphStore({
}): MockedGraphEnvironment {
const workspaceMock = {
runLayout: jest.fn(),
simpleSearch: jest.fn(),
nodes: [],
edges: [],
options: {},
@ -55,7 +56,7 @@ export function createMockGraphStore({
chrome: {
setBreadcrumbs: jest.fn(),
} as unknown as ChromeStart,
createWorkspace: jest.fn(),
createWorkspace: jest.fn((index, advancedSettings) => workspaceMock),
getWorkspace: jest.fn(() => workspaceMock),
indexPatternProvider: {
get: jest.fn(() =>

View file

@ -23,6 +23,18 @@ import { settingsSelector } from './advanced_settings';
import { openSaveModal } from '../services/save_modal';
const waitForPromise = () => new Promise((r) => setTimeout(r));
// mocking random id generator function
jest.mock('@elastic/eui', () => {
const original = jest.requireActual('@elastic/eui');
return {
...original,
htmlIdGenerator: (fn: unknown) => {
let counter = 0;
return () => counter++;
},
};
});
jest.mock('../services/persistence', () => ({
lookupIndexPatternId: jest.fn(() => ({ id: '123', attributes: { title: 'test-pattern' } })),