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] Improve space selector validation when not providing valid sp…
…ace (elastic#197117)
- Loading branch information
Showing
6 changed files
with
176 additions
and
46 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
90 changes: 90 additions & 0 deletions
90
...eet/sections/agent_policy/components/agent_policy_advanced_fields/space_selector.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,90 @@ | ||
/* | ||
* 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 React from 'react'; | ||
import { fireEvent } from '@testing-library/react'; | ||
|
||
import { useAgentPoliciesSpaces } from '../../../../../../hooks/use_request/spaces'; | ||
|
||
import { createFleetTestRendererMock } from '../../../../../../mock'; | ||
|
||
import { SpaceSelector } from './space_selector'; | ||
|
||
jest.mock('../../../../../../hooks/use_request/spaces'); | ||
|
||
describe('Space Selector', () => { | ||
beforeEach(() => { | ||
jest.mocked(useAgentPoliciesSpaces).mockReturnValue({ | ||
data: { | ||
items: [ | ||
{ | ||
name: 'Default', | ||
id: 'default', | ||
}, | ||
{ | ||
name: 'Test', | ||
id: 'test', | ||
}, | ||
], | ||
}, | ||
} as any); | ||
}); | ||
function render() { | ||
const renderer = createFleetTestRendererMock(); | ||
const onChange = jest.fn(); | ||
const setInvalidSpaceError = jest.fn(); | ||
const result = renderer.render( | ||
<SpaceSelector setInvalidSpaceError={setInvalidSpaceError} onChange={onChange} value={[]} /> | ||
); | ||
|
||
return { | ||
result, | ||
onChange, | ||
setInvalidSpaceError, | ||
}; | ||
} | ||
|
||
it('should render invalid space errors', () => { | ||
const { result, onChange, setInvalidSpaceError } = render(); | ||
const inputEl = result.getByTestId('comboBoxSearchInput'); | ||
fireEvent.change(inputEl, { | ||
target: { value: 'invalidSpace' }, | ||
}); | ||
fireEvent.keyDown(inputEl, { key: 'Enter', code: 'Enter' }); | ||
expect(result.container).toHaveTextContent('invalidSpace is not a valid space.'); | ||
expect(onChange).not.toBeCalled(); | ||
expect(setInvalidSpaceError).toBeCalledWith(true); | ||
}); | ||
|
||
it('should clear invalid space errors', () => { | ||
const { result, setInvalidSpaceError } = render(); | ||
const inputEl = result.getByTestId('comboBoxSearchInput'); | ||
fireEvent.change(inputEl, { | ||
target: { value: 'invalidSpace' }, | ||
}); | ||
fireEvent.keyDown(inputEl, { key: 'Enter', code: 'Enter' }); | ||
expect(result.container).toHaveTextContent('invalidSpace is not a valid space.'); | ||
fireEvent.change(inputEl, { | ||
target: { value: '' }, | ||
}); | ||
fireEvent.keyDown(inputEl, { key: 'Enter', code: 'Enter' }); | ||
expect(result.container).not.toHaveTextContent('invalidSpace is not a valid space.'); | ||
expect(setInvalidSpaceError).toBeCalledWith(false); | ||
}); | ||
|
||
it('should accept valid space', () => { | ||
const { result, onChange, setInvalidSpaceError } = render(); | ||
const inputEl = result.getByTestId('comboBoxSearchInput'); | ||
fireEvent.change(inputEl, { | ||
target: { value: 'test' }, | ||
}); | ||
fireEvent.keyDown(inputEl, { key: 'Enter', code: 'Enter' }); | ||
expect(result.container).not.toHaveTextContent('test is not a valid space.'); | ||
expect(onChange).toBeCalledWith(['test']); | ||
expect(setInvalidSpaceError).not.toBeCalledWith(true); | ||
}); | ||
}); |
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
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
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
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