Blame view

天文台pc/tianwentai-ui/node_modules/react-day-picker/src/components/Dropdown/Dropdown.test.tsx 1.94 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  import { fireEvent, screen } from '@testing-library/react';
  import { DayPickerProps } from 'DayPicker';
  
  import { customRender } from 'test/render';
  import { freezeBeforeAll } from 'test/utils';
  
  import { Dropdown, DropdownProps } from 'components/Dropdown';
  import { defaultClassNames } from 'contexts/DayPicker/defaultClassNames';
  import { CustomComponents } from 'types/DayPickerBase';
  
  const today = new Date(2021, 8);
  
  freezeBeforeAll(today);
  
  function setup(props: DropdownProps, dayPickerProps?: DayPickerProps) {
    customRender(<Dropdown {...props} />, dayPickerProps);
  }
  
  const props: Required<DropdownProps> = {
    name: 'dropdown',
    'aria-label': 'foo',
    onChange: jest.fn(),
    caption: 'Some caption',
    className: 'test',
    value: 'bar',
    children: <option value={'bar'} />,
    style: {}
  };
  
  describe('when rendered', () => {
    let combobox: HTMLElement;
    let label: HTMLElement;
  
    beforeEach(() => {
      setup(props);
      combobox = screen.getByRole('combobox');
      label = screen.getByText(props['aria-label']);
    });
  
    test('should render the vhidden aria label', () => {
      expect(label).toHaveClass(defaultClassNames.vhidden);
    });
  
    test('should render the combobox', () => {
      expect(combobox).toBeInTheDocument();
    });
  
    describe('when the combobox changes', () => {
      beforeEach(() => {
        fireEvent.change(combobox);
      });
      test('should call the "onChange" eve, nt handler', () => {
        expect(props.onChange).toHaveBeenCalled();
      });
    });
  
    test('should render the combobox with the given value', () => {
      expect(combobox).toHaveValue(props.value);
    });
  });
  
  describe('when using a custom IconDropdown component', () => {
    const components: CustomComponents = {
      IconDropdown: () => <div>Custom IconDropdown</div>
    };
    beforeEach(() => {
      setup(props, { components });
    });
    test('it should render the custom component instead', () => {
      expect(screen.getByText('Custom IconDropdown')).toBeInTheDocument();
    });
  });