Skip to content

Commit

Permalink
[8.x] [Fleet] Improve reusable integration policies flow in package p…
Browse files Browse the repository at this point in the history
…olicies UI (#193532) (#194787)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Fleet] Improve reusable integration policies flow in package
policies UI (#193532)](#193532)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Cristina
Amico","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-03T07:43:47Z","message":"[Fleet]
Improve reusable integration policies flow in package policies UI
(#193532)\n\nCloses
https://github.com/elastic/kibana/issues/186357\r\n\r\n##
Summary\r\nImprove package policies list table support for reusable
agent policies.\r\nThis PR tweaks some parts of this UI:\r\n- The agent
count is now an aggregation of the total agents on all the\r\nshared
agent policies. Clicking on this count opens up a new pop up
that\r\nshows the number of agents for each policy and allows the agent
to\r\nnavigate either to those agents or all of them. The list shows
only the\r\ntop 5 policies by agent count.\r\n![Screenshot 2024-09-30 at
15
45\r\n16](https://github.com/user-attachments/assets/d56b53fd-ce6b-48cf-bc5b-0b5a1f0a7cca)\r\n![Screenshot
2024-09-30 at 14
56\r\n21](https://github.com/user-attachments/assets/831dd34c-d44d-446f-bbb7-0ee146b671e8)\r\n-
When clicking on \"add agent\", either from the actions or from
the\r\nbutton, we now show the agent policy selector as first step of
the\r\nflyout, so the user can choose which policy wants to enroll
agents to.\r\nCurrently instead the policy is
preselected\r\n![Screenshot 2024-10-01 at 11
06\r\n39](https://github.com/user-attachments/assets/aaba7f0f-21c9-4232-87db-62a4226764df)\r\n\r\n\r\n###
Checklist\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine
<[email protected]>\r\nCo-authored-by:
kibanamachine
<[email protected]>","sha":"9bc761a22c43efd56c689de2bcfbbb5a0a5ae4f9","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Fleet","v9.0.0","backport:prev-major","v8.16.0"],"title":"[Fleet]
Improve reusable integration policies flow in package policies
UI","number":193532,"url":"https://github.com/elastic/kibana/pull/193532","mergeCommit":{"message":"[Fleet]
Improve reusable integration policies flow in package policies UI
(#193532)\n\nCloses
https://github.com/elastic/kibana/issues/186357\r\n\r\n##
Summary\r\nImprove package policies list table support for reusable
agent policies.\r\nThis PR tweaks some parts of this UI:\r\n- The agent
count is now an aggregation of the total agents on all the\r\nshared
agent policies. Clicking on this count opens up a new pop up
that\r\nshows the number of agents for each policy and allows the agent
to\r\nnavigate either to those agents or all of them. The list shows
only the\r\ntop 5 policies by agent count.\r\n![Screenshot 2024-09-30 at
15
45\r\n16](https://github.com/user-attachments/assets/d56b53fd-ce6b-48cf-bc5b-0b5a1f0a7cca)\r\n![Screenshot
2024-09-30 at 14
56\r\n21](https://github.com/user-attachments/assets/831dd34c-d44d-446f-bbb7-0ee146b671e8)\r\n-
When clicking on \"add agent\", either from the actions or from
the\r\nbutton, we now show the agent policy selector as first step of
the\r\nflyout, so the user can choose which policy wants to enroll
agents to.\r\nCurrently instead the policy is
preselected\r\n![Screenshot 2024-10-01 at 11
06\r\n39](https://github.com/user-attachments/assets/aaba7f0f-21c9-4232-87db-62a4226764df)\r\n\r\n\r\n###
Checklist\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine
<[email protected]>\r\nCo-authored-by:
kibanamachine
<[email protected]>","sha":"9bc761a22c43efd56c689de2bcfbbb5a0a5ae4f9"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/193532","number":193532,"mergeCommit":{"message":"[Fleet]
Improve reusable integration policies flow in package policies UI
(#193532)\n\nCloses
https://github.com/elastic/kibana/issues/186357\r\n\r\n##
Summary\r\nImprove package policies list table support for reusable
agent policies.\r\nThis PR tweaks some parts of this UI:\r\n- The agent
count is now an aggregation of the total agents on all the\r\nshared
agent policies. Clicking on this count opens up a new pop up
that\r\nshows the number of agents for each policy and allows the agent
to\r\nnavigate either to those agents or all of them. The list shows
only the\r\ntop 5 policies by agent count.\r\n![Screenshot 2024-09-30 at
15
45\r\n16](https://github.com/user-attachments/assets/d56b53fd-ce6b-48cf-bc5b-0b5a1f0a7cca)\r\n![Screenshot
2024-09-30 at 14
56\r\n21](https://github.com/user-attachments/assets/831dd34c-d44d-446f-bbb7-0ee146b671e8)\r\n-
When clicking on \"add agent\", either from the actions or from
the\r\nbutton, we now show the agent policy selector as first step of
the\r\nflyout, so the user can choose which policy wants to enroll
agents to.\r\nCurrently instead the policy is
preselected\r\n![Screenshot 2024-10-01 at 11
06\r\n39](https://github.com/user-attachments/assets/aaba7f0f-21c9-4232-87db-62a4226764df)\r\n\r\n\r\n###
Checklist\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine
<[email protected]>\r\nCo-authored-by:
kibanamachine
<[email protected]>","sha":"9bc761a22c43efd56c689de2bcfbbb5a0a5ae4f9"}},{"branch":"8.x","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Cristina Amico <[email protected]>
  • Loading branch information
kibanamachine and criamico authored Oct 3, 2024
1 parent 39bfe8a commit a805375
Show file tree
Hide file tree
Showing 7 changed files with 388 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,85 +7,194 @@

import React from 'react';

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

import { createIntegrationsTestRendererMock } from '../../../../../../../../mock';
import type { AgentPolicy } from '../../../../../../types';
import { useAuthz, useMultipleAgentPolicies } from '../../../../../../hooks';

import { PackagePolicyAgentsCell } from './package_policy_agents_cell';

jest.mock('../../../../../../hooks', () => ({
...jest.requireActual('../../../../../../hooks'),
useAuthz: jest.fn(),
useMultipleAgentPolicies: jest.fn(),
useConfirmForceInstall: jest.fn(),
}));

const useMultipleAgentPoliciesMock = useMultipleAgentPolicies as jest.MockedFunction<
typeof useMultipleAgentPolicies
>;
function renderCell({
agentCount = 0,
agentPolicy = {} as AgentPolicy,
agentPolicies = [] as AgentPolicy[],
onAddAgent = () => {},
hasHelpPopover = false,
canAddAgents = true,
}) {
const renderer = createIntegrationsTestRendererMock();

return renderer.render(
<PackagePolicyAgentsCell
agentCount={agentCount}
agentPolicy={agentPolicy}
agentPolicies={agentPolicies}
onAddAgent={onAddAgent}
canAddAgents={canAddAgents}
hasHelpPopover={hasHelpPopover}
/>
);
}

describe('PackagePolicyAgentsCell', () => {
test('it should display add agent if count is 0', async () => {
const utils = renderCell({ agentCount: 0 });
await act(async () => {
expect(utils.queryByText('Add agent')).toBeInTheDocument();
});
beforeEach(() => {
jest.mocked(useAuthz).mockReturnValue({
fleet: {
addAgents: true,
addFleetServers: true,
},
} as any);
});

test('it should not display add agent if policy is managed', async () => {
const utils = renderCell({
agentCount: 0,
agentPolicy: {
is_managed: true,
} as AgentPolicy,
afterEach(() => {
jest.resetAllMocks();
});

describe('when multiple agent policies is disabled', () => {
beforeEach(() => {
useMultipleAgentPoliciesMock.mockReturnValue({ canUseMultipleAgentPolicies: false });
});
await act(async () => {
expect(utils.queryByText('Add agent')).not.toBeInTheDocument();

test('it should display add agent button if count is 0', async () => {
const utils = renderCell({
agentPolicies: [
{
name: 'test Policy 1',
} as AgentPolicy,
],
});
utils.debug();
await act(async () => {
expect(utils.queryByText('Add agent')).toBeInTheDocument();
});
});
});

test('it should display only count if count > 0', async () => {
const utils = renderCell({ agentCount: 9999 });
await act(async () => {
expect(utils.queryByText('Add agent')).not.toBeInTheDocument();
expect(utils.queryByText('9999')).toBeInTheDocument();
test('it should display only count if count > 0', async () => {
const utils = renderCell({
agentPolicies: [
{
name: 'test Policy 1',
agents: 999,
} as AgentPolicy,
],
});
await act(async () => {
expect(utils.queryByText('Add agent')).not.toBeInTheDocument();
expect(utils.queryByText('999')).toBeInTheDocument();
});
});
});

test('it should display help popover if count is 0 and hasHelpPopover=true', async () => {
const utils = renderCell({ agentCount: 0, hasHelpPopover: true });
await act(async () => {
expect(utils.queryByText('9999')).not.toBeInTheDocument();
expect(utils.queryByText('Add agent')).toBeInTheDocument();
expect(
utils.container.querySelector('[data-test-subj="addAgentHelpPopover"]')
).toBeInTheDocument();
test('it should not display help popover if count is > 0 and hasHelpPopover=true', async () => {
const utils = renderCell({
agentPolicies: [
{
name: 'test Policy 1',
agents: 999,
} as AgentPolicy,
],
hasHelpPopover: true,
});
await act(async () => {
expect(utils.queryByText('999')).toBeInTheDocument();
expect(utils.queryByText('Add agent')).not.toBeInTheDocument();
expect(
utils.container.querySelector('[data-test-subj="addAgentHelpPopover"]')
).not.toBeInTheDocument();
});
});
});
test('it should not display help popover if count is > 0 and hasHelpPopover=true', async () => {
const utils = renderCell({ agentCount: 9999, hasHelpPopover: true });
await act(async () => {
expect(utils.queryByText('9999')).toBeInTheDocument();
expect(utils.queryByText('Add agent')).not.toBeInTheDocument();
expect(
utils.container.querySelector('[data-test-subj="addAgentHelpPopover"]')
).not.toBeInTheDocument();

test('it should display help popover if count = 0 and hasHelpPopover=true', async () => {
const utils = renderCell({
hasHelpPopover: true,
agentPolicies: [
{
name: 'test Policy 1',
} as AgentPolicy,
],
});
await act(async () => {
expect(utils.queryByText('9999')).not.toBeInTheDocument();
expect(utils.queryByText('Add agent')).toBeInTheDocument();
expect(
utils.container.querySelector('[data-test-subj="addAgentHelpPopover"]')
).toBeInTheDocument();
});
});

test('it should not display add agent button if policy is managed', async () => {
const utils = renderCell({
agentPolicies: [
{
name: 'test Policy 1',
agents: 999,
is_managed: true,
} as AgentPolicy,
],
});
utils.debug();
await act(async () => {
expect(utils.queryByText('Add agent')).not.toBeInTheDocument();
expect(utils.queryByTestId('LinkedAgentCountLink')).toBeInTheDocument();
expect(utils.queryByText('999')).toBeInTheDocument();
});
});

test('Add agent button should be disabled if canAddAgents is false', async () => {
jest.mocked(useAuthz).mockReturnValue({
fleet: {
addAgents: false,
},
} as any);

const utils = renderCell({
agentPolicies: [
{
name: 'test Policy 1',
} as AgentPolicy,
],
});
await act(async () => {
expect(utils.container.querySelector('[data-test-subj="addAgentButton"]')).toBeDisabled();
});
});
});
test('it should be disabled if canAddAgents is false', async () => {
const utils = renderCell({ agentCount: 0, canAddAgents: false });
await act(async () => {
expect(utils.container.querySelector('[data-test-subj="addAgentButton"]')).toBeDisabled();

describe('when multiple agent policies is enabled', () => {
beforeEach(() => {
useMultipleAgentPoliciesMock.mockReturnValue({ canUseMultipleAgentPolicies: true });
});

test('it should display agent count sum and popover if agent count > 0', async () => {
jest.mocked(useAuthz).mockReturnValue({
fleet: {
addAgents: false,
},
} as any);

const utils = renderCell({
agentPolicies: [
{
name: 'test Policy 1',
agents: 100,
} as AgentPolicy,
{
name: 'test Policy 2',
agents: 200,
} as AgentPolicy,
],
});
await act(async () => {
expect(utils.queryByText('300')).toBeInTheDocument();
expect(utils.queryByText('Add agent')).not.toBeInTheDocument();
const button = utils.getByTestId('agentsCountsButton');
fireEvent.click(button);
expect(utils.queryByTestId('agentCountsPopover')).toBeInTheDocument();
});
});
});
});
Loading

0 comments on commit a805375

Please sign in to comment.