Input.test.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import React from 'react';
  2. import '@testing-library/jest-dom/extend-expect';
  3. import { Input } from './Input';
  4. import { fireEvent, render, waitFor, screen } from '../../../../../tests/test-utils';
  5. describe('Input', () => {
  6. it('should render without errors', () => {
  7. // arrange
  8. const { container } = render(<Input name="test-input" />);
  9. // assert
  10. expect(container).toBeTruthy();
  11. });
  12. it('should render the label if provided', () => {
  13. // arrange
  14. render(<Input name="test-input" label="Test Label" />);
  15. const input = screen.getByLabelText('Test Label');
  16. // assert
  17. expect(input).toBeInTheDocument();
  18. });
  19. it('should render the placeholder if provided', () => {
  20. // arrange
  21. render(<Input name="test-input" placeholder="Test Placeholder" />);
  22. // assert
  23. const input = screen.getByPlaceholderText('Test Placeholder');
  24. expect(input).toBeInTheDocument();
  25. });
  26. it('should render the error message if provided', () => {
  27. // arrange
  28. render(<Input name="test-input" error="Test Error" />);
  29. // assert
  30. const error = screen.getByText('Test Error');
  31. expect(error).toBeInTheDocument();
  32. });
  33. it('should call onChange when the input value is changed', async () => {
  34. // arrange
  35. const onChange = jest.fn();
  36. render(<Input name="test-input" label="Test Label" onChange={onChange} />);
  37. const input = screen.getByLabelText('Test Label');
  38. // act
  39. fireEvent.change(input, { target: { value: 'changed' } });
  40. // assert
  41. await waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
  42. });
  43. it('should call onBlur when the input is blurred', async () => {
  44. // arrange
  45. const onBlur = jest.fn();
  46. render(<Input name="test-input" label="Test Label" onBlur={onBlur} />);
  47. const input = screen.getByLabelText('Test Label');
  48. // act
  49. fireEvent.blur(input);
  50. // assert
  51. await waitFor(() => expect(onBlur).toHaveBeenCalledTimes(1));
  52. });
  53. it('should set the input type if provided', () => {
  54. // arrange
  55. render(<Input name="test-input" label="Test Label" type="password" />);
  56. const input = screen.getByLabelText('Test Label') as HTMLInputElement;
  57. // assert
  58. expect(input.type).toBe('password');
  59. });
  60. it('should set the input value if provided', () => {
  61. // arrange
  62. render(<Input name="test-input" label="Test Label" value="Test Value" onChange={jest.fn} />);
  63. const input = screen.getByLabelText('Test Label') as HTMLInputElement;
  64. // assert
  65. expect(input.value).toBe('Test Value');
  66. });
  67. it('should apply the isInvalid prop to the input element', () => {
  68. // arrange
  69. render(<Input name="test-input" label="Test Label" isInvalid />);
  70. const input = screen.getByLabelText('Test Label');
  71. // assert
  72. expect(input).toHaveClass('is-invalid', 'is-invalid-lite');
  73. });
  74. it('should apply the disabled prop to the input element', () => {
  75. // arrange
  76. render(<Input name="test-input" label="Test Label" disabled />);
  77. // assert
  78. const input = screen.getByLabelText('Test Label');
  79. expect(input).toBeDisabled();
  80. });
  81. it('should set the input name attribute if provided', () => {
  82. // arrange
  83. render(<Input name="test-input" label="Test Label" />);
  84. const input = screen.getByLabelText('Test Label');
  85. // assert
  86. expect(input).toHaveAttribute('name', 'test-input');
  87. });
  88. it('should set the input id attribute if provided', () => {
  89. // arrange
  90. render(<Input name="test-input" label="Test Label" />);
  91. const input = screen.getByLabelText('Test Label');
  92. // assert
  93. expect(input).toHaveAttribute('id', 'test-input');
  94. });
  95. it('should set the input ref if provided', () => {
  96. // arrange
  97. const ref = React.createRef<HTMLInputElement>();
  98. render(<Input name="test-input" label="Test Label" ref={ref} />);
  99. const input = screen.getByLabelText('Test Label');
  100. // assert
  101. expect(input).toEqual(ref.current);
  102. });
  103. it('should set the input type attribute to "text" if not provided or if an invalid value is provided', () => {
  104. // arrange
  105. render(<Input name="test-input" label="Test Label" />);
  106. const input = screen.getByLabelText('Test Label') as HTMLInputElement;
  107. // assert
  108. expect(input.type).toBe('text');
  109. });
  110. it('should set the input placeholder attribute if provided', () => {
  111. // arrange
  112. render(<Input name="test-input" label="Test Label" placeholder="Test Placeholder" />);
  113. const input = screen.getByLabelText('Test Label');
  114. // assert
  115. expect(input).toHaveAttribute('placeholder', 'Test Placeholder');
  116. });
  117. });