From 7f3f903b32f26e696e94252f649eb24198142c62 Mon Sep 17 00:00:00 2001 From: Melloware Date: Fri, 4 Nov 2022 15:38:20 -0400 Subject: [PATCH] Fix #3582: InputTextarea Jest test (#3584) --- .../lib/inputtextarea/InputTextarea.spec.js | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 components/lib/inputtextarea/InputTextarea.spec.js diff --git a/components/lib/inputtextarea/InputTextarea.spec.js b/components/lib/inputtextarea/InputTextarea.spec.js new file mode 100644 index 0000000000..261884e46f --- /dev/null +++ b/components/lib/inputtextarea/InputTextarea.spec.js @@ -0,0 +1,130 @@ +import '@testing-library/jest-dom'; +import { fireEvent, render } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { InputTextarea } from './InputTextarea'; + +describe('InputTextarea', () => { + test('when textarea is enabled then textarea accepts data entry and have filled state', () => { + // Arrange + const { container } = render(); + const input = container.getElementsByTagName('textarea')[0]; + + // Act + fireEvent.input(input, { target: { value: 'abc' } }); + + // Act + expect(input).toBeEnabled(); + expect(input).toHaveClass('p-inputtextarea p-inputtext p-component p-filled'); + expect(input).toHaveValue('abc'); + }); + test('when textarea is is autosizing and has rows and columns it is rendered correctly', () => { + // Arrange + const blurOn = jest.fn(); + const { container } = render(); + const input = container.getElementsByTagName('textarea')[0]; + + // Act + fireEvent.blur(input); + + // Act and Assert + expect(input).toHaveClass('p-inputtextarea p-inputtext p-component p-inputtextarea-resizable'); + expect(input.getAttribute('rows')).toBe('3'); + expect(input.getAttribute('cols')).toBe('12'); + expect(blurOn).toHaveBeenCalledTimes(1); + }); + test('when input is blank it should not have filled state', () => { + // Arrange + const { container } = render(); + const input = container.getElementsByTagName('textarea')[0]; + + // Act + fireEvent.input(input, { target: { value: '' } }); + + // Act + expect(input).toBeEnabled(); + expect(input).toHaveClass('p-inputtextarea 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(); + const input = container.getElementsByTagName('textarea')[0]; + + // Act + fireEvent.input(input, { target: { value: 'def' } }); + + // Act + expect(input).toBeEnabled(); + expect(input).toHaveClass('p-inputtextarea p-inputtext p-component'); + expect(input).toHaveValue('def'); + }); + test('when input is disabled it should render as disabled', () => { + // Arrange + const { container } = render(); + const input = container.getElementsByTagName('textarea')[0]; + + // Act + fireEvent.input(input, { target: { value: '23' } }); + + // Act + expect(input).toBeDisabled(); + expect(input).toHaveClass('p-inputtextarea p-inputtext p-component p-disabled'); + }); + test('when input is using keyfilter for integers accept integer input', async () => { + // Arrange + const keydownOn = jest.fn(); + const keyupOn = jest.fn(); + const { container } = render(); + const input = container.getElementsByTagName('textarea')[0]; + + // Act + await userEvent.type(input, '123'); + + // Act + expect(input).toHaveValue('123'); + expect(keydownOn).toHaveBeenCalledTimes(3); + expect(keyupOn).toHaveBeenCalledTimes(3); + }); + test('when input is using keyfilter for integers do not accept alphabetic input', async () => { + // Arrange + const keydownOn = jest.fn(); + const { container } = render(); + const input = container.getElementsByTagName('textarea')[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(); + const input = container.getElementsByTagName('textarea')[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(); + const input = container.getElementsByTagName('textarea')[0]; + + // Act + input.focus(); + await userEvent.paste('123'); + + // Act + expect(input).toHaveValue(''); + expect(pasteOn).toHaveBeenCalledTimes(1); + }); +});