Browse Source
* test: add test case * test: add test case * docs: update api * docs: faq updatepull/42135/head
二货爱吃白萝卜
2 years ago
committed by
GitHub
6 changed files with 162 additions and 20 deletions
@ -0,0 +1,95 @@ |
|||
import Dayjs from 'dayjs'; |
|||
import 'dayjs/locale/zh-cn'; |
|||
import MockDate from 'mockdate'; |
|||
import { resetWarned } from 'rc-util/lib/warning'; |
|||
import React from 'react'; |
|||
import Calendar from '..'; |
|||
import { fireEvent, render, waitFakeTimer } from '../../../tests/utils'; |
|||
|
|||
describe('Calendar.onSelect', () => { |
|||
beforeAll(() => { |
|||
MockDate.set(Dayjs('2000-01-01').valueOf()); |
|||
}); |
|||
|
|||
beforeEach(() => { |
|||
resetWarned(); |
|||
jest.useFakeTimers(); |
|||
jest.clearAllTimers(); |
|||
}); |
|||
|
|||
afterEach(() => { |
|||
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} />); |
|||
|
|||
// 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: 'date' }); |
|||
}); |
|||
}); |
Loading…
Reference in new issue