selectBox.test.tsx 1.2 KB

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