Skip to content

Commit

Permalink
feat: add tests for useStickyFixed hook
Browse files Browse the repository at this point in the history
  • Loading branch information
admin-zlj committed Feb 8, 2025
1 parent 8274643 commit 6a19aeb
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/hooks/src/useStickyFixed/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -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); // 没有抛出错误
});
});

0 comments on commit 6a19aeb

Please sign in to comment.