Skip to content

Commit

Permalink
test: useLockScroll with strict params ant-design#6253
Browse files Browse the repository at this point in the history
  • Loading branch information
19Qingfeng committed Aug 9, 2023
1 parent ff93b4a commit 9dda031
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/utils/tests/use-lock-scroll.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useRef } from 'react'
import { render, fireEvent } from 'testing'
import { render, fireEvent, createEvent, screen } from 'testing'
import { useLockScroll } from '../use-lock-scroll'

describe('useLockScroll', () => {
Expand Down Expand Up @@ -44,26 +44,34 @@ describe('useLockScroll', () => {
TestComponent = null as any
})

test('onTouchMove', async () => {
test('use preventDefault when event listener is treated as as passive', () => {
const handleTouch = jest.fn()
const TestComponent = () => {
const divRef = useRef<HTMLDivElement>(null)

const { getByTestId } = render(
<TestComponent scrollParams={true} handleTouch={handleTouch} />
)
useLockScroll(divRef, true)

const testEl = getByTestId('lock')
return (
<div ref={divRef} onTouchMove={handleTouch}>
div
</div>
)
}

fireEvent.touchStart(testEl, {
touches: [{ clientX: 0, clientY: 0 }],
})
fireEvent.touchMove(testEl, {
touches: [{ clientX: 0, clientY: 200 }],
})
fireEvent.touchEnd(testEl, {
render(<TestComponent />)

const el = screen.getByText('div')
const fn = jest.fn()

const event = createEvent.touchMove(el, {
touches: [{ clientX: 0, clientY: 400 }],
})
Object.defineProperty(event, 'preventDefault', {
value: fn,
})
fireEvent(el, event)

expect(handleTouch).toHaveBeenCalled()
expect(fn).toBeCalled()
})

test('Scroll To Bottom', async () => {
Expand Down

0 comments on commit 9dda031

Please sign in to comment.