diff --git a/x-pack/plugins/graph/public/components/settings/settings.test.tsx b/x-pack/plugins/graph/public/components/settings/settings.test.tsx index 060b1e93fbdc..1c3fbb9f54f6 100644 --- a/x-pack/plugins/graph/public/components/settings/settings.test.tsx +++ b/x-pack/plugins/graph/public/components/settings/settings.test.tsx @@ -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; diff --git a/x-pack/plugins/graph/public/helpers/use_workspace_loader.test.tsx b/x-pack/plugins/graph/public/helpers/use_workspace_loader.test.tsx index cd0857f82ab6..c4c40ada490f 100644 --- a/x-pack/plugins/graph/public/helpers/use_workspace_loader.test.tsx +++ b/x-pack/plugins/graph/public/helpers/use_workspace_loader.test.tsx @@ -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(); - 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 + ); }); - 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 + ); }); - expect(props.spaces.ui.redirectLegacyUrl).toHaveBeenCalledWith( + expect(props.spaces?.ui.redirectLegacyUrl).toHaveBeenCalledWith( '#/workspace/aliasTargetId?query={}', 'Graph' ); diff --git a/x-pack/plugins/graph/public/state_management/mocks.ts b/x-pack/plugins/graph/public/state_management/mocks.ts index 6f7b5dba2e4a..5003e550f87a 100644 --- a/x-pack/plugins/graph/public/state_management/mocks.ts +++ b/x-pack/plugins/graph/public/state_management/mocks.ts @@ -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(() => diff --git a/x-pack/plugins/graph/public/state_management/persistence.test.ts b/x-pack/plugins/graph/public/state_management/persistence.test.ts index dc59869fafd4..2ef68f219807 100644 --- a/x-pack/plugins/graph/public/state_management/persistence.test.ts +++ b/x-pack/plugins/graph/public/state_management/persistence.test.ts @@ -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' } })),