Skip to content

Commit

Permalink
InputText Jest tests (#3581)
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware authored Nov 4, 2022
1 parent 880803e commit 7af6366
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 3 deletions.
2 changes: 1 addition & 1 deletion components/doc/chips/apidoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export function ApiDoc(props) {
</table>
</div>
</DevelopmentSection>

<h4>Dependencies</h4>
<p>None.</p>
</>
Expand Down
2 changes: 1 addition & 1 deletion components/doc/chips/templatedoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default function TemplateDemo() {
A chip is customized using <i>itemTemplate</i> function where value is passed to return JSX.
</DocSectionText>
<div className="card p-fluid">
<Chips value={value} onChange={(e) => setValue(e.value)} itemTemplate={customChip}/>
<Chips value={value} onChange={(e) => setValue(e.value)} itemTemplate={customChip} />
</div>
<DocSectionCode code={code} />
</>
Expand Down
1 change: 1 addition & 0 deletions components/lib/button/Button.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ describe('Button', () => {
fireEvent.click(button);

// Assert
expect(button).toBeDisabled();
expect(clickOn).toHaveBeenCalledTimes(0);
});
});
113 changes: 113 additions & 0 deletions components/lib/inputtext/InputText.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import '@testing-library/jest-dom';
import { fireEvent, render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { InputText } from './InputText';

describe('InputText', () => {
test('when input is enabled then input accepts data entry and have filled state', () => {
// Arrange
const { container } = render(<InputText />);
const input = container.getElementsByTagName('input')[0];

// Act
fireEvent.input(input, { target: { value: 'abc' } });

// Act
expect(input).toBeEnabled();
expect(input).toHaveClass('p-inputtext p-component p-filled');
expect(input).toHaveValue('abc');
});
test('when input is blank it should not have filled state', () => {
// Arrange
const { container } = render(<InputText disabled={false} />);
const input = container.getElementsByTagName('input')[0];

// Act
fireEvent.input(input, { target: { value: '' } });

// Act
expect(input).toBeEnabled();
expect(input).toHaveClass('p-inputtext p-component');
expect(input).not.toHaveClass('p-filled');
expect(input).toHaveValue('');
});
test('when input is is set for validation only', () => {
// Arrange
const { container } = render(<InputText validateOnly keyfilter={`alpha`} />);
const input = container.getElementsByTagName('input')[0];

// Act
fireEvent.input(input, { target: { value: 'def' } });

// Act
expect(input).toBeEnabled();
expect(input).toHaveClass('p-inputtext p-component');
expect(input).toHaveValue('def');
});
test('when input is disabled it should render as disabled', () => {
// Arrange
const { container } = render(<InputText disabled />);
const input = container.getElementsByTagName('input')[0];

// Act
fireEvent.input(input, { target: { value: '23' } });

// Act
expect(input).toBeDisabled();
expect(input).toHaveClass('p-inputtext p-component p-disabled');
});
test('when input is using keyfilter for integers accept integer input', async () => {
// Arrange
const keydownOn = jest.fn();
const { container } = render(<InputText keyfilter="int" onKeyDown={keydownOn} />);
const input = container.getElementsByTagName('input')[0];

// Act
await userEvent.type(input, '123');

// Act
expect(input).toHaveValue('123');
expect(keydownOn).toHaveBeenCalledTimes(3);
});
test('when input is using keyfilter for integers do not accept alphabetic input', async () => {
// Arrange
const keydownOn = jest.fn();
const { container } = render(<InputText keyfilter="int" onKeyDown={keydownOn} />);
const input = container.getElementsByTagName('input')[0];

// Act
await userEvent.type(input, 'abc');

// Act
expect(input).toHaveValue('');
expect(keydownOn).toHaveBeenCalledTimes(3);
});
test('when input is using keyfilter for alphabetic accept paste of alphabetic values', async () => {
// Arrange
const pasteOn = jest.fn();
const { container } = render(<InputText keyfilter="alpha" onPaste={pasteOn} />);
const input = container.getElementsByTagName('input')[0];

// Act
input.focus();
await userEvent.paste('abc');

// Act
expect(input).toHaveValue('abc');
expect(pasteOn).toHaveBeenCalledTimes(1);
});
test('when input is using keyfilter for alphabetic do not accept paste of integer values', async () => {
// Arrange
const pasteOn = jest.fn();
const { container } = render(<InputText keyfilter="alpha" onPaste={pasteOn} />);
const input = container.getElementsByTagName('input')[0];

// Act
input.focus();
await userEvent.paste('123');

// Act
expect(input).toHaveValue('');
expect(pasteOn).toHaveBeenCalledTimes(1);
});
});
4 changes: 3 additions & 1 deletion components/lib/keyfilter/KeyFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ export const KeyFilter = {
let value = e.target.value,
validatePattern = true;

if (value && !keyfilter.test(value)) {
const regex = this.getRegex(keyfilter);

if (value && !regex.test(value)) {
validatePattern = false;
}

Expand Down

0 comments on commit 7af6366

Please sign in to comment.