Skip to content

Commit

Permalink
test: add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Jul 2, 2024
1 parent 2eec018 commit c947e72
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/components/calendar-picker/tests/calendar-picker.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react'
import { render, testA11y, fireEvent } from 'testing'
import CalendarPicker from '..'
import MockDate from 'mockdate'
import { spyElementPrototype } from 'rc-util/lib/test/domHook'
import React, { act } from 'react'
import { fireEvent, render, testA11y } from 'testing'
import CalendarPicker, { CalendarPickerRef } from '..'

const classPrefix = `adm-calendar-picker-view`

Expand All @@ -13,7 +14,16 @@ const maxDate: Date = new Date('2023-05-31')
const singleDate: Date = new Date('2023-05-03')

describe('Calendar', () => {
beforeEach(() => {
jest.useFakeTimers()
})

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

test('a11y', async () => {
jest.useRealTimers()
await testA11y(<CalendarPicker />)
})

Expand All @@ -36,4 +46,25 @@ describe('Calendar', () => {
expect(dateEl.parentElement).toHaveClass(`${classPrefix}-cell-selected`)
expect(fn).toBeCalled()
})

test('jumpTo should trigger scroll', () => {
const ref = React.createRef<CalendarPickerRef>()
render(<CalendarPicker visible ref={ref} />)

const spyScrollIntoView = jest.fn()
const spyHTMLElement = spyElementPrototype(
HTMLElement,
'scrollIntoView',
spyScrollIntoView
)

// Trigger scroll
ref.current!.jumpToToday()
act(() => {
jest.runAllTimers()
})
expect(spyScrollIntoView).toBeCalled()

spyHTMLElement.mockRestore()
})
})

0 comments on commit c947e72

Please sign in to comment.