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.
added jest test for agent enrollment flyout steps behaviour
- Loading branch information
1 parent
e2217dc
commit 660930b
Showing
3 changed files
with
220 additions
and
4 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
.../sections/agents/components/agent_enrollment_flyout/agent_enrollment_flyout.test.mocks.ts
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,44 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
jest.mock('../../../../../../hooks/use_request', () => { | ||
const module = jest.requireActual('../../../../../../hooks/use_request'); | ||
return { | ||
...module, | ||
useGetSettings: jest.fn(), | ||
sendGetFleetStatus: jest.fn(), | ||
}; | ||
}); | ||
|
||
jest.mock('../../agent_requirements_page', () => { | ||
const module = jest.requireActual('../../agent_requirements_page'); | ||
return { | ||
...module, | ||
FleetServerRequirementPage: jest.fn(), | ||
useFleetServerInstructions: jest.fn(), | ||
}; | ||
}); | ||
|
||
/** | ||
* These steps functions use hooks inside useMemo which is not compatible with jest currently | ||
*/ | ||
jest.mock('./steps', () => { | ||
const module = jest.requireActual('./steps'); | ||
return { | ||
...module, | ||
AgentPolicySelectionStep: jest.fn(), | ||
AgentEnrollmentKeySelectionStep: jest.fn(), | ||
}; | ||
}); | ||
|
||
jest.mock('@elastic/eui', () => { | ||
const module = jest.requireActual('@elastic/eui'); | ||
return { | ||
...module, | ||
EuiSteps: 'eui-steps', | ||
}; | ||
}); |
164 changes: 164 additions & 0 deletions
164
...fleet/sections/agents/components/agent_enrollment_flyout/agent_enrollment_flyout.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,164 @@ | ||
/* | ||
* 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 './agent_enrollment_flyout.test.mocks'; | ||
|
||
import React from 'react'; | ||
import { registerTestBed } from '@kbn/test/jest'; | ||
import { act } from '@testing-library/react'; | ||
|
||
import { coreMock } from 'src/core/public/mocks'; | ||
|
||
import { KibanaContextProvider } from '../../../../../../../../../../src/plugins/kibana_react/public'; | ||
|
||
import type { AgentPolicy } from '../../../../../../../common'; | ||
import { useGetSettings, sendGetFleetStatus } from '../../../../../../hooks/use_request'; | ||
import { FleetStatusProvider, ConfigContext } from '../../../../../../hooks'; | ||
|
||
import { useFleetServerInstructions } from '../../agent_requirements_page'; | ||
|
||
import { AgentEnrollmentKeySelectionStep, AgentPolicySelectionStep } from './steps'; | ||
|
||
import type { Props } from '.'; | ||
import { AgentEnrollmentFlyout } from '.'; | ||
|
||
const TestComponent = (props: Props) => ( | ||
<KibanaContextProvider services={coreMock.createStart()}> | ||
<ConfigContext.Provider value={{ agents: { enabled: true, elasticsearch: {} }, enabled: true }}> | ||
<FleetStatusProvider> | ||
<AgentEnrollmentFlyout {...props} /> | ||
</FleetStatusProvider> | ||
</ConfigContext.Provider> | ||
</KibanaContextProvider> | ||
); | ||
|
||
const setup = async (props?: Props) => { | ||
const testBed = await registerTestBed(TestComponent)(props); | ||
const { find, component } = testBed; | ||
|
||
return { | ||
...testBed, | ||
actions: { | ||
goToStandaloneTab: () => | ||
act(() => { | ||
find('agentEnrollmentFlyout.standaloneTab').simulate('click'); | ||
component.update(); | ||
}), | ||
}, | ||
}; | ||
}; | ||
|
||
type SetupReturn = ReturnType<typeof setup>; | ||
type TestBed = SetupReturn extends Promise<infer U> ? U : SetupReturn; | ||
|
||
const testAgentPolicy: AgentPolicy = { | ||
id: 'test', | ||
is_managed: false, | ||
namespace: 'test', | ||
package_policies: [], | ||
revision: 1, | ||
status: 'active', | ||
updated_at: 'test', | ||
updated_by: 'test', | ||
name: 'test', | ||
}; | ||
|
||
describe('<AgentEnrollmentFlyout />', () => { | ||
let testBed: TestBed; | ||
|
||
beforeEach(async () => { | ||
(useGetSettings as jest.Mock).mockReturnValue({ | ||
data: { item: { fleet_server_hosts: ['test'] } }, | ||
}); | ||
|
||
(sendGetFleetStatus as jest.Mock).mockResolvedValue({ | ||
data: { isReady: true }, | ||
}); | ||
|
||
(useFleetServerInstructions as jest.Mock).mockReturnValue({ | ||
serviceToken: 'test', | ||
getServiceToken: jest.fn(), | ||
isLoadingServiceToken: false, | ||
installCommand: jest.fn(), | ||
platform: 'test', | ||
setPlatform: jest.fn(), | ||
}); | ||
|
||
await act(async () => { | ||
testBed = await setup({ | ||
agentPolicies: [], | ||
onClose: jest.fn(), | ||
}); | ||
testBed.component.update(); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('managed instructions', () => { | ||
it('uses the agent policy selection step', async () => { | ||
const { exists } = testBed; | ||
expect(exists('agentEnrollmentFlyout')).toBe(true); | ||
expect(AgentPolicySelectionStep).toHaveBeenCalled(); | ||
expect(AgentEnrollmentKeySelectionStep).not.toHaveBeenCalled(); | ||
}); | ||
|
||
describe('with a specific policy', () => { | ||
beforeEach(async () => { | ||
jest.clearAllMocks(); | ||
await act(async () => { | ||
testBed = await setup({ | ||
agentPolicy: testAgentPolicy, | ||
onClose: jest.fn(), | ||
}); | ||
testBed.component.update(); | ||
}); | ||
}); | ||
|
||
it('uses the configure enrollment step, not the agent policy selection step', () => { | ||
const { exists } = testBed; | ||
expect(exists('agentEnrollmentFlyout')).toBe(true); | ||
expect(AgentPolicySelectionStep).not.toHaveBeenCalled(); | ||
expect(AgentEnrollmentKeySelectionStep).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('standalone instructions', () => { | ||
it('uses the agent policy selection step', async () => { | ||
const { exists, actions } = testBed; | ||
actions.goToStandaloneTab(); | ||
expect(exists('agentEnrollmentFlyout')).toBe(true); | ||
expect(AgentPolicySelectionStep).toHaveBeenCalled(); | ||
expect(AgentEnrollmentKeySelectionStep).not.toHaveBeenCalled(); | ||
}); | ||
|
||
describe('with a specific policy', () => { | ||
beforeEach(async () => { | ||
jest.clearAllMocks(); | ||
await act(async () => { | ||
testBed = await setup({ | ||
agentPolicy: testAgentPolicy, | ||
onClose: jest.fn(), | ||
}); | ||
testBed.component.update(); | ||
}); | ||
}); | ||
|
||
it('does not use either of the agent policy selection or enrollment key steps', () => { | ||
const { exists, actions } = testBed; | ||
jest.clearAllMocks(); | ||
expect(exists('agentEnrollmentFlyout')).toBe(true); | ||
actions.goToStandaloneTab(); | ||
expect(AgentPolicySelectionStep).not.toHaveBeenCalled(); | ||
expect(AgentEnrollmentKeySelectionStep).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
}); |
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