Blame view

天文台pc/tianwentai-ui/node_modules/react-day-picker/src/components/Caption/Caption.test.tsx 3.17 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
108
  import { screen } from '@testing-library/react';
  import { DayPickerProps } from 'DayPicker';
  
  import { customRender } from 'test/render';
  import {
    getMonthCaption,
    getMonthDropdown,
    getNextButton,
    getPrevButton,
    getYearDropdown,
    queryNextButton,
    queryPrevButton
  } from 'test/selectors';
  import { freezeBeforeAll } from 'test/utils';
  
  import { CustomComponents } from 'types/DayPickerBase';
  
  import { Caption, CaptionProps } from './Caption';
  
  const today = new Date(2021, 8);
  
  freezeBeforeAll(today);
  
  function setup(props: CaptionProps, dayPickerProps?: DayPickerProps) {
    customRender(<Caption {...props} />, dayPickerProps);
  }
  
  describe('when navigation is disabled', () => {
    const props = { displayMonth: today };
    const dayPickerProps = { disableNavigation: true };
    beforeEach(() => setup(props, dayPickerProps));
    test('should display the caption label', () => {
      expect(getMonthCaption()).toHaveTextContent('September 2021');
    });
    test('should not render the navigation', () => {
      expect(queryPrevButton()).toBeNull();
      expect(queryNextButton()).toBeNull();
    });
  });
  
  describe('when using a custom CaptionLabel component', () => {
    const components: CustomComponents = {
      CaptionLabel: () => <>custom label foo</>
    };
    const props = { displayMonth: today };
    beforeEach(() => {
      setup(props, { components });
    });
    test('it should render the custom component instead', () => {
      expect(screen.getByText('custom label foo')).toBeInTheDocument();
    });
  });
  
  describe('when the caption layout is "dropdown"', () => {
    const dayPickerProps: DayPickerProps = {
      captionLayout: 'dropdown',
      fromYear: 2020,
      toYear: 2025
    };
    const props = { displayMonth: today };
    beforeEach(() => {
      setup(props, dayPickerProps);
    });
    test('should render the month drop-down', () => {
      expect(getMonthDropdown()).toBeInTheDocument();
    });
    test('should render the year drop-down', () => {
      expect(getYearDropdown()).toBeInTheDocument();
    });
  });
  
  describe('when the caption layout is "buttons"', () => {
    const dayPickerProps: DayPickerProps = {
      captionLayout: 'buttons'
    };
    test('should render the next month button', () => {
      customRender(<Caption displayMonth={today} />, dayPickerProps);
      expect(getNextButton()).toBeInTheDocument();
    });
    test('should render the previous month button', () => {
      customRender(<Caption displayMonth={today} />, dayPickerProps);
      expect(getPrevButton()).toBeInTheDocument();
    });
  });
  
  describe('when the caption layout is "dropdown-buttons"', () => {
    const dayPickerProps: DayPickerProps = {
      captionLayout: 'dropdown-buttons',
      fromYear: 2020,
      toYear: 2025
    };
    const props = { displayMonth: today };
    beforeEach(() => {
      setup(props, dayPickerProps);
    });
    test('should render the month drop-down', () => {
      expect(getMonthDropdown()).toBeInTheDocument();
    });
    test('should render the year drop-down', () => {
      expect(getYearDropdown()).toBeInTheDocument();
    });
    test('should render the next month button', () => {
      expect(getNextButton()).toBeInTheDocument();
    });
    test('should render the previous month button', () => {
      expect(getPrevButton()).toBeInTheDocument();
    });
  });