Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(affix): improve Affix component and tests #3270

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/affix/Affix.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ const Affix = forwardRef<AffixRef, AffixProps>((props, ref) => {
}

const calcTop = wrapToTop - containerToTop; // 节点顶部到 container 顶部的距离
const containerHeight =
scrollContainer.current[scrollContainer.current instanceof Window ? 'innerHeight' : 'clientHeight'] -
wrapHeight;
const isWindow = typeof window !== 'undefined' && scrollContainer.current === window;

const containerHeight = scrollContainer.current[isWindow ? 'innerHeight' : 'clientHeight'] - wrapHeight;
const calcBottom = containerToTop + containerHeight - (offsetBottom ?? 0); // 计算 bottom 相对应的 top 值

let fixedTop: number | false;
if (offsetTop !== undefined && calcTop <= offsetTop) {
if (props.offsetTop !== undefined && calcTop <= offsetTop) {
// top 的触发
fixedTop = containerToTop + offsetTop;
} else if (offsetBottom !== undefined && wrapToTop >= calcBottom) {
} else if (props?.offsetBottom !== undefined && wrapToTop >= calcBottom) {
// bottom 的触发
fixedTop = calcBottom;
} else {
Expand Down Expand Up @@ -102,7 +102,7 @@ const Affix = forwardRef<AffixRef, AffixProps>((props, ref) => {
});
}
ticking.current = true;
}, [classPrefix, offsetBottom, offsetTop, onFixedChange, zIndex]);
}, [classPrefix, offsetBottom, offsetTop, onFixedChange, props?.offsetBottom, props.offsetTop, zIndex]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不建议这样写dep , 另外这样子为什么就达到了修复判断距离容器位置参数是否传入的效果?

Copy link
Contributor Author

@RSS1102 RSS1102 Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
我被 eslint 提示这样传入 dep。

修复的原因在之前写了不小心 hide 了 现在恢复了。


useImperativeHandle(ref, () => ({
handleScroll,
Expand Down
117 changes: 110 additions & 7 deletions src/affix/__tests__/affix.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ import Affix from '../index';
describe('Affix 组件测试', () => {
const mockFn = vi.spyOn(HTMLDivElement.prototype, 'getBoundingClientRect');
const mockScrollTo = async (top: number) => {
mockFn.mockImplementation(() => ({ top, bottom: 0 }));
mockFn.mockImplementation(() => ({
top,
bottom: 0,
left: 0,
right: 0,
height: 0,
width: 0,
x: 0,
y: 0,
toJSON: () => ({}),
}));
};

beforeEach(async () => {
await mockScrollTo(0);
});

test('render perfectly', async () => {
const { queryByText } = render(
<Affix>
Expand All @@ -19,24 +31,115 @@ describe('Affix 组件测试', () => {

expect(queryByText('固钉')).toBeInTheDocument();
});
test('offsetTop and onFixedChange and zIndex', async () => {

test('className', async () => {
const { container } = render(
<Affix className="custom-class-name">
<div>固钉</div>
</Affix>,
);

const affixElement = container.querySelector('.custom-class-name');
expect(affixElement).not.toBeNull();
expect(affixElement?.className).toContain('custom-class-name');
});

test('style', async () => {
const { container } = render(
<Affix style={{ background: 'red' }} className="custom-class-name">
<div>固钉</div>
</Affix>,
);
const affixElement = container.querySelector('.custom-class-name');
expect(affixElement).not.toBeNull();
expect((affixElement as HTMLElement)?.style.background).toBe('red');
});

test('content', async () => {
const Children = () => <div>固钉</div>;
const { queryByText } = render(<Affix content={<Children />} />);
expect(queryByText('固钉')).toBeInTheDocument();
});

test('offsetTop trigger onFixedChange and zIndex', async () => {
const onFixedChangeMock = vi.fn();

const { getByText } = render(
<Affix offsetTop={20} onFixedChange={onFixedChangeMock} zIndex={2}>
<div>固钉</div>
</Affix>,
);
// 默认
expect(onFixedChangeMock).toHaveBeenCalledTimes(0);
expect(getByText('固钉').parentNode).not.toHaveClass('t-affix');
expect(getByText('固钉').parentElement?.style.zIndex).toBe('');

// offsetTop
await mockScrollTo(30);
await mockScrollTo(10);
await mockTimeout(() => false, 200);
expect(onFixedChangeMock).toHaveBeenCalledTimes(1);

expect(getByText('固钉').parentNode).toHaveClass('t-affix');
expect(getByText('固钉').parentElement?.style.zIndex).toBe('2');
});

test('offsetBottom trigger onFixedChange and zIndex', async () => {
const onFixedChangeMock = vi.fn();

const { getByText } = render(
<Affix offsetTop={-1} onFixedChange={onFixedChangeMock} zIndex={2}>
<Affix offsetBottom={20} onFixedChange={onFixedChangeMock} zIndex={2}>
<div>固钉</div>
</Affix>,
);
// 默认
expect(onFixedChangeMock).toHaveBeenCalledTimes(0);
expect(getByText('固钉').parentNode).not.toHaveClass('t-affix');
expect(getByText('固钉').parentElement?.style.zIndex).toBe('');

// offsetBottom
const isWindow = typeof window !== 'undefined' && window.innerHeight !== undefined;
const { clientHeight } = document.documentElement;
const { innerHeight } = window;
await mockScrollTo((isWindow ? innerHeight : clientHeight) - 40);
await mockScrollTo(isWindow ? innerHeight : clientHeight);
await mockTimeout(() => false, 200);
expect(onFixedChangeMock).toHaveBeenCalledTimes(1);

expect(getByText('固钉').parentNode).toHaveClass('t-affix');
expect(getByText('固钉').parentElement?.style.zIndex).toBe('2');
});

test('offsetTop and offsetBottom trigger onFixedChange and zIndex', async () => {
const onFixedChangeMock = vi.fn();

expect(onFixedChangeMock).toBeCalledTimes(0);
const { getByText } = render(
<Affix offsetBottom={20} offsetTop={20} onFixedChange={onFixedChangeMock} zIndex={2}>
<div>固钉</div>
</Affix>,
);
// 默认
expect(onFixedChangeMock).toHaveBeenCalledTimes(0);
expect(getByText('固钉').parentNode).not.toHaveClass('t-affix');
expect(getByText('固钉').parentElement.style.zIndex).toBe('');
expect(getByText('固钉').parentElement?.style.zIndex).toBe('');

await mockScrollTo(-10);
// offsetTop
await mockScrollTo(30);
await mockScrollTo(10);
await mockTimeout(() => false, 200);
expect(onFixedChangeMock).toHaveBeenCalledTimes(1);

// offsetBottom
const isWindow = typeof window !== 'undefined' && window.innerHeight !== undefined;
const { clientHeight } = document.documentElement;
const { innerHeight } = window;
await mockScrollTo((isWindow ? innerHeight : clientHeight) - 40);
await mockScrollTo(isWindow ? innerHeight : clientHeight);
await mockTimeout(() => false, 200);

expect(onFixedChangeMock).toHaveBeenCalledTimes(1);

expect(getByText('固钉').parentNode).toHaveClass('t-affix');
expect(getByText('固钉').parentElement.style.zIndex).toBe('2');
expect(getByText('固钉').parentElement?.style.zIndex).toBe('2');
});
});
Loading