2023-04-23 19:59:29 +08:00
|
|
|
import React, { useState } from 'react';
|
2024-04-08 14:04:08 +08:00
|
|
|
import { resetWarned } from 'rc-util/lib/warning';
|
2022-12-27 17:14:35 +08:00
|
|
|
import scrollIntoView from 'scroll-into-view-if-needed';
|
|
|
|
|
2020-04-30 10:33:29 +08:00
|
|
|
import Anchor from '..';
|
2023-03-19 16:17:08 +08:00
|
|
|
import { act, fireEvent, render, waitFakeTimer } from '../../../tests/utils';
|
2023-04-23 19:59:29 +08:00
|
|
|
import Button from '../../button';
|
2023-03-19 16:17:08 +08:00
|
|
|
import type { AnchorDirection } from '../Anchor';
|
2020-04-30 10:33:29 +08:00
|
|
|
|
|
|
|
const { Link } = Anchor;
|
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
function createDiv() {
|
2020-04-30 10:33:29 +08:00
|
|
|
const root = document.createElement('div');
|
|
|
|
document.body.appendChild(root);
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
let idCounter = 0;
|
|
|
|
const getHashUrl = () => `Anchor-API-${idCounter++}`;
|
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.mock('scroll-into-view-if-needed', () => jest.fn());
|
2022-12-27 17:14:35 +08:00
|
|
|
|
2023-06-18 18:55:36 +08:00
|
|
|
Object.defineProperty(window, 'location', {
|
|
|
|
value: {
|
|
|
|
replace: jest.fn(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-04-30 10:33:29 +08:00
|
|
|
describe('Anchor Render', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const getBoundingClientRectMock = jest.spyOn(
|
|
|
|
HTMLHeadingElement.prototype,
|
|
|
|
'getBoundingClientRect',
|
|
|
|
);
|
|
|
|
const getClientRectsMock = jest.spyOn(HTMLHeadingElement.prototype, 'getClientRects');
|
|
|
|
const scrollIntoViewMock = jest.createMockFromModule<any>('scroll-into-view-if-needed');
|
2020-04-30 10:33:29 +08:00
|
|
|
|
|
|
|
beforeAll(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useFakeTimers();
|
2020-04-30 10:33:29 +08:00
|
|
|
getBoundingClientRectMock.mockReturnValue({
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
top: 1000,
|
|
|
|
} as DOMRect);
|
2022-12-27 17:14:35 +08:00
|
|
|
getClientRectsMock.mockReturnValue([1] as unknown as DOMRectList);
|
2020-04-30 10:33:29 +08:00
|
|
|
});
|
|
|
|
|
2022-10-17 21:36:56 +08:00
|
|
|
beforeEach(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useFakeTimers();
|
|
|
|
scrollIntoViewMock.mockReset();
|
2022-10-17 21:36:56 +08:00
|
|
|
});
|
|
|
|
|
2022-10-11 22:22:36 +08:00
|
|
|
afterEach(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.clearAllTimers();
|
|
|
|
jest.useRealTimers();
|
2022-10-11 22:22:36 +08:00
|
|
|
});
|
|
|
|
|
2020-04-30 10:33:29 +08:00
|
|
|
afterAll(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.clearAllTimers();
|
|
|
|
jest.useRealTimers();
|
2020-04-30 10:33:29 +08:00
|
|
|
getBoundingClientRectMock.mockRestore();
|
|
|
|
getClientRectsMock.mockRestore();
|
|
|
|
});
|
|
|
|
|
2022-12-27 17:14:35 +08:00
|
|
|
it('renders items correctly', () => {
|
|
|
|
const { container, asFragment } = render(
|
|
|
|
<Anchor
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
key: '1',
|
2024-07-23 22:22:24 +08:00
|
|
|
href: '#anchor-demo-basic',
|
2022-12-27 17:14:35 +08:00
|
|
|
title: 'Item Basic Demo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '2',
|
2024-07-23 22:22:24 +08:00
|
|
|
href: '#anchor-demo-static',
|
2022-12-27 17:14:35 +08:00
|
|
|
title: 'Static demo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '3',
|
|
|
|
href: '#api',
|
|
|
|
title: 'API',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
key: '4',
|
|
|
|
href: '#anchor-props',
|
|
|
|
title: 'Anchor Props',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
key: '5',
|
|
|
|
href: '#link-props',
|
|
|
|
title: 'Link Props',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
expect(container.querySelectorAll('.ant-anchor .ant-anchor-link').length).toBe(5);
|
|
|
|
const linkTitles = Array.from(container.querySelector('.ant-anchor')?.childNodes!).map((n) =>
|
|
|
|
(n as HTMLElement).querySelector('.ant-anchor-link-title'),
|
|
|
|
);
|
2024-07-23 22:22:24 +08:00
|
|
|
expect((linkTitles[1] as HTMLAnchorElement).href).toContain('#anchor-demo-basic');
|
|
|
|
expect((linkTitles[2] as HTMLAnchorElement).href).toContain('#anchor-demo-static');
|
2022-12-27 17:14:35 +08:00
|
|
|
expect((linkTitles[3] as HTMLAnchorElement).href).toContain('#api');
|
|
|
|
expect(
|
|
|
|
(
|
|
|
|
container.querySelector(
|
|
|
|
'.ant-anchor .ant-anchor-link .ant-anchor-link .ant-anchor-link-title',
|
|
|
|
) as HTMLAnchorElement
|
|
|
|
)?.href,
|
|
|
|
).toContain('#anchor-props');
|
|
|
|
expect(
|
|
|
|
(
|
|
|
|
container.querySelector(
|
|
|
|
'.ant-anchor .ant-anchor-link .ant-anchor-link .ant-anchor-link .ant-anchor-link-title',
|
|
|
|
) as HTMLAnchorElement
|
|
|
|
)?.href,
|
|
|
|
).toContain('#link-props');
|
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders items correctly#horizontal', () => {
|
|
|
|
const { container, asFragment } = render(
|
|
|
|
<Anchor
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
key: '1',
|
2024-07-23 22:22:24 +08:00
|
|
|
href: '#anchor-demo-basic',
|
2022-12-27 17:14:35 +08:00
|
|
|
title: 'Item Basic Demo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '2',
|
2024-07-23 22:22:24 +08:00
|
|
|
href: '#anchor-demo-static',
|
2022-12-27 17:14:35 +08:00
|
|
|
title: 'Static demo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '3',
|
|
|
|
href: '#api',
|
|
|
|
title: 'API',
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
expect(container.querySelectorAll('.ant-anchor .ant-anchor-link').length).toBe(3);
|
|
|
|
const linkTitles = Array.from(container.querySelector('.ant-anchor')?.childNodes!).map((n) =>
|
|
|
|
(n as HTMLElement).querySelector('.ant-anchor-link-title'),
|
|
|
|
);
|
2024-07-23 22:22:24 +08:00
|
|
|
expect((linkTitles[1] as HTMLAnchorElement).href).toContain('#anchor-demo-basic');
|
|
|
|
expect((linkTitles[2] as HTMLAnchorElement).href).toContain('#anchor-demo-static');
|
2022-12-27 17:14:35 +08:00
|
|
|
expect((linkTitles[3] as HTMLAnchorElement).href).toContain('#api');
|
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('render items and ignore jsx children', () => {
|
|
|
|
const { container, asFragment } = render(
|
|
|
|
<Anchor
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
key: '1',
|
2024-07-23 22:22:24 +08:00
|
|
|
href: '#anchor-demo-basic',
|
2022-12-27 17:14:35 +08:00
|
|
|
title: 'Item Basic Demo',
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<Link href="#api" title="API" />
|
2020-04-30 10:33:29 +08:00
|
|
|
</Anchor>,
|
|
|
|
);
|
2022-12-27 17:14:35 +08:00
|
|
|
expect(container.querySelectorAll('.ant-anchor .ant-anchor-link').length).toBe(1);
|
|
|
|
expect(
|
|
|
|
(container.querySelector('.ant-anchor .ant-anchor-link-title') as HTMLAnchorElement).href,
|
2024-07-23 22:22:24 +08:00
|
|
|
).toContain('#anchor-demo-basic');
|
2022-12-27 17:14:35 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
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-12-27 17:14:35 +08:00
|
|
|
<Anchor
|
|
|
|
prefixCls="ant-anchor"
|
|
|
|
direction="horizontal"
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
key: hash,
|
|
|
|
title: hash,
|
|
|
|
href: `http://www.example.com/#${hash}`,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>,
|
2020-04-30 10:33:29 +08:00
|
|
|
);
|
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();
|
2023-06-07 21:59:21 +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 () => {
|
2023-06-07 21:59:21 +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(
|
2022-12-27 17:14:35 +08:00
|
|
|
<Anchor items={[{ key: 'Q1', title: 'Q1', href: '/#/faq?locale=en#Q1' }]} />,
|
2020-04-30 10:33:29 +08:00
|
|
|
);
|
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();
|
2023-06-07 21:59:21 +08:00
|
|
|
const root = createDiv();
|
|
|
|
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(
|
2022-12-27 17:14:35 +08:00
|
|
|
<Anchor
|
|
|
|
onChange={onChange}
|
|
|
|
items={[
|
|
|
|
{ key: hash1, href: `#${hash1}`, title: hash1 },
|
|
|
|
{ key: hash2, href: `#${hash2}`, title: hash2 },
|
|
|
|
]}
|
|
|
|
/>,
|
2020-04-30 10:33:29 +08:00
|
|
|
);
|
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(
|
2022-12-27 17:14:35 +08:00
|
|
|
<Anchor items={[{ key: hash, href: `#${hash}`, title: hash }]} />,
|
2020-04-30 10:33:29 +08:00
|
|
|
);
|
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 }) {
|
2022-12-27 17:14:35 +08:00
|
|
|
return <Anchor items={[{ key: hash, href, title: hash }]} />;
|
2020-04-30 10:33:29 +08:00
|
|
|
}
|
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
|
|
|
|
2023-06-07 21:59:21 +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(
|
2022-12-27 17:14:35 +08:00
|
|
|
<Anchor items={[{ key: hash, href: `#${hash}`, title: hash }]} />,
|
2021-08-30 12:10:02 +08:00
|
|
|
);
|
2022-04-19 16:44:47 +08:00
|
|
|
|
2022-05-04 23:14:07 +08:00
|
|
|
const setProps = (props: Record<string, any>) =>
|
2022-12-27 17:14:35 +08:00
|
|
|
rerender(<Anchor {...props} items={[{ key: hash, href: `#${hash}`, title: hash }]} />);
|
2022-05-04 23:14:07 +08:00
|
|
|
|
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
|
|
|
|
2023-06-07 21:59:21 +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(
|
2022-12-27 17:14:35 +08:00
|
|
|
<Anchor items={[{ key: hash, href: `#${hash}`, title: hash }]} />,
|
2020-04-30 10:33:29 +08:00
|
|
|
);
|
2022-04-19 16:44:47 +08:00
|
|
|
|
2022-05-04 23:14:07 +08:00
|
|
|
const setProps = (props: Record<string, any>) =>
|
2022-12-27 17:14:35 +08:00
|
|
|
rerender(<Anchor {...props} items={[{ key: hash, href: `#${hash}`, title: hash }]} />);
|
2022-05-04 23:14:07 +08:00
|
|
|
|
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(
|
2022-12-27 17:14:35 +08:00
|
|
|
<Anchor onClick={handleClick} items={[{ key: hash, href, title }]} />,
|
2022-10-30 14:35:45 +08:00
|
|
|
);
|
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
|
|
|
});
|
|
|
|
|
2023-06-18 18:55:36 +08:00
|
|
|
it('replaces item href in browser history', () => {
|
|
|
|
const hash = getHashUrl();
|
|
|
|
|
|
|
|
const href = `#${hash}`;
|
|
|
|
const title = hash;
|
|
|
|
const { container } = render(<Anchor replace items={[{ key: hash, href, title }]} />);
|
|
|
|
|
|
|
|
fireEvent.click(container.querySelector(`a[href="${href}"]`)!);
|
|
|
|
expect(window.location.replace).toHaveBeenCalledWith(href);
|
|
|
|
});
|
|
|
|
|
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();
|
2023-06-07 21:59:21 +08:00
|
|
|
const onChange = jest.fn();
|
2022-10-30 14:35:45 +08:00
|
|
|
const { container } = render(
|
2022-12-27 17:14:35 +08:00
|
|
|
<Anchor
|
|
|
|
onChange={onChange}
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
key: hash1,
|
|
|
|
href: `#${hash1}`,
|
|
|
|
title: hash1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: hash2,
|
|
|
|
href: `#${hash2}`,
|
|
|
|
title: hash2,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>,
|
2022-05-04 23:14:07 +08:00
|
|
|
// https://github.com/testing-library/react-testing-library/releases/tag/v13.0.0
|
|
|
|
{ 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
|
|
|
});
|
|
|
|
|
2023-04-23 19:59:29 +08:00
|
|
|
it('should be used the latest onChange method', () => {
|
|
|
|
const hash1 = getHashUrl();
|
|
|
|
const hash2 = getHashUrl();
|
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
const beforeFn = jest.fn();
|
|
|
|
const afterFn = jest.fn();
|
2023-04-23 19:59:29 +08:00
|
|
|
|
|
|
|
const Demo: React.FC = () => {
|
|
|
|
const [trigger, setTrigger] = useState(false);
|
|
|
|
const onChange = trigger ? afterFn : beforeFn;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Button className="test-button" onClick={() => setTrigger(true)} />
|
|
|
|
<Anchor
|
|
|
|
onChange={onChange}
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
key: hash1,
|
|
|
|
href: `#${hash1}`,
|
|
|
|
title: hash1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: hash2,
|
|
|
|
href: `#${hash2}`,
|
|
|
|
title: hash2,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const { container } = render(<Demo />);
|
|
|
|
expect(beforeFn).toHaveBeenCalled();
|
|
|
|
expect(afterFn).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
beforeFn.mockClear();
|
|
|
|
afterFn.mockClear();
|
|
|
|
|
|
|
|
fireEvent.click(container.querySelector('.test-button')!);
|
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash2}"]`)!);
|
|
|
|
|
|
|
|
expect(beforeFn).not.toHaveBeenCalled();
|
|
|
|
expect(afterFn).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-10-30 14:35:45 +08:00
|
|
|
it('handles invalid hash correctly', () => {
|
2022-05-04 23:14:07 +08:00
|
|
|
const { container } = render(
|
2023-02-23 21:56:43 +08:00
|
|
|
<Anchor items={[{ key: 'title', href: 'nonexistent', title: 'title' }]} />,
|
2020-05-05 19:33:33 +08:00
|
|
|
);
|
|
|
|
|
2023-02-23 21:56:43 +08:00
|
|
|
const link = container.querySelector(`a[href="nonexistent"]`)!;
|
2022-10-30 14:35:45 +08:00
|
|
|
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();
|
|
|
|
|
2023-06-07 21:59:21 +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-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();
|
|
|
|
|
2023-06-07 21:59:21 +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 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();
|
2023-06-07 21:59:21 +08:00
|
|
|
expect(scrollToSpy).toHaveBeenLastCalledWith(0, 800);
|
2020-05-05 19:33:33 +08:00
|
|
|
|
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();
|
2023-06-07 21:59:21 +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();
|
2023-06-07 21:59:21 +08:00
|
|
|
expect(scrollToSpy).toHaveBeenLastCalledWith(0, 800);
|
2020-05-05 19:33:33 +08:00
|
|
|
});
|
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(
|
2022-12-27 17:14:35 +08:00
|
|
|
<Anchor
|
|
|
|
getCurrentAnchor={getCurrentAnchor}
|
|
|
|
items={[
|
|
|
|
{ key: hash1, href: `#${hash1}`, title: hash1 },
|
|
|
|
{ key: hash2, href: `#${hash2}`, title: hash2 },
|
|
|
|
]}
|
|
|
|
/>,
|
2022-03-31 11:17:17 +08:00
|
|
|
);
|
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();
|
2023-06-07 21:59:21 +08:00
|
|
|
const onChange = jest.fn();
|
2022-10-30 14:35:45 +08:00
|
|
|
const { container } = render(
|
2022-12-27 17:14:35 +08:00
|
|
|
<Anchor
|
|
|
|
onChange={onChange}
|
|
|
|
getCurrentAnchor={() => hash1}
|
|
|
|
items={[
|
|
|
|
{ key: hash1, href: `#${hash1}`, title: hash1 },
|
|
|
|
{ key: hash2, href: `#${hash2}`, title: hash2 },
|
|
|
|
]}
|
|
|
|
/>,
|
2022-05-04 23:14:07 +08:00
|
|
|
// https://github.com/testing-library/react-testing-library/releases/tag/v13.0.0
|
|
|
|
{ legacyRoot: true },
|
2022-03-31 11:17:17 +08:00
|
|
|
);
|
2022-04-19 16:44:47 +08:00
|
|
|
|
2023-07-31 18:26:33 +08:00
|
|
|
// Should be 2 times:
|
|
|
|
// 1. ''
|
|
|
|
// 2. hash1 (Since `getCurrentAnchor` still return same hash)
|
2022-03-31 11:17:17 +08:00
|
|
|
expect(onChange).toHaveBeenCalledTimes(2);
|
2023-07-31 18:26:33 +08:00
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash2}"]`)!);
|
|
|
|
expect(onChange).toHaveBeenCalledTimes(3);
|
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();
|
2023-06-07 21:59:21 +08:00
|
|
|
const getCurrentAnchor = jest.fn();
|
2022-05-04 23:14:07 +08:00
|
|
|
const { container } = render(
|
2022-12-27 17:14:35 +08:00
|
|
|
<Anchor
|
|
|
|
getCurrentAnchor={getCurrentAnchor}
|
|
|
|
items={[
|
|
|
|
{ key: hash1, href: `#${hash1}`, title: hash1 },
|
|
|
|
{ key: hash2, href: `#${hash2}`, title: hash2 },
|
|
|
|
]}
|
|
|
|
/>,
|
2022-03-31 11:17:17 +08:00
|
|
|
);
|
|
|
|
|
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 }) => (
|
2022-12-27 17:14:35 +08:00
|
|
|
<Anchor
|
|
|
|
getCurrentAnchor={() => `#${current}`}
|
|
|
|
items={[
|
|
|
|
{ key: hash1, href: `#${hash1}`, title: hash1 },
|
|
|
|
{ key: hash2, href: `#${hash2}`, title: hash2 },
|
|
|
|
]}
|
|
|
|
/>
|
2022-09-20 11:06:51 +08:00
|
|
|
);
|
|
|
|
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(
|
2022-12-27 17:14:35 +08:00
|
|
|
<Anchor items={[{ key: 'test', href: null as unknown as string, title: 'test' }]} />,
|
2022-10-17 21:36:56 +08:00
|
|
|
);
|
|
|
|
fireEvent.scroll(window || document);
|
|
|
|
}).not.toThrow();
|
|
|
|
});
|
2023-07-31 18:26:33 +08:00
|
|
|
|
|
|
|
it('should repeat trigger when scrolling', () => {
|
|
|
|
const getCurrentAnchor = jest.fn();
|
|
|
|
render(
|
|
|
|
<Anchor
|
|
|
|
getCurrentAnchor={getCurrentAnchor}
|
|
|
|
items={[{ key: 'test', href: null as unknown as string, title: 'test' }]}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
|
|
|
for (let i = 0; i < 100; i += 1) {
|
|
|
|
getCurrentAnchor.mockReset();
|
|
|
|
fireEvent.scroll(window || document);
|
|
|
|
expect(getCurrentAnchor).toHaveBeenCalled();
|
|
|
|
}
|
|
|
|
});
|
2022-03-31 11:17:17 +08:00
|
|
|
});
|
2022-11-30 15:55:43 +08:00
|
|
|
|
2022-12-27 17:14:35 +08:00
|
|
|
describe('horizontal anchor', () => {
|
|
|
|
describe('scroll x', () => {
|
|
|
|
it('targetOffset horizontal', async () => {
|
|
|
|
const hash = getHashUrl();
|
2023-06-07 21:59:21 +08:00
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo');
|
|
|
|
const root = createDiv();
|
2022-12-27 17:14:35 +08:00
|
|
|
render(<h1 id={hash}>Hello</h1>, { container: root });
|
|
|
|
const { container, rerender } = render(
|
|
|
|
<Anchor
|
|
|
|
direction="horizontal"
|
|
|
|
items={[
|
2022-11-30 15:55:43 +08:00
|
|
|
{
|
2022-12-27 17:14:35 +08:00
|
|
|
key: hash,
|
|
|
|
href: `#${hash}`,
|
|
|
|
title: hash,
|
2022-11-30 15:55:43 +08:00
|
|
|
},
|
2022-12-27 17:14:35 +08:00
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
const setProps = (props: Record<string, any>) =>
|
|
|
|
rerender(
|
|
|
|
<Anchor
|
|
|
|
{...props}
|
|
|
|
direction="horizontal"
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
key: hash,
|
|
|
|
href: `#${hash}`,
|
|
|
|
title: hash,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash}"]`)!);
|
|
|
|
await waitFakeTimer();
|
|
|
|
|
|
|
|
expect(scrollIntoView).toHaveBeenCalled();
|
|
|
|
expect(scrollToSpy).toHaveBeenLastCalledWith(0, 1000);
|
|
|
|
|
|
|
|
setProps({ offsetTop: 100 });
|
|
|
|
|
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash}"]`)!);
|
|
|
|
await waitFakeTimer();
|
|
|
|
expect(scrollToSpy).toHaveBeenLastCalledWith(0, 900);
|
|
|
|
|
|
|
|
setProps({ targetOffset: 200 });
|
|
|
|
fireEvent.click(container.querySelector(`a[href="#${hash}"]`)!);
|
|
|
|
await waitFakeTimer();
|
|
|
|
expect(scrollToSpy).toHaveBeenLastCalledWith(0, 800);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('test direction prop', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Anchor
|
|
|
|
direction="horizontal"
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
key: '1',
|
2024-07-23 22:22:24 +08:00
|
|
|
href: '#anchor-demo-basic',
|
2022-12-27 17:14:35 +08:00
|
|
|
title: 'Item Basic Demo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '2',
|
2024-07-23 22:22:24 +08:00
|
|
|
href: '#anchor-demo-static',
|
2022-12-27 17:14:35 +08:00
|
|
|
title: 'Static demo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '3',
|
|
|
|
href: '#api',
|
|
|
|
title: 'API',
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
expect(container.querySelectorAll('.ant-anchor-ink').length).toBe(1);
|
|
|
|
expect(
|
|
|
|
container
|
|
|
|
.querySelector('.ant-anchor-wrapper')
|
|
|
|
?.classList.contains('ant-anchor-wrapper-horizontal'),
|
|
|
|
).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('nested children via items should be filtered out when direction is horizontal', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Anchor
|
|
|
|
direction="horizontal"
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
key: '1',
|
2024-07-23 22:22:24 +08:00
|
|
|
href: '#anchor-demo-basic',
|
2022-12-27 17:14:35 +08:00
|
|
|
title: 'Item Basic Demo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '2',
|
2024-07-23 22:22:24 +08:00
|
|
|
href: '#anchor-demo-static',
|
2022-12-27 17:14:35 +08:00
|
|
|
title: 'Static demo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '3',
|
|
|
|
href: '#api',
|
|
|
|
title: 'API',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
key: '4',
|
|
|
|
href: '#anchor-props',
|
|
|
|
title: 'Anchor Props',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '5',
|
|
|
|
href: '#link-props',
|
|
|
|
title: 'Link Props',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
expect(container.querySelectorAll('.ant-anchor-link').length).toBe(3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('nested children via jsx should be filtered out when direction is horizontal', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Anchor direction="horizontal">
|
2024-07-23 22:22:24 +08:00
|
|
|
<Link href="#anchor-demo-basic" title="Basic demo" />
|
|
|
|
<Link href="#anchor-demo-static" title="Static demo" />
|
2022-12-27 17:14:35 +08:00
|
|
|
<Link href="#api" title="API">
|
|
|
|
<Link href="#anchor-props" title="Anchor Props" />
|
|
|
|
<Link href="#link-props" title="Link Props" />
|
|
|
|
</Link>
|
|
|
|
</Anchor>,
|
|
|
|
);
|
|
|
|
expect(container.querySelectorAll('.ant-anchor-link').length).toBe(3);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('deprecated/legacy jsx syntax', () => {
|
|
|
|
it('renders jsx correctly', () => {
|
|
|
|
const hash = getHashUrl();
|
|
|
|
const { container } = render(
|
|
|
|
<Anchor>
|
|
|
|
<Link href={`#${hash}`} title={hash} />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
|
|
|
expect(container.querySelector(`a[href="#${hash}"]`)).not.toBe(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('actives the target when clicking a link', async () => {
|
|
|
|
const hash = getHashUrl();
|
|
|
|
const { container } = render(
|
|
|
|
<Anchor prefixCls="ant-anchor">
|
|
|
|
<Link href={`http://www.example.com/#${hash}`} title={hash} />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
|
|
|
const link = container.querySelector(`a[href="http://www.example.com/#${hash}"]`)!;
|
|
|
|
fireEvent.click(link);
|
|
|
|
await waitFakeTimer();
|
2023-06-07 21:59:21 +08:00
|
|
|
expect(link.classList).toContain('ant-anchor-link-title-active');
|
2022-12-27 17:14:35 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('scrolls the page when clicking a link', async () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const root = createDiv();
|
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo');
|
2022-12-27 17:14:35 +08:00
|
|
|
render(<div id="/faq?locale=en#Q1">Q1</div>, { container: root });
|
|
|
|
const { container } = render(
|
|
|
|
<Anchor>
|
|
|
|
<Link href="/#/faq?locale=en#Q1" title="Q1" />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
|
|
|
const link = container.querySelector(`a[href="/#/faq?locale=en#Q1"]`)!;
|
|
|
|
fireEvent.click(link);
|
|
|
|
await waitFakeTimer();
|
|
|
|
expect(scrollToSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handleScroll should not be triggered when scrolling caused by clicking a link', async () => {
|
|
|
|
const hash1 = getHashUrl();
|
|
|
|
const hash2 = getHashUrl();
|
2023-06-07 21:59:21 +08:00
|
|
|
const root = createDiv();
|
|
|
|
const onChange = jest.fn();
|
2022-12-27 17:14:35 +08:00
|
|
|
render(
|
|
|
|
<div>
|
|
|
|
<div id={hash1}>Hello</div>
|
|
|
|
<div id={hash2}>World</div>
|
|
|
|
</div>,
|
|
|
|
{ container: root },
|
|
|
|
);
|
|
|
|
const { container } = render(
|
|
|
|
<Anchor onChange={onChange}>
|
|
|
|
<Link href={`#${hash1}`} title={hash1} />
|
|
|
|
<Link href={`#${hash2}`} title={hash2} />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
|
|
|
onChange.mockClear();
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
await waitFakeTimer();
|
|
|
|
// if the scroll listener is triggered, we will get 2 onChange, now we expect only 1.
|
|
|
|
expect(onChange).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update DOM when children are unmounted', () => {
|
|
|
|
const hash = getHashUrl();
|
|
|
|
const { container, rerender } = render(
|
|
|
|
<Anchor>
|
|
|
|
<Link href={`#${hash}`} title={hash} />
|
|
|
|
</Anchor>,
|
|
|
|
);
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update DOM when link href is changed', async () => {
|
|
|
|
const hash = getHashUrl();
|
|
|
|
function AnchorUpdate({ href }: { href: string }) {
|
|
|
|
return (
|
|
|
|
<Anchor>
|
|
|
|
<Link href={href} title={hash} />
|
|
|
|
</Anchor>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const { container, rerender } = render(<AnchorUpdate href={`#${hash}`} />);
|
|
|
|
|
|
|
|
expect(container.querySelector(`a[href="#${hash}"]`)).toBeTruthy();
|
|
|
|
rerender(<AnchorUpdate href={`#${hash}_1`} />);
|
|
|
|
expect(container.querySelector(`a[href="#${hash}_1"]`)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles invalid hash correctly', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Anchor>
|
2023-02-23 21:56:43 +08:00
|
|
|
<Link href="nonexistent" title="title" />
|
2022-12-27 17:14:35 +08:00
|
|
|
</Anchor>,
|
|
|
|
);
|
|
|
|
|
2023-02-23 21:56:43 +08:00
|
|
|
const link = container.querySelector(`a[href="nonexistent"]`)!;
|
2022-12-27 17:14:35 +08:00
|
|
|
fireEvent.click(link);
|
|
|
|
expect(container.querySelector(`.ant-anchor-link-title-active`)?.textContent).toBe('title');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('warning', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
let errSpy: jest.SpyInstance;
|
2022-12-27 17:14:35 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
resetWarned();
|
2023-06-07 21:59:21 +08:00
|
|
|
errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2022-12-27 17:14:35 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
errSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('warning nested children when direction is horizontal ', () => {
|
|
|
|
render(
|
|
|
|
<Anchor
|
|
|
|
direction="horizontal"
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
key: '1',
|
2024-07-23 22:22:24 +08:00
|
|
|
href: '#anchor-demo-basic',
|
2022-12-27 17:14:35 +08:00
|
|
|
title: 'Item Basic Demo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '2',
|
2024-07-23 22:22:24 +08:00
|
|
|
href: '#anchor-demo-static',
|
2022-12-27 17:14:35 +08:00
|
|
|
title: 'Static demo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '3',
|
|
|
|
href: '#api',
|
|
|
|
title: 'API',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
key: '4',
|
|
|
|
href: '#anchor-props',
|
|
|
|
title: 'Anchor Props',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
expect(errSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Anchor] `Anchor items#children` is not supported when `Anchor` direction is horizontal.',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('deprecated jsx style', () => {
|
|
|
|
render(
|
|
|
|
<Anchor direction="horizontal">
|
2024-07-23 22:22:24 +08:00
|
|
|
<Link href="#anchor-demo-basic" title="Basic demo" />
|
|
|
|
<Link href="#anchor-demo-static" title="Static demo" />
|
2022-12-27 17:14:35 +08:00
|
|
|
</Anchor>,
|
|
|
|
);
|
|
|
|
expect(errSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Anchor] `Anchor children` is deprecated. Please use `items` instead.',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('deprecated jsx style for direction#vertical', () => {
|
|
|
|
render(
|
|
|
|
<Anchor>
|
2024-07-23 22:22:24 +08:00
|
|
|
<Link href="#anchor-demo-basic" title="Basic demo" />
|
|
|
|
<Link href="#anchor-demo-static" title="Static demo" />
|
2022-12-27 17:14:35 +08:00
|
|
|
</Anchor>,
|
|
|
|
);
|
|
|
|
expect(errSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Anchor] `Anchor children` is deprecated. Please use `items` instead.',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('deprecated jsx style for direction#vertical 1: with nested children', () => {
|
|
|
|
render(
|
|
|
|
<Anchor direction="horizontal">
|
|
|
|
<Link href="#api" title="API">
|
|
|
|
<Link href="#anchor-props" title="Anchor Props" />
|
|
|
|
</Link>
|
|
|
|
</Anchor>,
|
|
|
|
);
|
|
|
|
expect(errSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Anchor] `Anchor children` is deprecated. Please use `items` instead.',
|
|
|
|
);
|
|
|
|
expect(errSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Anchor.Link] `Anchor.Link children` is not supported when `Anchor` direction is horizontal',
|
|
|
|
);
|
|
|
|
});
|
2023-03-19 16:17:08 +08:00
|
|
|
it('switch direction', async () => {
|
|
|
|
const Foo: React.FC = () => {
|
|
|
|
const [direction, setDirection] = useState<AnchorDirection>('vertical');
|
|
|
|
const toggle = () => {
|
|
|
|
setDirection(direction === 'vertical' ? 'horizontal' : 'vertical');
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<button onClick={toggle} type="button">
|
|
|
|
toggle
|
|
|
|
</button>
|
|
|
|
<Anchor
|
|
|
|
direction={direction}
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
title: 'part-1',
|
|
|
|
href: 'part-1',
|
|
|
|
key: 'part-1',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'part-2',
|
|
|
|
href: 'part-2',
|
|
|
|
key: 'part-2',
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
const wrapper = await render(<Foo />);
|
|
|
|
(await wrapper.findByText('part-1')).click();
|
|
|
|
await waitFakeTimer();
|
|
|
|
const ink = wrapper.container.querySelector<HTMLSpanElement>('.ant-anchor-ink')!;
|
|
|
|
const toggleButton = wrapper.container.querySelector('button')!;
|
|
|
|
|
|
|
|
fireEvent.click(toggleButton);
|
2023-06-07 21:59:21 +08:00
|
|
|
act(() => jest.runAllTimers());
|
2023-03-19 16:17:08 +08:00
|
|
|
expect(!!ink.style.left).toBe(true);
|
|
|
|
expect(!!ink.style.width).toBe(true);
|
|
|
|
expect(ink.style.top).toBe('');
|
|
|
|
expect(ink.style.height).toBe('');
|
|
|
|
|
|
|
|
fireEvent.click(toggleButton);
|
2023-06-07 21:59:21 +08:00
|
|
|
act(() => jest.runAllTimers());
|
2023-03-19 16:17:08 +08:00
|
|
|
expect(!!ink.style.top).toBe(true);
|
|
|
|
expect(!!ink.style.height).toBe(true);
|
|
|
|
expect(ink.style.left).toBe('');
|
|
|
|
expect(ink.style.width).toBe('');
|
|
|
|
});
|
2022-11-30 15:55:43 +08:00
|
|
|
});
|
2020-04-30 10:33:29 +08:00
|
|
|
});
|