File size: 3,002 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Dayjs from 'dayjs';

import 'dayjs/locale/zh-cn';

import React from 'react';
import { resetWarned } from 'rc-util/lib/warning';

import Calendar from '..';
import { fireEvent, render, waitFakeTimer } from '../../../tests/utils';

describe('Calendar.onSelect', () => {
  beforeEach(() => {
    resetWarned();
    jest.useFakeTimers().setSystemTime(new Date('2000-02-01'));
  });

  afterEach(() => {
    jest.clearAllTimers();
    jest.useRealTimers();
  });

  it('source of year select', async () => {
    const onSelect = jest.fn();
    const { container } = render(<Calendar onSelect={onSelect} />);

    fireEvent.mouseDown(container.querySelector('.ant-select-selector')!);
    await waitFakeTimer();

    fireEvent.click(container.querySelector('.ant-select-item-option')!);
    await waitFakeTimer();

    expect(onSelect).toHaveBeenCalledWith(expect.anything(), { source: 'year' });
  });

  it('source of month select', async () => {
    const onSelect = jest.fn();
    const { container } = render(<Calendar onSelect={onSelect} />);

    fireEvent.mouseDown(container.querySelectorAll('.ant-select-selector')[1]!);
    await waitFakeTimer();

    fireEvent.click(container.querySelector('.ant-select-item-option')!);
    await waitFakeTimer();

    expect(onSelect).toHaveBeenCalledWith(expect.anything(), { source: 'month' });
  });

  it('source of customize', async () => {
    const onSelect = jest.fn();
    const { container } = render(
      <Calendar
        onSelect={onSelect}
        headerRender={({ onChange }) => (
          <button
            className="bamboo"
            type="button"
            onClick={() => {
              onChange(Dayjs('1999-01-01'));
            }}
          >
            Trigger
          </button>
        )}
      />,
    );

    fireEvent.click(container.querySelector('.bamboo')!);
    await waitFakeTimer();

    expect(onSelect).toHaveBeenCalledWith(expect.anything(), { source: 'customize' });
  });

  it('source of date', () => {
    const onSelect = jest.fn();
    const { container } = render(<Calendar onSelect={onSelect} />);

    fireEvent.click(container.querySelector('.ant-picker-cell')!);
    expect(onSelect).toHaveBeenCalledWith(expect.anything(), { source: 'date' });
  });

  it('source of date with month panel', async () => {
    const onSelect = jest.fn();
    const onPanelChange = jest.fn();
    const { container } = render(<Calendar onSelect={onSelect} onPanelChange={onPanelChange} />);

    // Default is month radio
    fireEvent.click(container.querySelector('.ant-picker-cell')!);
    expect(onSelect).toHaveBeenCalledWith(expect.anything(), { source: 'date' });

    // Click year radio
    fireEvent.click(container.querySelectorAll('.ant-radio-button-input')[1]!);
    expect(onPanelChange).toHaveBeenCalledWith(expect.anything(), 'year');

    fireEvent.click(container.querySelector('.ant-picker-cell')!);
    expect(onSelect).toHaveBeenCalledWith(expect.anything(), { source: 'month' });
  });
});