Blame view

天文台pc/tianwentai-ui/node_modules/react-day-picker/src/components/MonthsDropdown/MonthsDropdown.test.tsx 3.33 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  import { screen } from '@testing-library/react';
  import { addMonths, differenceInMonths } from 'date-fns';
  import { DayPickerProps } from 'DayPicker';
  
  import { customRender } from 'test/render';
  import { user } from 'test/user';
  import { freezeBeforeAll } from 'test/utils';
  
  import { MonthsDropdown, MonthsDropdownProps } from './MonthsDropdown';
  
  const today = new Date(2020, 12, 22);
  
  freezeBeforeAll(today);
  
  let root: HTMLDivElement;
  let options: HTMLCollectionOf<HTMLOptionElement> | undefined;
  let select: HTMLSelectElement | null;
  
  function setup(props: MonthsDropdownProps, dayPickerProps?: DayPickerProps) {
    const view = customRender(<MonthsDropdown {...props} />, dayPickerProps);
    root = view.container.firstChild as HTMLDivElement;
    select = screen.queryByRole('combobox', { name: 'Month:' });
    options = select?.getElementsByTagName('option');
  }
  
  const props: MonthsDropdownProps = {
    displayMonth: today,
    onChange: jest.fn()
  };
  
  describe('when fromDate and toDate are passed in', () => {
    beforeEach(() => {
      setup(props, { fromDate: new Date(), toDate: addMonths(new Date(), 1) });
    });
    test('should render the dropdown element', () => {
      expect(root).toMatchSnapshot();
      expect(select).toHaveAttribute('name', 'months');
    });
  });
  
  describe('when "fromDate" is not set', () => {
    beforeEach(() => {
      setup(props, { fromDate: undefined });
    });
    test('should return nothing', () => {
      expect(root).toBeNull();
    });
  });
  
  describe('when "toDate" is not set', () => {
    beforeEach(() => {
      setup(props, { toDate: undefined });
    });
    test('should return nothing', () => {
      expect(root).toBeNull();
    });
  });
  
  describe('when "fromDate" and "toDate" are in the same year', () => {
    const fromDate = new Date(2012, 0, 22);
    const toDate = new Date(2012, 10, 22);
    beforeEach(() => {
      setup(props, { fromDate, toDate });
    });
    test('should display the months included between the two dates', () => {
      expect(options).toHaveLength(differenceInMonths(toDate, fromDate) + 1);
    });
    test('the first month should be the fromDate month', () => {
      expect(options?.[0]).toHaveValue(String(fromDate.getMonth()));
    });
    test('the last month should be the toMonth month', () => {
      expect(options?.[options.length - 1]).toHaveValue(
        String(toDate.getMonth())
      );
    });
  });
  
  describe('when "fromDate" and "toDate" are not in the same year', () => {
    const fromDate = new Date(2012, 0, 22);
    const toDate = new Date(2015, 10, 22);
    const displayMonth = new Date(2015, 7, 0);
    beforeEach(() => {
      setup({ ...props, displayMonth }, { fromDate, toDate });
    });
    test('should display the 12 months', () => {
      expect(options).toHaveLength(12);
    });
    test('the first month should be January', () => {
      expect(options?.[0]).toHaveValue('0');
    });
    test('the last month should be December', () => {
      expect(options?.[options.length - 1]).toHaveValue('11');
    });
    test('should select the displayed month', () => {
      expect(select).toHaveValue(`${displayMonth.getMonth()}`);
    });
  
    describe('when the dropdown changes', () => {
      beforeEach(async () => {
        if (select) await user.selectOptions(select, 'February');
      });
      test('should fire the "onChange" event handler', () => {
        const expectedMonth = new Date(2015, 1, 1);
        expect(props.onChange).toHaveBeenCalledWith(expectedMonth);
      });
    });
  });