-
Notifications
You must be signed in to change notification settings - Fork 891
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for workspace name and description field
Signed-off-by: Lin Wang <[email protected]>
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...ns/workspace/public/components/workspace_form/fields/workspace_description_field.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,47 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
|
||
import { MAX_WORKSPACE_DESCRIPTION_LENGTH } from '../../../../common/constants'; | ||
import { WorkspaceDescriptionField } from './workspace_description_field'; | ||
|
||
describe('<WorkspaceDescriptionField />', () => { | ||
it('should call onChange when the new value is within MAX_WORKSPACE_DESCRIPTION_LENGTH', () => { | ||
const onChangeMock = jest.fn(); | ||
const value = 'test'; | ||
|
||
render(<WorkspaceDescriptionField value={value} onChange={onChangeMock} />); | ||
|
||
const textarea = screen.getByPlaceholderText('Describe the workspace'); | ||
fireEvent.change(textarea, { target: { value: 'new value' } }); | ||
|
||
expect(onChangeMock).toHaveBeenCalledWith('new value'); | ||
}); | ||
|
||
it('should not call onChange when the new value exceeds MAX_WORKSPACE_DESCRIPTION_LENGTH', () => { | ||
const onChangeMock = jest.fn(); | ||
const value = 'a'.repeat(MAX_WORKSPACE_DESCRIPTION_LENGTH); | ||
|
||
render(<WorkspaceDescriptionField value={value} onChange={onChangeMock} />); | ||
|
||
const textarea = screen.getByPlaceholderText('Describe the workspace'); | ||
fireEvent.change(textarea, { | ||
target: { value: 'a'.repeat(MAX_WORKSPACE_DESCRIPTION_LENGTH + 1) }, | ||
}); | ||
|
||
expect(onChangeMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should render the correct number of characters left when value is falsy', () => { | ||
render(<WorkspaceDescriptionField value={undefined} onChange={jest.fn()} />); | ||
|
||
const helpText = screen.getByText( | ||
new RegExp(`${MAX_WORKSPACE_DESCRIPTION_LENGTH}.+characters left\.`) | ||
); | ||
expect(helpText).toBeInTheDocument(); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
src/plugins/workspace/public/components/workspace_form/fields/workspace_name_field.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,45 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
|
||
import { MAX_WORKSPACE_NAME_LENGTH } from '../../../../common/constants'; | ||
import { WorkspaceNameField } from './workspace_name_field'; | ||
|
||
describe('<WorkspaceNameField />', () => { | ||
it('should call onChange when the new value is within MAX_WORKSPACE_NAME_LENGTH', () => { | ||
const onChangeMock = jest.fn(); | ||
const value = 'test'; | ||
|
||
render(<WorkspaceNameField value={value} onChange={onChangeMock} />); | ||
|
||
const input = screen.getByPlaceholderText('Enter a name'); | ||
fireEvent.change(input, { target: { value: 'new value' } }); | ||
|
||
expect(onChangeMock).toHaveBeenCalledWith('new value'); | ||
}); | ||
|
||
it('should not call onChange when the new value exceeds MAX_WORKSPACE_NAME_LENGTH', () => { | ||
const onChangeMock = jest.fn(); | ||
const value = 'a'.repeat(MAX_WORKSPACE_NAME_LENGTH); | ||
|
||
render(<WorkspaceNameField value={value} onChange={onChangeMock} />); | ||
|
||
const input = screen.getByPlaceholderText('Enter a name'); | ||
fireEvent.change(input, { target: { value: 'a'.repeat(MAX_WORKSPACE_NAME_LENGTH + 1) } }); | ||
|
||
expect(onChangeMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should render the correct number of characters left when value is falsy', () => { | ||
render(<WorkspaceNameField value={undefined} onChange={jest.fn()} />); | ||
|
||
const helpText = screen.getByText( | ||
new RegExp(`${MAX_WORKSPACE_NAME_LENGTH}.+characters left\.`) | ||
); | ||
expect(helpText).toBeInTheDocument(); | ||
}); | ||
}); |