From e4d9aed2784dffe3377682b9c03c34a237c67a8a Mon Sep 17 00:00:00 2001 From: miracles1919 <516571350@qq.com> Date: Tue, 8 Aug 2023 13:42:10 +0800 Subject: [PATCH] test(useLockScroll): update test (#6292) --- src/utils/tests/use-lock-scroll.test.tsx | 41 ++++++++---------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/src/utils/tests/use-lock-scroll.test.tsx b/src/utils/tests/use-lock-scroll.test.tsx index 5529a3b59f..d294743421 100644 --- a/src/utils/tests/use-lock-scroll.test.tsx +++ b/src/utils/tests/use-lock-scroll.test.tsx @@ -1,9 +1,9 @@ -import React, { useRef } from 'react' -import { render, fireEvent } from 'testing' +import React, { useRef, createRef } from 'react' +import { render, fireEvent, screen, createEvent } from 'testing' import { useLockScroll } from '../use-lock-scroll' describe('useLockScroll', () => { - test('onTouchMove', async () => { + test('use preventDefault when event listener is treated as as passive', () => { const handleTouch = jest.fn() const TestComponent = () => { const divRef = useRef(null) @@ -11,38 +11,25 @@ describe('useLockScroll', () => { useLockScroll(divRef, true) return ( -
- {new Array(10).fill({}).map((_, i) => ( -

Test component {i}

- ))} +
+ div
) } - const { getByTestId } = render() + render() - const testEl = getByTestId('lock') + const el = screen.getByText('div') + const fn = jest.fn() - fireEvent.touchStart(testEl, { - touches: [{ clientX: 0, clientY: 0 }], - }) - fireEvent.touchMove(testEl, { - touches: [{ clientX: 0, clientY: 200 }], - }) - fireEvent.touchEnd(testEl, { + 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() }) })