2016-12-14 14:48:09 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
|
|
|
import Anchor from '..';
|
|
|
|
|
|
|
|
const { Link } = Anchor;
|
|
|
|
|
|
|
|
describe('Anchor Render', () => {
|
|
|
|
it('Anchor render perfectly', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Anchor>
|
|
|
|
<Link href="#API" title="API" />
|
|
|
|
</Anchor>
|
|
|
|
);
|
|
|
|
|
|
|
|
wrapper.find('a[href="#API"]').simulate('click');
|
|
|
|
|
2017-09-20 16:26:18 +08:00
|
|
|
wrapper.instance().handleScroll();
|
|
|
|
expect(wrapper.instance().state).not.toBe(null);
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
2018-01-04 16:40:14 +08:00
|
|
|
|
|
|
|
it('Anchor render perfectly for complete href - click', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Anchor>
|
|
|
|
<Link href="http://www.example.com/#API" title="API" />
|
|
|
|
</Anchor>
|
|
|
|
);
|
|
|
|
wrapper.find('a[href="http://www.example.com/#API"]').simulate('click');
|
|
|
|
expect(wrapper.instance().state.activeLink).toBe('http://www.example.com/#API');
|
|
|
|
});
|
|
|
|
|
2018-05-25 17:02:24 +08:00
|
|
|
it('Anchor render perfectly for complete href - scroll', () => {
|
2018-01-04 16:40:14 +08:00
|
|
|
let root = document.getElementById('root');
|
|
|
|
if (!root) {
|
|
|
|
root = document.createElement('div', { id: 'root' });
|
|
|
|
root.id = 'root';
|
|
|
|
document.body.appendChild(root);
|
|
|
|
}
|
|
|
|
mount(<div id="API">Hello</div>, { attachTo: root });
|
|
|
|
const wrapper = mount(
|
|
|
|
<Anchor>
|
|
|
|
<Link href="http://www.example.com/#API" title="API" />
|
|
|
|
</Anchor>
|
|
|
|
);
|
|
|
|
wrapper.instance().handleScroll();
|
|
|
|
expect(wrapper.instance().state.activeLink).toBe('http://www.example.com/#API');
|
|
|
|
});
|
|
|
|
|
2018-05-25 17:15:49 +08:00
|
|
|
it('Anchor render perfectly for complete href - scrollTo', async () => {
|
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo');
|
2018-01-04 16:40:14 +08:00
|
|
|
let root = document.getElementById('root');
|
|
|
|
if (!root) {
|
|
|
|
root = document.createElement('div', { id: 'root' });
|
|
|
|
root.id = 'root';
|
|
|
|
document.body.appendChild(root);
|
|
|
|
}
|
|
|
|
mount(<div id="API">Hello</div>, { attachTo: root });
|
|
|
|
const wrapper = mount(
|
|
|
|
<Anchor>
|
|
|
|
<Link href="##API" title="API" />
|
|
|
|
</Anchor>
|
|
|
|
);
|
|
|
|
wrapper.instance().handleScrollTo('##API');
|
|
|
|
expect(wrapper.instance().state.activeLink).toBe('##API');
|
2018-05-25 17:15:49 +08:00
|
|
|
expect(scrollToSpy).not.toHaveBeenCalled();
|
2018-08-10 00:11:12 +08:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
2018-08-11 11:23:42 +08:00
|
|
|
expect(scrollToSpy).toHaveBeenCalled();
|
2018-05-25 17:15:49 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove listener when unmount', async () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Anchor>
|
|
|
|
<Link href="#API" title="API" />
|
|
|
|
</Anchor>
|
|
|
|
);
|
|
|
|
const removeListenerSpy = jest.spyOn(wrapper.instance().scrollEvent, 'remove');
|
|
|
|
wrapper.unmount();
|
|
|
|
expect(removeListenerSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should unregister link when unmount children', async () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Anchor>
|
|
|
|
<Link href="#API" title="API" />
|
|
|
|
</Anchor>
|
|
|
|
);
|
|
|
|
expect(wrapper.instance().links).toEqual(['#API']);
|
|
|
|
wrapper.setProps({ children: null });
|
|
|
|
expect(wrapper.instance().links).toEqual([]);
|
2018-01-04 16:40:14 +08:00
|
|
|
});
|
2018-07-16 14:20:41 +08:00
|
|
|
|
|
|
|
it('should update links when link href update', async () => {
|
|
|
|
let anchorInstance = null;
|
|
|
|
function AnchorUpdate({ href }) {
|
|
|
|
return (
|
|
|
|
<Anchor ref={c => anchorInstance = c}>
|
|
|
|
<Link href={href} title="API" />
|
|
|
|
</Anchor>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const wrapper = mount(<AnchorUpdate href="#API" />);
|
|
|
|
|
|
|
|
expect(anchorInstance.links).toEqual(['#API']);
|
|
|
|
wrapper.setProps({ href: '#API_1' });
|
|
|
|
expect(anchorInstance.links).toEqual(['#API_1']);
|
|
|
|
});
|
2018-08-28 20:20:46 +08:00
|
|
|
|
|
|
|
it('Anchor onClick event', () => {
|
|
|
|
let event;
|
|
|
|
let link;
|
|
|
|
const handleClick = (...arg) => ([event, link] = arg);
|
|
|
|
|
|
|
|
const href = '#API';
|
|
|
|
const title = 'API';
|
|
|
|
|
|
|
|
const wrapper = mount(
|
|
|
|
<Anchor onClick={handleClick}>
|
|
|
|
<Link href={href} title={title} />
|
|
|
|
</Anchor>
|
|
|
|
);
|
|
|
|
|
|
|
|
wrapper.find(`a[href="${href}"]`).simulate('click');
|
|
|
|
|
|
|
|
wrapper.instance().handleScroll();
|
|
|
|
expect(event).not.toBe(undefined);
|
|
|
|
expect(link).toEqual({ href, title });
|
|
|
|
});
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|