2019-01-12 03:33:27 +00:00
|
|
|
import { mount } from '@vue/test-utils';
|
2020-07-25 07:46:49 +00:00
|
|
|
import * as Vue from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
import { asyncExpect } from '@/tests/utils';
|
|
|
|
import Anchor from '..';
|
2018-06-04 09:09:01 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const { Link } = Anchor;
|
2018-06-04 09:09:01 +00:00
|
|
|
|
2020-10-22 15:27:18 +00:00
|
|
|
let idCounter = 0;
|
|
|
|
const getHashUrl = () => `Anchor-API-${idCounter++}`;
|
|
|
|
|
2018-06-04 09:09:01 +00:00
|
|
|
describe('Anchor Render', () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
it('Anchor render perfectly', done => {
|
2020-10-22 15:27:18 +00:00
|
|
|
const hash = getHashUrl();
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper = mount(
|
|
|
|
{
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Anchor ref="anchor">
|
2020-10-22 15:27:18 +00:00
|
|
|
<Link href={`#${hash}`} title={hash} />
|
2019-01-12 03:33:27 +00:00
|
|
|
</Anchor>
|
|
|
|
);
|
|
|
|
},
|
2018-06-04 09:09:01 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
{ sync: false },
|
|
|
|
);
|
2018-06-04 09:09:01 +00:00
|
|
|
Vue.nextTick(() => {
|
2020-10-22 15:27:18 +00:00
|
|
|
wrapper.find(`a[href="#${hash}`).trigger('click');
|
2019-01-12 03:33:27 +00:00
|
|
|
wrapper.vm.$refs.anchor.handleScroll();
|
2018-06-06 14:33:09 +00:00
|
|
|
setTimeout(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(wrapper.vm.$refs.anchor.$data.activeLink).not.toBe(null);
|
|
|
|
done();
|
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
});
|
2018-06-04 09:09:01 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
it('Anchor render perfectly for complete href - click', done => {
|
|
|
|
const wrapper = mount(
|
|
|
|
{
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Anchor ref="anchor">
|
|
|
|
<Link href="http://www.example.com/#API" title="API" />
|
|
|
|
</Anchor>
|
|
|
|
);
|
|
|
|
},
|
2018-06-04 09:09:01 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
{ sync: false },
|
|
|
|
);
|
2018-06-04 09:09:01 +00:00
|
|
|
Vue.nextTick(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
wrapper.find('a[href="http://www.example.com/#API"]').trigger('click');
|
|
|
|
expect(wrapper.vm.$refs.anchor.$data.activeLink).toBe('http://www.example.com/#API');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-06-04 09:09:01 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
it('Anchor render perfectly for complete href - scroll', done => {
|
|
|
|
const wrapper = mount(
|
|
|
|
{
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div id="API">Hello</div>
|
|
|
|
<Anchor ref="anchor">
|
|
|
|
<Link href="http://www.example.com/#API" title="API" />
|
|
|
|
</Anchor>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
2018-06-04 09:09:01 +00:00
|
|
|
},
|
2020-07-25 07:46:49 +00:00
|
|
|
{ sync: false, attachTo: 'body' },
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-06-04 09:09:01 +00:00
|
|
|
Vue.nextTick(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
wrapper.vm.$refs.anchor.handleScroll();
|
|
|
|
expect(wrapper.vm.$refs.anchor.$data.activeLink).toBe('http://www.example.com/#API');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-06-04 09:09:01 +00:00
|
|
|
|
2018-09-05 13:28:54 +00:00
|
|
|
it('Anchor render perfectly for complete href - scrollTo', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const scrollToSpy = jest.spyOn(window, 'scrollTo');
|
|
|
|
const wrapper = mount(
|
|
|
|
{
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div id="API">Hello</div>
|
|
|
|
<Anchor ref="anchor">
|
|
|
|
<Link href="##API" title="API" />
|
|
|
|
</Anchor>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
2018-06-04 09:09:01 +00:00
|
|
|
},
|
2020-07-25 07:46:49 +00:00
|
|
|
{ sync: false, attachTo: 'body' },
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-09-05 13:28:54 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
wrapper.vm.$refs.anchor.handleScrollTo('##API');
|
|
|
|
expect(wrapper.vm.$refs.anchor.$data.activeLink).toBe('##API');
|
|
|
|
expect(scrollToSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(scrollToSpy).toHaveBeenCalled();
|
|
|
|
}, 1000);
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
it('should remove listener when unmount', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper = mount(
|
|
|
|
{
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Anchor ref="anchor">
|
|
|
|
<Link href="#API" title="API" />
|
|
|
|
</Anchor>
|
|
|
|
);
|
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2020-07-25 07:46:49 +00:00
|
|
|
{ sync: false, attachTo: 'body' },
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-09-05 13:28:54 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const removeListenerSpy = jest.spyOn(wrapper.vm.$refs.anchor.scrollEvent, 'remove');
|
2020-07-25 07:46:49 +00:00
|
|
|
wrapper.unmount();
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(removeListenerSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
it('should unregister link when unmount children', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper = mount(
|
|
|
|
{
|
|
|
|
props: {
|
|
|
|
showLink: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Anchor ref="anchor">{this.showLink ? <Link href="#API" title="API" /> : null}</Anchor>
|
|
|
|
);
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
|
|
|
},
|
2020-07-25 07:46:49 +00:00
|
|
|
{ sync: false, attachTo: 'body' },
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-09-05 13:28:54 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(wrapper.vm.$refs.anchor.links).toEqual(['#API']);
|
|
|
|
wrapper.setProps({ showLink: false });
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(wrapper.vm.$refs.anchor.links).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
it('should update links when link href update', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper = mount(
|
|
|
|
{
|
|
|
|
props: ['href'],
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Anchor ref="anchor">
|
|
|
|
<Link href={this.href} title="API" />
|
|
|
|
</Anchor>
|
|
|
|
);
|
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
{
|
|
|
|
sync: false,
|
2020-07-25 07:46:49 +00:00
|
|
|
attachTo: 'body',
|
|
|
|
props: {
|
2019-01-12 03:33:27 +00:00
|
|
|
href: '#API',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
2018-09-05 13:28:54 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(wrapper.vm.$refs.anchor.links).toEqual(['#API']);
|
|
|
|
wrapper.setProps({ href: '#API_1' });
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(wrapper.vm.$refs.anchor.links).toEqual(['#API_1']);
|
|
|
|
});
|
|
|
|
});
|
2018-11-28 02:35:37 +00:00
|
|
|
|
|
|
|
it('Anchor onClick event', () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
let event;
|
|
|
|
let link;
|
|
|
|
const handleClick = (...arg) => ([event, link] = arg);
|
2018-11-28 02:35:37 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const href = '#API';
|
|
|
|
const title = 'API';
|
2018-11-28 02:35:37 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper = mount({
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Anchor ref="anchorRef" onClick={handleClick}>
|
|
|
|
<Link href={href} title={title} />
|
|
|
|
</Anchor>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
2018-11-28 02:35:37 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
wrapper.find(`a[href="${href}"]`).trigger('click');
|
2018-11-28 02:35:37 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
wrapper.vm.$refs.anchorRef.handleScroll();
|
|
|
|
expect(event).not.toBe(undefined);
|
|
|
|
expect(link).toEqual({ href, title });
|
|
|
|
});
|
|
|
|
});
|