selectBox.test.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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('div') as HTMLDivElement;
  23. expect(ele.textContent).toBe('option 1');
  24. });
  25. test('open list', () => {
  26. const { container } = render(<SelectBox options={options} />);
  27. fireEvent.mouseDown(container.querySelector('[data-testid="selected"]') as HTMLElement);
  28. expect(container.querySelectorAll('span')[1].textContent).toBe('option 2');
  29. });
  30. test('use input box', () => {
  31. const { container } = render(<SelectBox options={options} useInput />);
  32. const inputNode = container.querySelector('input') as HTMLInputElement;
  33. expect(inputNode.value).toBe('option 1');
  34. });
  35. });