Skip to content

Commit

Permalink
correct usages of testing library for fleet
Browse files Browse the repository at this point in the history
  • Loading branch information
eokoneyo committed Nov 22, 2024
1 parent 51a84eb commit 1392385
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import React, { lazy, memo } from 'react';
import { Route } from '@kbn/shared-ux-router';
import { act, cleanup } from '@testing-library/react';
import { act } from '@testing-library/react';

import { INTEGRATIONS_ROUTING_PATHS, pagePathGetters } from '../../../../constants';
import type {
Expand Down Expand Up @@ -65,10 +65,6 @@ describe('When on integration detail', () => {
act(() => testRenderer.mountHistory.push(detailPageUrlPath));
});

afterEach(() => {
cleanup();
});

describe('and the package is installed', () => {
beforeEach(async () => {
await render();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import './agent_enrollment_flyout.test.mocks';

import React from 'react';
import { act, cleanup, fireEvent, waitFor } from '@testing-library/react';
import { fireEvent, waitFor } from '@testing-library/react';
import type { RenderResult } from '@testing-library/react';

import { createFleetTestRendererMock } from '../../mock';
Expand All @@ -23,11 +23,8 @@ import type { FlyOutProps } from './types';
import { AgentEnrollmentFlyout } from '.';

const render = (props?: Partial<FlyOutProps>) => {
cleanup();
const renderer = createFleetTestRendererMock();
const results = renderer.render(<AgentEnrollmentFlyout onClose={jest.fn()} {...props} />);

return results;
return renderer.render(<AgentEnrollmentFlyout onClose={jest.fn()} {...props} />);
};

const testAgentPolicy: AgentPolicy = {
Expand All @@ -46,7 +43,7 @@ const testAgentPolicy: AgentPolicy = {
describe('<AgentEnrollmentFlyout />', () => {
let results: RenderResult;

beforeEach(async () => {
beforeEach(() => {
jest.mocked(useAuthz).mockReturnValue({
fleet: {
readAgentPolicies: true,
Expand Down Expand Up @@ -88,10 +85,6 @@ describe('<AgentEnrollmentFlyout />', () => {
agentPolicies: [{ id: 'fleet-server-policy' } as AgentPolicy],
refreshAgentPolicies: jest.fn(),
});

act(() => {
results = render();
});
});

afterEach(() => {
Expand All @@ -113,6 +106,8 @@ describe('<AgentEnrollmentFlyout />', () => {

describe('managed instructions', () => {
it('uses the agent policy selection step', () => {
results = render();

expect(results.queryByTestId('agentEnrollmentFlyout')).not.toBeNull();
expect(results.queryByTestId('agent-policy-selection-step')).not.toBeNull();
expect(results.queryByTestId('agent-enrollment-key-selection-step')).toBeNull();
Expand Down Expand Up @@ -154,24 +149,22 @@ describe('<AgentEnrollmentFlyout />', () => {

describe('standalone instructions', () => {
function goToStandaloneTab() {
act(() => {
fireEvent.click(results.getByTestId('standaloneTab'));
});
fireEvent.click(results.getByTestId('standaloneTab'));
}

beforeEach(() => {
it('uses the agent policy selection step', async () => {
results = render({
isIntegrationFlow: true,
});
});

it('uses the agent policy selection step', async () => {
goToStandaloneTab();

expect(results.queryByTestId('agentEnrollmentFlyout')).not.toBeNull();
expect(results.queryByTestId('agent-policy-selection-step')).not.toBeNull();
expect(results.queryByTestId('agent-enrollment-key-selection-step')).toBeNull();
expect(results.queryByTestId('configure-standalone-step')).not.toBeNull();
await waitFor(() => {
expect(results.queryByTestId('agentEnrollmentFlyout')).not.toBeNull();
expect(results.queryByTestId('agent-policy-selection-step')).not.toBeNull();
expect(results.queryByTestId('agent-enrollment-key-selection-step')).toBeNull();
expect(results.queryByTestId('configure-standalone-step')).not.toBeNull();
});
});

describe('with a specific policy', () => {
Expand All @@ -185,13 +178,15 @@ describe('<AgentEnrollmentFlyout />', () => {
});
});

it('does not use either of the agent policy selection or enrollment key steps', () => {
it('does not use either of the agent policy selection or enrollment key steps', async () => {
goToStandaloneTab();

expect(results.queryByTestId('agentEnrollmentFlyout')).not.toBeNull();
expect(results.queryByTestId('agent-policy-selection-step')).toBeNull();
expect(results.queryByTestId('agent-enrollment-key-selection-step')).toBeNull();
expect(results.queryByTestId('configure-standalone-step')).not.toBeNull();
await waitFor(() => {
expect(results.queryByTestId('agentEnrollmentFlyout')).not.toBeNull();
expect(results.queryByTestId('agent-policy-selection-step')).toBeNull();
expect(results.queryByTestId('agent-enrollment-key-selection-step')).toBeNull();
expect(results.queryByTestId('configure-standalone-step')).not.toBeNull();
});
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@

import React from 'react';

import { cleanup, waitFor } from '@testing-library/react';
import { waitFor } from '@testing-library/react';

import { createFleetTestRendererMock } from '../../mock';

import { RootPrivilegesCallout } from './root_privileges_callout';

describe('RootPrivilegesCallout', () => {
function render(rootIntegrations?: Array<{ name: string; title: string }>) {
cleanup();
const renderer = createFleetTestRendererMock();
const results = renderer.render(<RootPrivilegesCallout rootIntegrations={rootIntegrations} />);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { act, fireEvent } from '@testing-library/react';
import { fireEvent, waitFor } from '@testing-library/react';
import React from 'react';

import type { TestRenderer } from '../mock';
Expand All @@ -31,7 +31,7 @@ describe('MultipleAgentPolicySummaryLine', () => {
testRenderer = createFleetTestRendererMock();
});

test('it should only render the policy name when there is only one policy', async () => {
test('it should only render the policy name when there is only one policy', () => {
const results = render([{ name: 'Test policy', revision: 2 }] as AgentPolicy[]);
expect(results.container.textContent).toBe('Test policyrev. 2');
expect(results.queryByTestId('agentPolicyNameLink')).toBeInTheDocument();
Expand All @@ -48,19 +48,18 @@ describe('MultipleAgentPolicySummaryLine', () => {
expect(results.queryByTestId('agentPoliciesNumberBadge')).toBeInTheDocument();
expect(results.container.textContent).toBe('Test policy 1+2');

await act(async () => {
fireEvent.click(results.getByTestId('agentPoliciesNumberBadge'));
});
expect(results.queryByTestId('agentPoliciesPopover')).toBeInTheDocument();
expect(results.queryByTestId('agentPoliciesPopoverButton')).toBeInTheDocument();
expect(results.queryByTestId('policy-0001')).toBeInTheDocument();
expect(results.queryByTestId('policy-0002')).toBeInTheDocument();
expect(results.queryByTestId('policy-0003')).toBeInTheDocument();
fireEvent.click(results.getByTestId('agentPoliciesNumberBadge'));

await act(async () => {
fireEvent.click(results.getByTestId('agentPoliciesPopoverButton'));
await waitFor(() => {
expect(results.queryByTestId('agentPoliciesPopover')).toBeInTheDocument();
expect(results.queryByTestId('agentPoliciesPopoverButton')).toBeInTheDocument();
expect(results.queryByTestId('policy-0001')).toBeInTheDocument();
expect(results.queryByTestId('policy-0002')).toBeInTheDocument();
expect(results.queryByTestId('policy-0003')).toBeInTheDocument();
});

fireEvent.click(results.getByTestId('agentPoliciesPopoverButton'));

expect(results.queryByTestId('manageAgentPoliciesModal')).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import React from 'react';

import { act } from '@testing-library/react';
import { waitFor } from '@testing-library/react';

import type { AgentPolicy, InMemoryPackagePolicy } from '../types';
import { createIntegrationsTestRendererMock } from '../mock';
Expand Down Expand Up @@ -118,7 +118,8 @@ describe('PackagePolicyActionsMenu', () => {
const agentPolicies = createMockAgentPolicies();
const packagePolicy = createMockPackagePolicy({ hasUpgrade: false });
const { utils } = renderMenu({ agentPolicies, packagePolicy });
await act(async () => {

await waitFor(() => {
const upgradeButton = utils.getByTestId('PackagePolicyActionsUpgradeItem');
expect(upgradeButton).toBeDisabled();
});
Expand All @@ -129,7 +130,7 @@ describe('PackagePolicyActionsMenu', () => {
const packagePolicy = createMockPackagePolicy({ hasUpgrade: true });
const { utils } = renderMenu({ agentPolicies, packagePolicy });

await act(async () => {
await waitFor(() => {
const upgradeButton = utils.getByTestId('PackagePolicyActionsUpgradeItem');
expect(upgradeButton).not.toBeDisabled();
});
Expand All @@ -140,7 +141,7 @@ describe('PackagePolicyActionsMenu', () => {
const packagePolicy = createMockPackagePolicy({ hasUpgrade: true });
const { utils } = renderMenu({ agentPolicies, packagePolicy });

await act(async () => {
await waitFor(() => {
const upgradeButton = utils.getByTestId('PackagePolicyActionsUpgradeItem');
expect(upgradeButton).toBeDisabled();
});
Expand All @@ -150,7 +151,7 @@ describe('PackagePolicyActionsMenu', () => {
const agentPolicies = createMockAgentPolicies({ is_managed: true });
const packagePolicy = createMockPackagePolicy();
const { utils } = renderMenu({ agentPolicies, packagePolicy });
await act(async () => {
await waitFor(() => {
expect(utils.queryByText('Delete integration')).toBeNull();
});
});
Expand All @@ -159,7 +160,7 @@ describe('PackagePolicyActionsMenu', () => {
const agentPolicies = createMockAgentPolicies({ is_managed: false });
const packagePolicy = createMockPackagePolicy();
const { utils } = renderMenu({ agentPolicies, packagePolicy });
await act(async () => {
await waitFor(() => {
expect(utils.queryByText('Delete integration')).not.toBeNull();
});
});
Expand All @@ -168,7 +169,7 @@ describe('PackagePolicyActionsMenu', () => {
const agentPolicies = createMockAgentPolicies({ is_managed: false, supports_agentless: true });
const packagePolicy = createMockPackagePolicy();
const { utils } = renderMenu({ agentPolicies, packagePolicy });
await act(async () => {
await waitFor(() => {
expect(utils.queryByText('Delete integration')).not.toBeNull();
});
});
Expand All @@ -177,7 +178,7 @@ describe('PackagePolicyActionsMenu', () => {
const agentPolicies = createMockAgentPolicies();
const packagePolicy = createMockPackagePolicy({ hasUpgrade: true });
const { utils } = renderMenu({ agentPolicies, packagePolicy, showAddAgent: true });
await act(async () => {
await waitFor(() => {
expect(utils.queryByText('Add agent')).not.toBeNull();
});
});
Expand All @@ -186,7 +187,7 @@ describe('PackagePolicyActionsMenu', () => {
const agentPolicies = createMockAgentPolicies({ is_managed: true });
const packagePolicy = createMockPackagePolicy({ hasUpgrade: true });
const { utils } = renderMenu({ agentPolicies, packagePolicy, showAddAgent: true });
await act(async () => {
await waitFor(() => {
expect(utils.queryByText('Add agent')).toBeNull();
});
});
Expand All @@ -195,7 +196,7 @@ describe('PackagePolicyActionsMenu', () => {
const agentPolicies = createMockAgentPolicies({ supports_agentless: true });
const packagePolicy = createMockPackagePolicy({ hasUpgrade: true });
const { utils } = renderMenu({ agentPolicies, packagePolicy, showAddAgent: true });
await act(async () => {
await waitFor(() => {
expect(utils.queryByText('Add agent')).toBeNull();
});
});
Expand All @@ -204,7 +205,7 @@ describe('PackagePolicyActionsMenu', () => {
const agentPolicies = createMockAgentPolicies();
const packagePolicy = createMockPackagePolicy();
const { utils } = renderMenu({ agentPolicies, packagePolicy });
await act(async () => {
await waitFor(() => {
const editButton = utils.getByTestId('PackagePolicyActionsEditItem');
expect(editButton).not.toHaveAttribute('disabled');
expect(editButton).toHaveAttribute('href');
Expand All @@ -227,7 +228,7 @@ describe('PackagePolicyActionsMenu', () => {
agentPolicies: [],
packagePolicy,
});
await act(async () => {
await waitFor(() => {
const editButton = utils.getByTestId('PackagePolicyActionsEditItem');
expect(editButton).not.toHaveAttribute('disabled');
expect(editButton).toHaveAttribute('href');
Expand Down

0 comments on commit 1392385

Please sign in to comment.