inputBox.test.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* eslint-disable no-undef */
  2. import React from 'react';
  3. import { render, cleanup, fireEvent } from '@testing-library/react';
  4. import '@testing-library/jest-dom';
  5. import InputBox from '../components/InputBox';
  6. import TextareaBox from '../components/TextareaBox';
  7. describe('Input Box component', () => {
  8. afterEach(cleanup);
  9. test('input element', () => {
  10. const { container } = render(<InputBox />);
  11. const inputNode = container.querySelector('input') as HTMLElement;
  12. expect(inputNode.tagName).toBe('INPUT');
  13. });
  14. test('textarea element', () => {
  15. const { container } = render(<TextareaBox />);
  16. const textAreaNode = container.querySelector('TextArea') as HTMLElement;
  17. expect(textAreaNode.tagName).toBe('TEXTAREA');
  18. });
  19. test('onChange event', () => {
  20. const { container } = render(<InputBox />);
  21. const inputNode = container.querySelector('input') as HTMLInputElement;
  22. fireEvent.change(inputNode, { target: { value: 'text' } });
  23. expect(inputNode.value).toBe('text');
  24. });
  25. test('check disabled input', () => {
  26. const handleChange = jest.fn();
  27. const { getByTestId } = render(
  28. <InputBox disabled onChange={handleChange} />,
  29. );
  30. fireEvent.change(getByTestId('input'), { target: { value: 'text' } });
  31. expect(handleChange).toBeCalledTimes(0);
  32. expect(getByTestId('input')).toBeDisabled();
  33. });
  34. });