selectBox.test.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* eslint-disable import/no-extraneous-dependencies */
  2. /* eslint-disable no-undef */
  3. import React from 'react';
  4. import { render, cleanup, fireEvent } from '@testing-library/react';
  5. import SelectBox from '../components/SelectBox';
  6. describe('SelectBox component', () => {
  7. afterEach(cleanup);
  8. const options = [
  9. {
  10. key: 1,
  11. content: 'option 1',
  12. value: 1,
  13. },
  14. {
  15. key: 2,
  16. content: 'option 2',
  17. value: 2,
  18. },
  19. ];
  20. test('default value', () => {
  21. const { getAllByTestId } = render(<SelectBox options={options} />);
  22. const ele = getAllByTestId('selected')[0].querySelector(
  23. 'div'
  24. ) as HTMLDivElement;
  25. expect(ele.textContent).toBe('option 1');
  26. });
  27. test('open list', () => {
  28. const { container } = render(<SelectBox options={options} />);
  29. fireEvent.mouseDown(
  30. container.querySelector('[data-testid="selected"]') as HTMLElement
  31. );
  32. expect(container.querySelectorAll('span')[1].textContent).toBe('option 2');
  33. });
  34. test('use input box', () => {
  35. const { container } = render(<SelectBox options={options} useInput />);
  36. const inputNode = container.querySelector('input') as HTMLInputElement;
  37. expect(inputNode.value).toBe('option 1');
  38. });
  39. });