You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InputSearch-test.tsx 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { screen, waitFor } from '@testing-library/react';
  21. import { renderWithContext } from '../../../helpers/testUtils';
  22. import { FCProps } from '../../../types/misc';
  23. import { InputSearch } from '../InputSearch';
  24. it('should warn when input is too short', async () => {
  25. const { user } = setupWithProps({ value: 'f' });
  26. expect(screen.getByRole('note')).toBeInTheDocument();
  27. await user.type(screen.getByRole('searchbox'), 'oo');
  28. expect(screen.queryByRole('note')).not.toBeInTheDocument();
  29. });
  30. it('should show clear button only when there is a value', async () => {
  31. const { user } = setupWithProps({ value: 'f' });
  32. expect(screen.getByRole('button')).toBeInTheDocument();
  33. await user.clear(screen.getByRole('searchbox'));
  34. expect(screen.queryByRole('button')).not.toBeInTheDocument();
  35. });
  36. it('should attach ref', () => {
  37. const ref = jest.fn() as jest.Mock<unknown, unknown[]>;
  38. setupWithProps({ innerRef: ref });
  39. expect(ref).toHaveBeenCalled();
  40. expect(ref.mock.calls[0][0]).toBeInstanceOf(HTMLInputElement);
  41. });
  42. it('should trigger reset correctly with clear button', async () => {
  43. const onChange = jest.fn();
  44. const { user } = setupWithProps({ onChange });
  45. await user.click(screen.getByRole('button'));
  46. expect(onChange).toHaveBeenCalledWith('');
  47. });
  48. it('should trigger change correctly', async () => {
  49. const onChange = jest.fn();
  50. const { user } = setupWithProps({ onChange, value: 'f' });
  51. await user.type(screen.getByRole('searchbox'), 'oo');
  52. await waitFor(() => {
  53. expect(onChange).toHaveBeenCalledWith('foo');
  54. });
  55. });
  56. it('should not change when value is too short', async () => {
  57. const onChange = jest.fn();
  58. const { user } = setupWithProps({ onChange, value: '', minLength: 3 });
  59. await user.type(screen.getByRole('searchbox'), 'fo');
  60. expect(onChange).not.toHaveBeenCalled();
  61. });
  62. it('should clear input using escape', async () => {
  63. const onChange = jest.fn();
  64. const { user } = setupWithProps({ onChange, value: 'foo' });
  65. await user.type(screen.getByRole('searchbox'), '{Escape}');
  66. expect(onChange).toHaveBeenCalledWith('');
  67. });
  68. function setupWithProps(props: Partial<FCProps<typeof InputSearch>> = {}) {
  69. return renderWithContext(
  70. <InputSearch
  71. maxLength={150}
  72. minLength={2}
  73. onChange={jest.fn()}
  74. placeholder="placeholder"
  75. searchInputAriaLabel=""
  76. value="foo"
  77. {...props}
  78. />,
  79. );
  80. }