From 6a19aeb6166c1d6f47c9564c1311a7f46b1f61cc Mon Sep 17 00:00:00 2001 From: admin-zlj <1759629281@qq.com> Date: Sat, 8 Feb 2025 15:08:09 +0800 Subject: [PATCH] feat: add tests for useStickyFixed hook --- .../useStickyFixed/__tests__/index.test.ts | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 packages/hooks/src/useStickyFixed/__tests__/index.test.ts diff --git a/packages/hooks/src/useStickyFixed/__tests__/index.test.ts b/packages/hooks/src/useStickyFixed/__tests__/index.test.ts new file mode 100644 index 000000000..7f6bd2ccd --- /dev/null +++ b/packages/hooks/src/useStickyFixed/__tests__/index.test.ts @@ -0,0 +1,42 @@ +import { act, renderHook } from '@testing-library/react'; +import useStickyFixed from '../index'; + +const scrollElement = document.createElement('div'); +scrollElement.style.overflowY = 'scroll'; +scrollElement.style.height = '200px'; // 设置高度以允许滚动 +scrollElement.style.width = '200px'; // 设置高度以允许滚动 +document.body.appendChild(scrollElement); + +const topElement = document.createElement('div'); //top元素用于填充 +topElement.style.height = '100px'; +scrollElement.appendChild(topElement); + +const targetElement = document.createElement('div'); // 模拟 sticky 的元素 +targetElement.style.position = 'sticky'; +targetElement.style.top = '0'; +targetElement.style.height = '20px'; //其他元素用于填充 +scrollElement.appendChild(targetElement); + +const bottomElement = document.createElement('div'); //bottom元素用于填充 +bottomElement.style.height = '200px'; +scrollElement.appendChild(bottomElement); + +describe('useStickyFixed', () => { + it('should set state to false when not scrolling', () => { + const { result } = renderHook(() => useStickyFixed(targetElement, { scrollTarget: scrollElement })); + + // 开始未滚动 返回false + expect(result.current[0]).toBe(false); + }); + + it('should not throw if target is not found', () => { + const { result } = renderHook(() => useStickyFixed(null, { scrollTarget: scrollElement })); + + act(() => { + scrollElement.scrollTop = 0; + scrollElement.dispatchEvent(new Event('scroll')); + }); + + expect(result.current[0]).toBe(false); // 没有抛出错误 + }); +});