2020-04-30 10:33:29 +08:00
|
|
|
import React from 'react';
|
|
|
|
import Anchor from '..';
|
2022-10-11 22:22:36 +08:00
|
|
|
import { fireEvent, render, waitFakeTimer } from '../../../tests/utils';
|
2020-04-30 10:33:29 +08:00
|
|
|
|
|
|
|
const { Link } = Anchor;
|
|
|
|
|
|
|
|
function createDiv() {
|
|
|
|
const root = document.createElement('div');
|
|
|
|
document.body.appendChild(root);
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
let idCounter = 0;
|
|
|
|
const getHashUrl = () => `Anchor-API-${idCounter++}`;
|
|
|
|
|
|
|
|
describe('Anchor Render', () => {
|
|
|
|
const getBoundingClientRectMock = jest.spyOn(
|
|
|
|
HTMLHeadingElement.prototype,
|
|
|
|
'getBoundingClientRect',
|
|
|
|
);
|
|
|
|
const getClientRectsMock = jest.spyOn(HTMLHeadingElement.prototype, 'getClientRects');
|
|
|
|
|
|
|
|
beforeAll(() => {
|
2022-10-11 22:22:36 +08:00
|
|
|
jest.useFakeTimers();
|
2020-04-30 10:33:29 +08:00
|
|
|
getBoundingClientRectMock.mockReturnValue({
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
top: 1000,
|
|
|
|
} as DOMRect);
|
|
|
|
getClientRectsMock.mockReturnValue({ length: 1 } as DOMRectList);
|
|
|
|
});
|
|
|
|
|
2022-10-17 21:36:56 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
2022-10-11 22:22:36 +08:00
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllTimers();
|
2022-10-17 21:36:56 +08:00
|
|
|
jest.useRealTimers();
|
2022-10-11 22:22:36 +08:00
|
|
|
});
|
|
|
|
|
2020-04-30 10:33:29 +08:00
|
|
|
afterAll(() => {
|
2022-10-17 21:36:56 +08:00
|
|
|
jest.clearAllTimers();
|
2022-10-11 22:22:36 +08:00
|
|
|
jest.useRealTimers();
|
2020-04-30 10:33:29 +08:00
|
|
|
getBoundingClientRectMock.mockRestore();
|
|
|
|
getClientRectsMock.mockRestore();
|
|
|
|
});
|
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
it('renders correctly', () => {
|
2020-04-30 10:33:29 +08:00
|
|
|
const hash = getHashUrl();
|
2022-05-04 23:14:07 +08:00
|
|
|
const { container } = render(
|
2022-10-30 14:35:45 +08:00
|
|
|
<Anchor>
|
2020-04-30 10:33:29 +08:00
|
|
|
<Link href={`#${hash}`} title={hash} />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
2022-10-30 14:35:45 +08:00
|
|
|
expect(container.querySelector(`a[href="#${hash}"]`)).not.toBe(null);
|
2020-04-30 10:33:29 +08:00
|
|
|
});
|
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
it('actives the target when clicking a link', async () => {
|
2020-04-30 10:33:29 +08:00
|
|
|
const hash = getHashUrl();
|
2022-05-04 23:14:07 +08:00
|
|
|
const { container } = render(
|
2022-10-30 14:35:45 +08:00
|
|
|
<Anchor prefixCls="ant-anchor">
|
2020-04-30 10:33:29 +08:00
|
|
|
<Link href={`http://www.example.com/#${hash}`} title={hash} />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
2022-10-30 14:35:45 +08:00
|
|
|
const link = container.querySelector(`a[href="http://www.example.com/#${hash}"]`)!;
|
|
|
|
fireEvent.click(link);
|
2022-10-17 21:36:56 +08:00
|
|
|
await waitFakeTimer();
|
2022-10-30 14:35:45 +08:00
|
|
|
expect(link.classList).toContain('ant-anchor-link-title-active');
|
2020-04-30 10:33:29 +08:00
|
|
|
});
|
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
it('scrolls the page when clicking a link', async () => {
|
2020-04-30 10:33:29 +08:00
|
|
|
const root = createDiv();
|
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo');
|
2022-05-04 23:14:07 +08:00
|
|
|
render(<div id="/faq?locale=en#Q1">Q1</div>, { container: root });
|
2022-10-30 14:35:45 +08:00
|
|
|
const { container } = render(
|
|
|
|
<Anchor>
|
2020-04-30 10:33:29 +08:00
|
|
|
<Link href="/#/faq?locale=en#Q1" title="Q1" />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
2022-10-30 14:35:45 +08:00
|
|
|
const link = container.querySelector(`a[href="/#/faq?locale=en#Q1"]`)!;
|
|
|
|
fireEvent.click(link);
|
2022-10-11 22:22:36 +08:00
|
|
|
await waitFakeTimer();
|
2020-04-30 10:33:29 +08:00
|
|
|
expect(scrollToSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
it('handleScroll should not be triggered when scrolling caused by clicking a link', async () => {
|
|
|
|
const hash1 = getHashUrl();
|
|
|
|
const hash2 = getHashUrl();
|
2020-04-30 10:33:29 +08:00
|
|
|
const root = createDiv();
|
2022-10-30 14:35:45 +08:00
|
|
|
const onChange = jest.fn();
|
2022-05-04 23:14:07 +08:00
|
|
|
render(
|
2022-10-30 14:35:45 +08:00
|
|
|
<div>
|
|
|
|
<div id={hash1}>Hello</div>
|
|
|
|
<div id={hash2}>World</div>
|
|
|
|
</div>,
|
|
|
|
{ container: root },
|
2020-04-30 10:33:29 +08:00
|
|
|
);
|
2022-10-30 14:35:45 +08:00
|
|
|
const { container } = render(
|
|
|
|
<Anchor onChange={onChange}>
|
|
|
|
<Link href={`#${hash1}`} title={hash1} />
|
|
|
|
<Link href={`#${hash2}`} title={hash2} />
|
2020-04-30 10:33:29 +08:00
|
|
|
</Anchor>,
|
|
|
|
);
|
2022-10-30 14:35:45 +08:00
|
|
|
onChange.mockClear();
|
2022-04-19 16:44:47 +08:00
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
const link = container.querySelector(`a[href="#${hash2}"]`)!;
|
|
|
|
// this will trigger 1 onChange
|
|
|
|
fireEvent.click(link);
|
|
|
|
// smooth scroll caused by clicking needs time to finish.
|
|
|
|
// we scroll the window before it finish, the scroll listener should not be triggered,
|
|
|
|
fireEvent.scroll(window);
|
2020-04-30 10:33:29 +08:00
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
await waitFakeTimer();
|
|
|
|
// if the scroll listener is triggered, we will get 2 onChange, now we expect only 1.
|
|
|
|
expect(onChange).toHaveBeenCalledTimes(1);
|
2020-04-30 10:33:29 +08:00
|
|
|
});
|
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
it('should update DOM when children are unmounted', () => {
|
2020-04-30 10:33:29 +08:00
|
|
|
const hash = getHashUrl();
|
2022-04-18 21:02:11 +08:00
|
|
|
const { container, rerender } = render(
|
2020-04-30 10:33:29 +08:00
|
|
|
<Anchor>
|
|
|
|
<Link href={`#${hash}`} title={hash} />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
2022-04-18 21:02:11 +08:00
|
|
|
|
|
|
|
expect(container.querySelectorAll('.ant-anchor-link-title')).toHaveLength(1);
|
|
|
|
expect(container.querySelector('.ant-anchor-link-title')).toHaveAttribute('href', `#${hash}`);
|
|
|
|
|
|
|
|
rerender(<Anchor />);
|
|
|
|
expect(container.querySelector('.ant-anchor-link-title')).toBeFalsy();
|
2020-04-30 10:33:29 +08:00
|
|
|
});
|
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
it('should update DOM when link href is changed', async () => {
|
2020-04-30 10:33:29 +08:00
|
|
|
const hash = getHashUrl();
|
|
|
|
function AnchorUpdate({ href }: { href: string }) {
|
|
|
|
return (
|
2022-10-30 14:35:45 +08:00
|
|
|
<Anchor>
|
2020-04-30 10:33:29 +08:00
|
|
|
<Link href={href} title={hash} />
|
|
|
|
</Anchor>
|
|
|
|
);
|
|
|
|
}
|
2022-10-30 14:35:45 +08:00
|
|
|
const { container, rerender } = render(<AnchorUpdate href={`#${hash}`} />);
|
2020-04-30 10:33:29 +08:00
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
expect(container.querySelector(`a[href="#${hash}"]`)).toBeTruthy();
|
2022-05-04 23:14:07 +08:00
|
|
|
rerender(<AnchorUpdate href={`#${hash}_1`} />);
|
2022-10-30 14:35:45 +08:00
|
|
|
expect(container.querySelector(`a[href="#${hash}_1"]`)).toBeTruthy();
|
2020-04-30 10:33:29 +08:00
|
|
|
});
|
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
it('targetOffset prop', async () => {
|
2020-04-30 10:33:29 +08:00
|
|
|
const hash = getHashUrl();
|
2021-08-30 12:10:02 +08:00
|
|
|
|
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo');
|
|
|
|
const root = createDiv();
|
2022-05-04 23:14:07 +08:00
|
|
|
render(<h1 id={hash}>Hello</h1>, { container: root });
|
2022-10-30 14:35:45 +08:00
|
|
|
const { container, rerender } = render(
|
|
|
|
<Anchor>
|
2021-08-30 12:10:02 +08:00
|
|
|
<Link href={`#${hash}`} title={hash} />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
2022-04-19 16:44:47 +08:00
|
|
|
|
2022-05-04 23:14:07 +08:00
|
|
|
const setProps = (props: Record<string, any>) =>
|
|
|
|
rerender(
|
2022-10-30 14:35:45 +08:00
|
|
|
<Anchor {...props}>
|
2022-05-04 23:14:07 +08:00
|
|
|
<Link href={`#${hash}`} title={hash} />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash}"]`)!);
|
2022-10-11 22:22:36 +08:00
|
|
|
await waitFakeTimer();
|
2021-08-30 12:10:02 +08:00
|
|
|
expect(scrollToSpy).toHaveBeenLastCalledWith(0, 1000);
|
|
|
|
|
2022-05-04 23:14:07 +08:00
|
|
|
setProps({ offsetTop: 100 });
|
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash}"]`)!);
|
2022-10-11 22:22:36 +08:00
|
|
|
await waitFakeTimer();
|
2021-08-30 12:10:02 +08:00
|
|
|
expect(scrollToSpy).toHaveBeenLastCalledWith(0, 900);
|
|
|
|
|
2022-05-04 23:14:07 +08:00
|
|
|
setProps({ targetOffset: 200 });
|
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash}"]`)!);
|
2022-10-11 22:22:36 +08:00
|
|
|
await waitFakeTimer();
|
2021-08-30 12:10:02 +08:00
|
|
|
expect(scrollToSpy).toHaveBeenLastCalledWith(0, 800);
|
|
|
|
});
|
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/31941
|
2022-10-30 14:35:45 +08:00
|
|
|
it('targetOffset prop when contain spaces', async () => {
|
2021-08-30 12:10:02 +08:00
|
|
|
const hash = `${getHashUrl()} s p a c e s`;
|
2020-04-30 10:33:29 +08:00
|
|
|
|
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo');
|
|
|
|
const root = createDiv();
|
2022-05-04 23:14:07 +08:00
|
|
|
render(<h1 id={hash}>Hello</h1>, { container: root });
|
2022-10-30 14:35:45 +08:00
|
|
|
const { container, rerender } = render(
|
|
|
|
<Anchor>
|
2020-04-30 10:33:29 +08:00
|
|
|
<Link href={`#${hash}`} title={hash} />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
2022-04-19 16:44:47 +08:00
|
|
|
|
2022-05-04 23:14:07 +08:00
|
|
|
const setProps = (props: Record<string, any>) =>
|
|
|
|
rerender(
|
2022-10-30 14:35:45 +08:00
|
|
|
<Anchor {...props}>
|
2022-05-04 23:14:07 +08:00
|
|
|
<Link href={`#${hash}`} title={hash} />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash}"]`)!);
|
2022-10-11 22:22:36 +08:00
|
|
|
await waitFakeTimer();
|
2020-04-30 10:33:29 +08:00
|
|
|
expect(scrollToSpy).toHaveBeenLastCalledWith(0, 1000);
|
|
|
|
|
2022-05-04 23:14:07 +08:00
|
|
|
setProps({ offsetTop: 100 });
|
2022-10-30 14:35:45 +08:00
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash}"]`)!);
|
2022-10-11 22:22:36 +08:00
|
|
|
await waitFakeTimer();
|
2020-04-30 10:33:29 +08:00
|
|
|
expect(scrollToSpy).toHaveBeenLastCalledWith(0, 900);
|
|
|
|
|
2022-05-04 23:14:07 +08:00
|
|
|
setProps({ targetOffset: 200 });
|
2022-10-30 14:35:45 +08:00
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash}"]`)!);
|
2022-10-11 22:22:36 +08:00
|
|
|
await waitFakeTimer();
|
2020-04-30 10:33:29 +08:00
|
|
|
expect(scrollToSpy).toHaveBeenLastCalledWith(0, 800);
|
2022-10-30 14:35:45 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('onClick event', () => {
|
|
|
|
const hash = getHashUrl();
|
|
|
|
let event;
|
|
|
|
let link;
|
|
|
|
const handleClick = (
|
|
|
|
e: React.MouseEvent<HTMLElement>,
|
|
|
|
_link: { title: React.ReactNode; href: string },
|
|
|
|
) => {
|
|
|
|
event = e;
|
|
|
|
link = _link;
|
|
|
|
};
|
|
|
|
|
|
|
|
const href = `#${hash}`;
|
|
|
|
const title = hash;
|
|
|
|
const { container } = render(
|
|
|
|
<Anchor onClick={handleClick}>
|
|
|
|
<Link href={href} title={title} />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
2020-04-30 10:33:29 +08:00
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
fireEvent.click(container.querySelector(`a[href="${href}"]`)!);
|
|
|
|
expect(event).not.toBe(undefined);
|
|
|
|
expect(link).toEqual({ href, title });
|
2020-04-30 10:33:29 +08:00
|
|
|
});
|
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
it('onChange event', () => {
|
2020-04-30 10:33:29 +08:00
|
|
|
const hash1 = getHashUrl();
|
|
|
|
const hash2 = getHashUrl();
|
|
|
|
const onChange = jest.fn();
|
2022-10-30 14:35:45 +08:00
|
|
|
const { container } = render(
|
|
|
|
<Anchor onChange={onChange}>
|
2020-04-30 10:33:29 +08:00
|
|
|
<Link href={`#${hash1}`} title={hash1} />
|
|
|
|
<Link href={`#${hash2}`} title={hash2} />
|
|
|
|
</Anchor>,
|
2022-05-04 23:14:07 +08:00
|
|
|
// https://github.com/testing-library/react-testing-library/releases/tag/v13.0.0
|
|
|
|
// @ts-ignore
|
|
|
|
{ legacyRoot: true },
|
2020-04-30 10:33:29 +08:00
|
|
|
);
|
2022-04-19 16:44:47 +08:00
|
|
|
|
2020-04-30 10:33:29 +08:00
|
|
|
expect(onChange).toHaveBeenCalledTimes(1);
|
2022-10-30 14:35:45 +08:00
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash2}"]`)!);
|
2020-04-30 10:33:29 +08:00
|
|
|
expect(onChange).toHaveBeenCalledTimes(2);
|
2022-10-30 14:35:45 +08:00
|
|
|
expect(onChange).toHaveBeenLastCalledWith(`#${hash2}`);
|
2021-05-20 21:36:53 +08:00
|
|
|
});
|
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
it('handles invalid hash correctly', () => {
|
2022-05-04 23:14:07 +08:00
|
|
|
const { container } = render(
|
2022-10-30 14:35:45 +08:00
|
|
|
<Anchor>
|
2020-05-05 19:33:33 +08:00
|
|
|
<Link href="notexsited" title="title" />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
const link = container.querySelector(`a[href="notexsited"]`)!;
|
|
|
|
fireEvent.click(link);
|
|
|
|
expect(container.querySelector(`.ant-anchor-link-title-active`)?.textContent).toBe('title');
|
2020-05-05 19:33:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('test edge case when getBoundingClientRect return zero size', async () => {
|
2022-10-17 21:36:56 +08:00
|
|
|
getBoundingClientRectMock.mockReturnValue({ width: 0, height: 0, top: 1000 } as DOMRect);
|
2020-05-05 19:33:33 +08:00
|
|
|
const hash = getHashUrl();
|
|
|
|
|
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo');
|
|
|
|
const root = createDiv();
|
2022-05-04 23:14:07 +08:00
|
|
|
render(<h1 id={hash}>Hello</h1>, { container: root });
|
2022-10-30 14:35:45 +08:00
|
|
|
const { container, rerender } = render(
|
|
|
|
<Anchor>
|
2020-05-05 19:33:33 +08:00
|
|
|
<Link href={`#${hash}`} title={hash} />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
2022-04-19 16:44:47 +08:00
|
|
|
|
2022-05-04 23:14:07 +08:00
|
|
|
const setProps = (props: Record<string, any>) =>
|
|
|
|
rerender(
|
2022-10-30 14:35:45 +08:00
|
|
|
<Anchor {...props}>
|
2022-05-04 23:14:07 +08:00
|
|
|
<Link href={`#${hash}`} title={hash} />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
2022-10-30 14:35:45 +08:00
|
|
|
|
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash}"]`)!);
|
2022-10-11 22:22:36 +08:00
|
|
|
await waitFakeTimer();
|
2020-05-05 19:33:33 +08:00
|
|
|
expect(scrollToSpy).toHaveBeenLastCalledWith(0, 1000);
|
|
|
|
|
2022-05-04 23:14:07 +08:00
|
|
|
setProps({ offsetTop: 100 });
|
2022-10-30 14:35:45 +08:00
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash}"]`)!);
|
2022-10-11 22:22:36 +08:00
|
|
|
await waitFakeTimer();
|
2020-05-05 19:33:33 +08:00
|
|
|
expect(scrollToSpy).toHaveBeenLastCalledWith(0, 900);
|
|
|
|
|
2022-05-04 23:14:07 +08:00
|
|
|
setProps({ targetOffset: 200 });
|
2022-10-30 14:35:45 +08:00
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash}"]`)!);
|
2022-10-11 22:22:36 +08:00
|
|
|
await waitFakeTimer();
|
2020-05-05 19:33:33 +08:00
|
|
|
expect(scrollToSpy).toHaveBeenLastCalledWith(0, 800);
|
|
|
|
|
|
|
|
getBoundingClientRectMock.mockReturnValue({
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
top: 1000,
|
|
|
|
} as DOMRect);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('test edge case when container is not windows', async () => {
|
|
|
|
const hash = getHashUrl();
|
|
|
|
|
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo');
|
|
|
|
const root = createDiv();
|
2022-05-04 23:14:07 +08:00
|
|
|
render(<h1 id={hash}>Hello</h1>, { container: root });
|
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
const { container, rerender } = render(
|
|
|
|
<Anchor getContainer={() => document.body}>
|
2020-05-05 19:33:33 +08:00
|
|
|
<Link href={`#${hash}`} title={hash} />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
2022-04-19 16:44:47 +08:00
|
|
|
|
2022-05-04 23:14:07 +08:00
|
|
|
const setProps = (props: Record<string, any>) =>
|
|
|
|
rerender(
|
2022-10-30 14:35:45 +08:00
|
|
|
<Anchor getContainer={() => document.body} {...props}>
|
2022-05-04 23:14:07 +08:00
|
|
|
<Link href={`#${hash}`} title={hash} />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
2022-10-30 14:35:45 +08:00
|
|
|
|
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash}"]`)!);
|
2022-10-11 22:22:36 +08:00
|
|
|
await waitFakeTimer();
|
2020-05-05 19:33:33 +08:00
|
|
|
expect(scrollToSpy).toHaveBeenLastCalledWith(0, 800);
|
|
|
|
|
2022-05-04 23:14:07 +08:00
|
|
|
setProps({ offsetTop: 100 });
|
2022-10-30 14:35:45 +08:00
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash}"]`)!);
|
2022-10-11 22:22:36 +08:00
|
|
|
await waitFakeTimer();
|
2020-05-05 19:33:33 +08:00
|
|
|
expect(scrollToSpy).toHaveBeenLastCalledWith(0, 800);
|
2022-10-30 14:35:45 +08:00
|
|
|
|
2022-05-04 23:14:07 +08:00
|
|
|
setProps({ targetOffset: 200 });
|
2022-10-30 14:35:45 +08:00
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash}"]`)!);
|
2022-10-11 22:22:36 +08:00
|
|
|
await waitFakeTimer();
|
2020-05-05 19:33:33 +08:00
|
|
|
expect(scrollToSpy).toHaveBeenLastCalledWith(0, 800);
|
|
|
|
});
|
2022-03-31 11:17:17 +08:00
|
|
|
|
|
|
|
describe('getCurrentAnchor', () => {
|
2022-10-30 14:35:45 +08:00
|
|
|
it('getCurrentAnchor prop', () => {
|
2022-03-31 11:17:17 +08:00
|
|
|
const hash1 = getHashUrl();
|
|
|
|
const hash2 = getHashUrl();
|
|
|
|
const getCurrentAnchor = () => `#${hash2}`;
|
2022-10-30 14:35:45 +08:00
|
|
|
const { container } = render(
|
|
|
|
<Anchor getCurrentAnchor={getCurrentAnchor}>
|
2022-03-31 11:17:17 +08:00
|
|
|
<Link href={`#${hash1}`} title={hash1} />
|
|
|
|
<Link href={`#${hash2}`} title={hash2} />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
2022-04-19 16:44:47 +08:00
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
expect(container.querySelector(`.ant-anchor-link-title-active`)?.textContent).toBe(hash2);
|
2022-03-31 11:17:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/30584
|
2022-10-30 14:35:45 +08:00
|
|
|
it('should trigger onChange when have getCurrentAnchor', () => {
|
2022-03-31 11:17:17 +08:00
|
|
|
const hash1 = getHashUrl();
|
|
|
|
const hash2 = getHashUrl();
|
|
|
|
const onChange = jest.fn();
|
2022-10-30 14:35:45 +08:00
|
|
|
const { container } = render(
|
|
|
|
<Anchor onChange={onChange} getCurrentAnchor={() => hash1}>
|
2022-03-31 11:17:17 +08:00
|
|
|
<Link href={`#${hash1}`} title={hash1} />
|
|
|
|
<Link href={`#${hash2}`} title={hash2} />
|
|
|
|
</Anchor>,
|
2022-05-04 23:14:07 +08:00
|
|
|
// https://github.com/testing-library/react-testing-library/releases/tag/v13.0.0
|
|
|
|
// @ts-ignore
|
|
|
|
{ legacyRoot: true },
|
2022-03-31 11:17:17 +08:00
|
|
|
);
|
2022-04-19 16:44:47 +08:00
|
|
|
|
2022-03-31 11:17:17 +08:00
|
|
|
expect(onChange).toHaveBeenCalledTimes(1);
|
2022-10-30 14:35:45 +08:00
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash2}"]`)!);
|
2022-03-31 11:17:17 +08:00
|
|
|
expect(onChange).toHaveBeenCalledTimes(2);
|
2022-10-30 14:35:45 +08:00
|
|
|
expect(onChange).toHaveBeenLastCalledWith(`#${hash2}`);
|
2022-03-31 11:17:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/34784
|
2022-10-30 14:35:45 +08:00
|
|
|
it('getCurrentAnchor have default link as argument', () => {
|
2022-03-31 11:17:17 +08:00
|
|
|
const hash1 = getHashUrl();
|
|
|
|
const hash2 = getHashUrl();
|
|
|
|
const getCurrentAnchor = jest.fn();
|
2022-05-04 23:14:07 +08:00
|
|
|
const { container } = render(
|
2022-03-31 11:17:17 +08:00
|
|
|
<Anchor getCurrentAnchor={getCurrentAnchor}>
|
|
|
|
<Link href={`#${hash1}`} title={hash1} />
|
|
|
|
<Link href={`#${hash2}`} title={hash2} />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
|
|
|
|
2022-05-04 23:14:07 +08:00
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash1}"]`)!);
|
2022-03-31 11:17:17 +08:00
|
|
|
expect(getCurrentAnchor).toHaveBeenCalledWith(`#${hash1}`);
|
2022-05-04 23:14:07 +08:00
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash2}"]`)!);
|
2022-03-31 11:17:17 +08:00
|
|
|
expect(getCurrentAnchor).toHaveBeenCalledWith(`#${hash2}`);
|
|
|
|
});
|
2022-09-20 11:06:51 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/37627
|
2022-10-30 14:35:45 +08:00
|
|
|
it('should update active link when getCurrentAnchor changes', async () => {
|
2022-09-20 11:06:51 +08:00
|
|
|
const hash1 = getHashUrl();
|
|
|
|
const hash2 = getHashUrl();
|
|
|
|
const Demo: React.FC<{ current: string }> = ({ current }) => (
|
|
|
|
<Anchor getCurrentAnchor={() => `#${current}`}>
|
|
|
|
<Link href={`#${hash1}`} title={hash1} />
|
|
|
|
<Link href={`#${hash2}`} title={hash2} />
|
|
|
|
</Anchor>
|
|
|
|
);
|
|
|
|
const { container, rerender } = render(<Demo current={hash1} />);
|
|
|
|
expect(container.querySelector(`.ant-anchor-link-title-active`)?.textContent).toBe(hash1);
|
|
|
|
rerender(<Demo current={hash2} />);
|
|
|
|
expect(container.querySelector(`.ant-anchor-link-title-active`)?.textContent).toBe(hash2);
|
|
|
|
});
|
2022-10-30 14:35:45 +08:00
|
|
|
|
|
|
|
it('should render correctly when href is null', () => {
|
2022-10-17 21:36:56 +08:00
|
|
|
expect(() => {
|
|
|
|
render(
|
|
|
|
<Anchor>
|
|
|
|
<Link href={null as unknown as string} title="test" />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
|
|
|
fireEvent.scroll(window || document);
|
|
|
|
}).not.toThrow();
|
|
|
|
});
|
2022-03-31 11:17:17 +08:00
|
|
|
});
|
2020-04-30 10:33:29 +08:00
|
|
|
});
|