Blame view

天文台pc/tianwentai-ui/node_modules/react-day-picker/src/contexts/SelectSingle/SelectSingleContext.test.ts 2.45 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
  import { MouseEvent } from 'react';
  
  import { DayPickerProps } from 'DayPicker';
  
  import { renderDayPickerHook } from 'test/render';
  import { freezeBeforeAll } from 'test/utils';
  
  import { DayPickerSingleProps } from 'types/DayPickerSingle';
  import { ActiveModifiers } from 'types/Modifiers';
  
  import {
    SelectSingleContextValue,
    useSelectSingle
  } from './SelectSingleContext';
  
  const today = new Date(2021, 11, 8);
  freezeBeforeAll(today);
  
  function renderHook(props?: Partial<DayPickerProps>) {
    return renderDayPickerHook<SelectSingleContextValue>(useSelectSingle, props);
  }
  describe('when is not a single select DayPicker', () => {
    test('the selected day should be undefined', () => {
      const result = renderHook();
      expect(result.current.selected).toBeUndefined();
    });
  });
  
  describe('when a day is selected from DayPicker props', () => {
    test('the selected day should be today', () => {
      const dayPickerProps: DayPickerSingleProps = {
        mode: 'single',
        selected: today
      };
      const result = renderHook(dayPickerProps);
      expect(result.current.selected).toBe(today);
    });
  });
  describe('when onDayClick is called', () => {
    const dayPickerProps: DayPickerSingleProps = {
      mode: 'single',
      onSelect: jest.fn(),
      onDayClick: jest.fn()
    };
    const result = renderHook(dayPickerProps);
    const activeModifiers = {};
    const event = {} as MouseEvent;
    test('should call the `onSelect` event handler', () => {
      result.current.onDayClick?.(today, activeModifiers, event);
      expect(dayPickerProps.onSelect).toHaveBeenCalledWith(
        today,
        today,
        activeModifiers,
        event
      );
    });
    test('should call the `onDayClick` event handler', () => {
      result.current.onDayClick?.(today, activeModifiers, event);
      expect(dayPickerProps.onDayClick).toHaveBeenCalledWith(
        today,
        activeModifiers,
        event
      );
    });
  });
  describe('if a selected day is not required', () => {
    const dayPickerProps: DayPickerSingleProps = {
      mode: 'single',
      onSelect: jest.fn(),
      required: false
    };
    test('should call the `onSelect` event handler with an undefined day', () => {
      const result = renderHook(dayPickerProps);
      const activeModifiers: ActiveModifiers = { selected: true };
      const event = {} as MouseEvent;
      result.current.onDayClick?.(today, activeModifiers, event);
      expect(dayPickerProps.onSelect).toHaveBeenCalledWith(
        undefined,
        today,
        activeModifiers,
        event
      );
    });
  });