From 9dda0313eb77f4ea2c629c1a4d0fbab8a201b53a Mon Sep 17 00:00:00 2001 From: hyu_wang Date: Wed, 9 Aug 2023 20:44:10 +0800 Subject: [PATCH] test: useLockScroll with strict params #6253 --- src/utils/tests/use-lock-scroll.test.tsx | 36 +++++++++++++++--------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/utils/tests/use-lock-scroll.test.tsx b/src/utils/tests/use-lock-scroll.test.tsx index 035783f995..169721f3fe 100644 --- a/src/utils/tests/use-lock-scroll.test.tsx +++ b/src/utils/tests/use-lock-scroll.test.tsx @@ -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', () => { @@ -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(null) - const { getByTestId } = render( - - ) + useLockScroll(divRef, true) - const testEl = getByTestId('lock') + return ( +
+ div +
+ ) + } - fireEvent.touchStart(testEl, { - touches: [{ clientX: 0, clientY: 0 }], - }) - fireEvent.touchMove(testEl, { - touches: [{ clientX: 0, clientY: 200 }], - }) - fireEvent.touchEnd(testEl, { + render() + + 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 () => {