Skip to content

Commit

Permalink
refactor with more comments: x-pack/plugins/security_solution/public/…
Browse files Browse the repository at this point in the history
…management/pages/endpoint_hosts/view/details/components/actions_menu.test.tsx
  • Loading branch information
walterra committed Sep 6, 2024
1 parent 8669579 commit a803117
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { QueryClient } from '@tanstack/react-query';
import { coreMock } from '@kbn/core/public/mocks';
import { PLUGIN_ID } from '@kbn/fleet-plugin/common';
import type { RenderHookOptions, RenderHookResult } from '@testing-library/react-hooks';
import { renderHook as reactRenderHoook } from '@testing-library/react-hooks';
import { renderHook as reactRenderHook } from '@testing-library/react-hooks';
import type {
ReactHooksRenderer,
WrapperComponent,
Expand Down Expand Up @@ -309,7 +309,7 @@ export const createAppRootMockRenderer = (): AppContextTestRender => {
hookFn: HookRendererFunction<TProps, TResult>,
options: RenderHookOptions<TProps> = {}
): RenderHookResult<TProps, TResult> => {
return reactRenderHoook<TProps, TResult>(hookFn, {
return reactRenderHook<TProps, TResult>(hookFn, {
wrapper: AppWrapper as WrapperComponent<TProps>,
...options,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { useUserPrivileges } from '../../../../../../common/components/user_priv
import { initialUserPrivilegesState } from '../../../../../../common/components/user_privileges/user_privileges_context';
import { getUserPrivilegesMockDefaultValue } from '../../../../../../common/components/user_privileges/__mocks__';
import type { HostInfo } from '../../../../../../../common/endpoint/types';
import userEvent from '@testing-library/user-event';
import userEvent, { type UserEvent } from '@testing-library/user-event';

jest.mock('../../../../../../common/lib/kibana/kibana_react', () => {
const originalModule = jest.requireActual('../../../../../../common/lib/kibana/kibana_react');
Expand All @@ -38,10 +38,12 @@ jest.mock('../../../../../../common/hooks/use_license');
jest.mock('../../../../../../common/components/user_privileges');

describe('When using the Endpoint Details Actions Menu', () => {
let user: UserEvent;
let render: () => Promise<ReturnType<AppContextTestRender['render']>>;
let coreStart: AppContextTestRender['coreStart'];
let renderResult: ReturnType<AppContextTestRender['render']>;
let httpMocks: ReturnType<typeof endpointPageHttpMock>;
// TODO middlewareSpy.waitForAction() times out after the upgrade to userEvent v14 https://github.com/elastic/kibana/pull/189949
// let middlewareSpy: AppContextTestRender['middlewareSpy'];
let endpointHost: HostInfo;

Expand All @@ -57,11 +59,21 @@ describe('When using the Endpoint Details Actions Menu', () => {
httpMocks.responseProvider.metadataDetails.mockReturnValue(endpointHost);
};

beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
});

beforeEach(() => {
// Workaround for timeout via https://github.com/testing-library/user-event/issues/833#issuecomment-1171452841
user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime });
const mockedContext = createAppRootMockRenderer();

(useKibana as jest.Mock).mockReturnValue({ services: mockedContext.startServices });
coreStart = mockedContext.coreStart;
// TODO middlewareSpy.waitForAction() times out after the upgrade to userEvent v14 https://github.com/elastic/kibana/pull/189949
// middlewareSpy = mockedContext.middlewareSpy;

httpMocks = endpointPageHttpMock(mockedContext.coreStart.http);
Expand All @@ -78,7 +90,7 @@ describe('When using the Endpoint Details Actions Menu', () => {
renderResult = mockedContext.render(<ActionsMenu hostMetadata={endpointHost?.metadata} />);
const endpointDetailsActionsButton = renderResult.getByTestId('endpointDetailsActionsButton');
endpointDetailsActionsButton.style.pointerEvents = 'all';
await userEvent.click(endpointDetailsActionsButton);
await user.click(endpointDetailsActionsButton);

return renderResult;
};
Expand Down Expand Up @@ -117,14 +129,14 @@ describe('When using the Endpoint Details Actions Menu', () => {
'should navigate via kibana `navigateToApp()` when %s is clicked',
async (_, dataTestSubj) => {
await render();
// TODO Revisit this as it times out after the upgrade to userEvent v14
// TODO middlewareSpy.waitForAction() times out after the upgrade to userEvent v14 https://github.com/elastic/kibana/pull/189949
// await act(async () => {
// await middlewareSpy.waitForAction('serverReturnedEndpointAgentPolicies');
// });

const takeActionMenuItem = renderResult.getByTestId(dataTestSubj);
takeActionMenuItem.style.pointerEvents = 'all';
await userEvent.click(takeActionMenuItem);
await user.click(takeActionMenuItem);

expect(coreStart.application.navigateToApp).toHaveBeenCalled();
}
Expand All @@ -144,7 +156,7 @@ describe('When using the Endpoint Details Actions Menu', () => {
await render();
const isolateButton = renderResult.getByTestId('unIsolateLink');
isolateButton.style.pointerEvents = 'all';
await userEvent.click(isolateButton);
await user.click(isolateButton);

expect(coreStart.application.navigateToApp).toHaveBeenCalled();
});
Expand Down

0 comments on commit a803117

Please sign in to comment.