selectBox.test.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import React from 'react';
  2. import { cleanup, fireEvent } from '@testing-library/react';
  3. import testWrapper from '../helpers/testWrapper';
  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 } = testWrapper(<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 } = testWrapper(<SelectBox options={options} />);
  28. fireEvent.mouseDown(
  29. container.querySelector('[data-testid="selected"]') as HTMLElement,
  30. );
  31. expect(container.querySelectorAll('span')[2].textContent).toBe('option 2');
  32. });
  33. test('use input box', () => {
  34. const { container } = testWrapper(<SelectBox options={options} useInput />);
  35. const inputNode = container.querySelector('input') as HTMLInputElement;
  36. expect(inputNode.value).toBe('option 1');
  37. });
  38. });