Skip to content

Commit

Permalink
Add unit tests for workspace name and description field
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Wang <[email protected]>
  • Loading branch information
wanglam committed Aug 8, 2024
1 parent 0cf2fd8 commit b68bb41
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
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();
});
});
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();
});
});

0 comments on commit b68bb41

Please sign in to comment.