forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fleet] Extract hook for agentList table logic (elastic#175528)
Closes elastic#131153 Follow up of elastic#175318 ## Summary Extract the main logic for agentList component in a separate hook. ### Checklist - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed --------- Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
2a5c8ed
commit 62660b1
Showing
4 changed files
with
512 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
...c/applications/fleet/sections/agents/agent_list_page/hooks/use_fetch_agents_data.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
|
||
import { useStartServices } from '../../../../hooks'; | ||
|
||
import { ExperimentalFeaturesService } from '../../../../services'; | ||
|
||
import { useFetchAgentsData } from './use_fetch_agents_data'; | ||
|
||
jest.mock('../../../../../../services/experimental_features'); | ||
const mockedExperimentalFeaturesService = jest.mocked(ExperimentalFeaturesService); | ||
|
||
jest.mock('../../../../hooks', () => ({ | ||
...jest.requireActual('../../../../hooks'), | ||
sendGetAgents: jest.fn().mockResolvedValue({ | ||
data: { | ||
total: 5, | ||
}, | ||
}), | ||
sendGetAgentStatus: jest.fn().mockResolvedValue({ | ||
data: { | ||
results: { | ||
inactive: 2, | ||
}, | ||
totalInactive: 2, | ||
}, | ||
}), | ||
sendGetAgentPolicies: jest.fn().mockReturnValue({ | ||
data: { | ||
items: [ | ||
{ id: 'agent-policy-1', name: 'Agent policy 1', namespace: 'default' }, | ||
{ | ||
id: 'agent-policy-managed', | ||
name: 'Managed Agent policy', | ||
namespace: 'default', | ||
managed: true, | ||
}, | ||
], | ||
}, | ||
}), | ||
useGetAgentPolicies: jest.fn().mockReturnValue({ | ||
data: { | ||
items: [ | ||
{ id: 'agent-policy-1', name: 'Agent policy 1', namespace: 'default' }, | ||
{ | ||
id: 'agent-policy-managed', | ||
name: 'Managed Agent policy', | ||
namespace: 'default', | ||
managed: true, | ||
}, | ||
], | ||
}, | ||
error: undefined, | ||
isLoading: false, | ||
resendRequest: jest.fn(), | ||
} as any), | ||
sendGetAgentTags: jest.fn().mockReturnValue({ data: { items: ['tag1', 'tag2'] } }), | ||
useStartServices: jest.fn().mockReturnValue({ | ||
notifications: { | ||
toasts: { | ||
addError: jest.fn(), | ||
}, | ||
}, | ||
cloud: {}, | ||
data: { dataViews: { getFieldsForWildcard: jest.fn() } }, | ||
}), | ||
usePagination: jest.fn().mockReturnValue({ | ||
pagination: { | ||
currentPage: 1, | ||
pageSize: 5, | ||
}, | ||
pageSizeOptions: [5, 20, 50], | ||
setPagination: jest.fn(), | ||
}), | ||
useUrlParams: jest.fn().mockReturnValue({ urlParams: { kuery: '' } }), | ||
})); | ||
|
||
describe('useFetchAgentsData', () => { | ||
const startServices = useStartServices(); | ||
const mockErrorToast = startServices.notifications.toasts.addError as jest.Mock; | ||
|
||
beforeAll(() => { | ||
mockedExperimentalFeaturesService.get.mockReturnValue({ | ||
displayAgentMetrics: true, | ||
} as any); | ||
}); | ||
|
||
beforeEach(() => { | ||
mockErrorToast.mockReset(); | ||
mockErrorToast.mockResolvedValue({}); | ||
}); | ||
|
||
it('should fetch agents and agent policies data', async () => { | ||
let result: any | undefined; | ||
let waitForNextUpdate: any | undefined; | ||
await act(async () => { | ||
({ result, waitForNextUpdate } = renderHook(() => useFetchAgentsData())); | ||
await waitForNextUpdate(); | ||
}); | ||
|
||
expect(result?.current.selectedStatus).toEqual(['healthy', 'unhealthy', 'updating', 'offline']); | ||
expect(result?.current.agentPolicies).toEqual([ | ||
{ | ||
id: 'agent-policy-1', | ||
name: 'Agent policy 1', | ||
namespace: 'default', | ||
}, | ||
{ | ||
id: 'agent-policy-managed', | ||
managed: true, | ||
name: 'Managed Agent policy', | ||
namespace: 'default', | ||
}, | ||
]); | ||
|
||
expect(result?.current.agentPoliciesIndexedById).toEqual({ | ||
'agent-policy-1': { | ||
id: 'agent-policy-1', | ||
name: 'Agent policy 1', | ||
namespace: 'default', | ||
}, | ||
'agent-policy-managed': { | ||
id: 'agent-policy-managed', | ||
managed: true, | ||
name: 'Managed Agent policy', | ||
namespace: 'default', | ||
}, | ||
}); | ||
expect(result?.current.kuery).toEqual( | ||
'status:online or (status:error or status:degraded) or (status:updating or status:unenrolling or status:enrolling) or status:offline' | ||
); | ||
expect(result?.current.currentRequestRef).toEqual({ current: 1 }); | ||
expect(result?.current.pagination).toEqual({ currentPage: 1, pageSize: 5 }); | ||
expect(result?.current.pageSizeOptions).toEqual([5, 20, 50]); | ||
}); | ||
}); |
Oops, something went wrong.