Blame view

天文台pc/tianwentai-ui/node_modules/react-day-picker/src/contexts/Navigation/utils/getInitialMonth.test.ts 1.88 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
  import { addMonths, isSameMonth } from 'date-fns';
  
  import { getInitialMonth } from './getInitialMonth';
  
  describe('when no toDate is given', () => {
    describe('when month is in context', () => {
      const month = new Date(2010, 11, 12);
      it('return that month', () => {
        const initialMonth = getInitialMonth({ month });
        expect(isSameMonth(initialMonth, month)).toBe(true);
      });
    });
    describe('when defaultMonth is in context', () => {
      const defaultMonth = new Date(2010, 11, 12);
      it('return that month', () => {
        const initialMonth = getInitialMonth({ defaultMonth });
        expect(isSameMonth(initialMonth, defaultMonth)).toBe(true);
      });
    });
    describe('when no month or defaultMonth are in context', () => {
      const today = new Date(2010, 11, 12);
      it('return the today month', () => {
        const initialMonth = getInitialMonth({ today });
        expect(isSameMonth(initialMonth, today)).toBe(true);
      });
    });
  });
  describe('when toDate is given', () => {
    describe('when toDate is before the default initial date', () => {
      const month = new Date(2010, 11, 12);
      const toDate = addMonths(month, -2);
      describe('when the number of month is 1', () => {
        const numberOfMonths = 1;
        it('return the toDate', () => {
          const initialMonth = getInitialMonth({
            month,
            toDate,
            numberOfMonths
          });
          expect(isSameMonth(initialMonth, toDate)).toBe(true);
        });
      });
      describe('when the number of month is 3', () => {
        const numberOfMonths = 3;
        it('return the toDate plus the number of months', () => {
          const initialMonth = getInitialMonth({
            month,
            toDate,
            numberOfMonths
          });
          const expectedMonth = addMonths(toDate, -1 * (numberOfMonths - 1));
          expect(isSameMonth(initialMonth, expectedMonth)).toBe(true);
        });
      });
    });
  });