Blame view

天文台pc/tianwentai-ui/node_modules/react-day-picker/src/contexts/Focus/FocusContext.test.ts 5.32 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
  import { act } from '@testing-library/react';
  import {
    addDays,
    addMonths,
    addWeeks,
    addYears,
    endOfWeek,
    startOfWeek
  } from 'date-fns';
  
  import { renderDayPickerHook, RenderHookResult } from 'test/render';
  import { freezeBeforeAll } from 'test/utils';
  
  import { FocusContextValue, useFocusContext } from 'contexts/Focus';
  
  const today = new Date(2021, 11, 8); // make sure is in the middle of the week for the complete test
  freezeBeforeAll(today);
  
  function renderHook() {
    return renderDayPickerHook<FocusContextValue>(useFocusContext);
  }
  
  type HookFunction =
    | 'focusDayAfter'
    | 'focusDayBefore'
    | 'focusWeekAfter'
    | 'focusWeekBefore'
    | 'focusMonthBefore'
    | 'focusMonthAfter'
    | 'focusYearBefore'
    | 'focusYearAfter'
    | 'focusStartOfWeek'
    | 'focusEndOfWeek';
  
  test('`focusedDay` should be undefined', () => {
    const result = renderHook();
    expect(result.current.focusedDay).toBeUndefined();
  });
  
  const tests: Array<HookFunction> = [
    'focusDayAfter',
    'focusDayBefore',
    'focusWeekAfter',
    'focusWeekBefore',
    'focusMonthBefore',
    'focusMonthAfter',
    'focusYearBefore',
    'focusYearAfter',
    'focusStartOfWeek',
    'focusEndOfWeek'
  ];
  describe.each(tests)('when calling %s', (fn: HookFunction) => {
    test('`focusedDay` should be undefined', () => {
      const result = renderHook();
      result.current[fn];
      expect(result.current.focusedDay).toBeUndefined();
    });
  });
  
  describe('when a day is focused', () => {
    const day = today;
    let result: RenderHookResult<FocusContextValue>;
    beforeEach(() => {
      result = renderHook();
      act(() => result.current.focus(day));
    });
    test('should set the focused day', () => {
      expect(result.current.focusedDay).toEqual(day);
    });
    describe('when "focusDayBefore" is called', () => {
      const dayBefore = addDays(day, -1);
      beforeEach(() => act(() => result.current.focusDayBefore()));
      test('should focus the day before', () => {
        expect(result.current.focusedDay).toEqual(dayBefore);
      });
      test.todo('should call the navigation goToDate');
    });
    describe('when "focusDayAfter" is called', () => {
      beforeEach(() => act(() => result.current.focusDayAfter()));
      test('should focus the day after', () => {
        const dayAfter = addDays(day, 1);
        expect(result.current.focusedDay).toEqual(dayAfter);
      });
      test.todo('should call the navigation goToDate');
    });
    describe('when "focusWeekBefore" is called', () => {
      beforeEach(() => act(() => result.current.focusWeekBefore()));
      test('should focus the day in the previous week', () => {
        const prevWeek = addWeeks(day, -1);
        expect(result.current.focusedDay).toEqual(prevWeek);
      });
      test.todo('should call the navigation goToDate');
    });
    describe('when "focusWeekAfter" is called', () => {
      beforeEach(() => act(() => result.current.focusWeekAfter()));
      test('should focus the day in the next week', () => {
        const nextWeek = addWeeks(day, 1);
        expect(result.current.focusedDay).toEqual(nextWeek);
      });
      test.todo('should call the navigation goToDate');
    });
    describe('when "focusStartOfWeek" is called', () => {
      beforeEach(() => act(() => result.current.focusStartOfWeek()));
      test('should focus the first day of the week', () => {
        const firstDayOfWeek = startOfWeek(day);
        expect(result.current.focusedDay).toEqual(firstDayOfWeek);
      });
      test.todo('should call the navigation goToDate');
    });
    describe('when "focusEndOfWeek" is called', () => {
      beforeEach(() => act(() => result.current.focusEndOfWeek()));
      test('should focus the last day of the week', () => {
        const lastDayOfWeek = endOfWeek(day);
        expect(result.current.focusedDay).toEqual(lastDayOfWeek);
      });
      test.todo('should call the navigation goToDate');
    });
    describe('when "focusMonthBefore" is called', () => {
      beforeEach(() => act(() => result.current.focusMonthBefore()));
      test('should focus the day in the month before', () => {
        const monthBefore = addMonths(day, -1);
        expect(result.current.focusedDay).toEqual(monthBefore);
      });
      test.todo('should call the navigation goToDate');
    });
    describe('when "focusMonthAfter" is called', () => {
      beforeEach(() => act(() => result.current.focusMonthAfter()));
      test('should focus the day in the month after', () => {
        const monthAfter = addMonths(day, 1);
        expect(result.current.focusedDay).toEqual(monthAfter);
      });
      test.todo('should call the navigation goToDate');
    });
    describe('when "focusYearBefore" is called', () => {
      beforeEach(() => act(() => result.current.focusYearBefore()));
      test('should focus the day in the year before', () => {
        const prevYear = addYears(day, -1);
        expect(result.current.focusedDay).toEqual(prevYear);
      });
      test.todo('should call the navigation goToDate');
    });
    describe('when "focusYearAfter" is called', () => {
      beforeEach(() => act(() => result.current.focusYearAfter()));
      test('should focus the day in the year after', () => {
        const nextYear = addYears(day, 1);
        expect(result.current.focusedDay).toEqual(nextYear);
      });
      test.todo('should call the navigation goToDate');
    });
    describe('when blur is called', () => {
      beforeEach(() => act(() => result.current.blur()));
      test('`focusedDay` should be undefined', () => {
        expect(result.current.focusedDay).toBeUndefined();
      });
    });
  });