Skip to content

Commit

Permalink
test: add useLockScroll test case
Browse files Browse the repository at this point in the history
  • Loading branch information
1587315093 committed Aug 7, 2023
1 parent e20ef21 commit d0b4211
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/utils/tests/use-lock-scroll.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React, { useRef } from 'react'
import { render, fireEvent } from 'testing'
import { useLockScroll } from '../use-lock-scroll'

describe('useLockScroll', () => {
test('onTouchMove', async () => {
const handleTouch = jest.fn()
const TestComponent = () => {
const divRef = useRef<HTMLDivElement>(null)

useLockScroll(divRef, true)

return (
<div
ref={divRef}
data-testid='lock'
style={{
height: 200,
overflow: 'scroll',
cursor: 'grab',
touchAction: 'none',
}}
onTouchMove={handleTouch}
>
{new Array(10).fill({}).map((_, i) => (
<h1 key={i}> Test component {i}</h1>
))}
</div>
)
}

const { getByTestId } = render(<TestComponent />)

const testEl = getByTestId('lock')

fireEvent.touchStart(testEl, {
touches: [{ clientX: 0, clientY: 0 }],
})
fireEvent.touchMove(testEl, {
touches: [{ clientX: 0, clientY: 200 }],
})
fireEvent.touchEnd(testEl, {
touches: [{ clientX: 0, clientY: 400 }],
})

expect(handleTouch).toHaveBeenCalled()
})
})

0 comments on commit d0b4211

Please sign in to comment.