Blame view

天文台pc/tianwentai-ui/node_modules/react-day-picker/src/components/Month/Month.test.tsx 6.23 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
  import { screen } from '@testing-library/react';
  import { DayPickerProps } from 'DayPicker';
  
  import { customRender } from 'test/render';
  import { getMonthCaption, getMonthGrid } from 'test/selectors';
  
  import { CustomComponents } from 'types/DayPickerBase';
  
  import { Month, MonthProps } from './Month';
  
  let root: HTMLDivElement;
  
  const displayMonth = new Date(2022, 10, 4);
  
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const testStyles: Record<string, any> = {
    caption_start: { color: 'red' },
    caption_end: { background: 'blue' },
    caption_between: { fontSize: 20 }
  };
  
  const testClassNames: Record<string, string> = {
    caption_start: 'caption_start',
    caption_end: 'caption_end',
    caption_between: 'caption_between'
  };
  
  type Test = {
    monthProps: MonthProps;
    dayPickerProps: DayPickerProps;
    expected: string[];
    notExpected: string[];
  };
  
  function setup(props: MonthProps, dayPickerProps?: DayPickerProps) {
    const view = customRender(<Month {...props} />, dayPickerProps);
    root = view.container.firstChild as HTMLDivElement;
  }
  describe('when rendered', () => {
    beforeEach(() => {
      setup({ displayIndex: 0, displayMonth });
    });
    test('the caption id should be the aria-labelledby of the grid', () => {
      const captionId = getMonthCaption().getAttribute('id');
      const gridLabelledBy = getMonthGrid().getAttribute('aria-labelledby');
      expect(captionId).toEqual(gridLabelledBy);
    });
  });
  
  describe('when rendered with a custom id', () => {
    const id = 'custom-id';
    beforeEach(() => {
      setup({ displayIndex: 0, displayMonth }, { id });
    });
    test('the caption id should include the display index', () => {
      const captionId = getMonthCaption().getAttribute('id');
      expect(captionId).toEqual('custom-id-0');
    });
  
    test('the table id should include the display index', () => {
      const tableId = getMonthGrid().getAttribute('id');
      expect(tableId).toEqual('custom-id-grid-0');
    });
  });
  
  describe('when using a custom Caption component', () => {
    const components: CustomComponents = {
      Caption: () => <>custom caption foo</>
    };
    beforeEach(() => {
      setup({ displayIndex: 0, displayMonth }, { components });
    });
    test('it should render the custom component instead', () => {
      expect(screen.getByText('custom caption foo')).toBeInTheDocument();
    });
  });
  
  describe('when dir is ltr', () => {
    const testLtr: Test[] = [
      {
        monthProps: {
          displayIndex: 0,
          displayMonth
        },
        dayPickerProps: {
          numberOfMonths: 1,
          styles: testStyles,
          classNames: testClassNames
        },
        expected: ['caption_start', 'caption_end'],
        notExpected: ['caption_between']
      },
      {
        monthProps: {
          displayIndex: 0,
          displayMonth
        },
        dayPickerProps: {
          numberOfMonths: 2,
          styles: testStyles,
          classNames: testClassNames
        },
        expected: ['caption_start'],
        notExpected: ['caption_between', 'caption_end']
      },
      {
        monthProps: {
          displayIndex: 1,
          displayMonth
        },
        dayPickerProps: {
          numberOfMonths: 2,
          styles: testStyles,
          classNames: testClassNames
        },
        expected: ['caption_end'],
        notExpected: ['caption_start', 'caption_between']
      },
      {
        monthProps: {
          displayIndex: 1,
          displayMonth
        },
        dayPickerProps: {
          numberOfMonths: 3,
          styles: testStyles,
          classNames: testClassNames
        },
        expected: ['caption_between'],
        notExpected: ['caption_start', 'caption_end']
      }
    ];
  
    describe.each(testLtr)(
      'when displayIndex is $monthProps.displayIndex and numberOfMonths is $dayPickerProps.numberOfMonths',
      ({ monthProps, dayPickerProps, expected, notExpected }) => {
        beforeEach(() => {
          setup(monthProps, dayPickerProps);
        });
        test.each(expected)(`the root should have the %s class`, (name) =>
          expect(root).toHaveClass(testClassNames[name])
        );
        test.each(expected)(`the root should have the %s style`, (name) =>
          expect(root).toHaveStyle(testStyles[name])
        );
        test.each(notExpected)(`the root should not have the %s class`, (name) =>
          expect(root).not.toHaveClass(testClassNames[name])
        );
      }
    );
  });
  
  describe('when dir is rtl', () => {
    const testRtl: Test[] = [
      {
        monthProps: {
          displayIndex: 0,
          displayMonth
        },
        dayPickerProps: {
          dir: 'rtl',
          numberOfMonths: 1,
          styles: testStyles,
          classNames: testClassNames
        },
        expected: ['caption_start', 'caption_end'],
        notExpected: ['caption_between']
      },
      {
        monthProps: {
          displayIndex: 0,
          displayMonth
        },
        dayPickerProps: {
          dir: 'rtl',
          numberOfMonths: 2,
          styles: testStyles,
          classNames: testClassNames
        },
        expected: ['caption_end'],
        notExpected: ['caption_between', 'caption_start']
      },
      {
        monthProps: {
          displayIndex: 1,
          displayMonth
        },
        dayPickerProps: {
          dir: 'rtl',
          numberOfMonths: 2,
          styles: testStyles,
          classNames: testClassNames
        },
        expected: ['caption_start'],
        notExpected: ['caption_end', 'caption_between']
      },
      {
        monthProps: {
          displayIndex: 1,
          displayMonth
        },
        dayPickerProps: {
          dir: 'rtl',
          numberOfMonths: 3,
          styles: testStyles,
          classNames: testClassNames
        },
        expected: ['caption_between'],
        notExpected: ['caption_start', 'caption_end']
      }
    ];
  
    describe.each(testRtl)(
      'when displayIndex is $monthProps.displayIndex and numberOfMonths is $dayPickerProps.numberOfMonths',
      ({ monthProps, dayPickerProps, expected, notExpected }) => {
        beforeEach(() => {
          setup(monthProps, dayPickerProps);
        });
        test.each(expected)(`the root should have the %s class`, (name) =>
          expect(root).toHaveClass(testClassNames[name])
        );
        test.each(expected)(`the root should have the %s style`, (name) =>
          expect(root).toHaveStyle(testStyles[name])
        );
        test.each(notExpected)(`the root should not have the %s class`, (name) =>
          expect(root).not.toHaveClass(testClassNames[name])
        );
      }
    );
  });