|
11 | 11 | import { shallowMount } from '@vue/test-utils';
|
12 | 12 | import scrollToElement from 'docc-render/mixins/scrollToElement';
|
13 | 13 | import * as loading from 'docc-render/utils/loading';
|
| 14 | +import { createEvent as createBaseEvent } from '../../../test-utils'; |
14 | 15 |
|
15 | 16 | const framesWait = jest.spyOn(loading, 'waitFrames');
|
16 | 17 |
|
17 | 18 | describe('scrollToElement', () => {
|
18 | 19 | const scrollOffset = { x: 0, y: 14 };
|
19 | 20 | const anchor = 'heres-why';
|
20 |
| - |
21 |
| - const wrapper = shallowMount({ |
22 |
| - name: 'MyComponent', |
23 |
| - mixins: [scrollToElement], |
24 |
| - render() { |
25 |
| - return `<div id="${anchor}"/>`; |
26 |
| - }, |
27 |
| - }, { |
28 |
| - mocks: { |
29 |
| - $router: { |
30 |
| - resolve: ({ hash }) => ({ route: { hash } }), |
31 |
| - options: { |
32 |
| - scrollBehavior(to) { |
33 |
| - return new Promise(resolve => ( |
34 |
| - resolve({ selector: to.hash, offset: scrollOffset }) |
35 |
| - )); |
| 21 | + const pushMock = jest.fn(); |
| 22 | + const preventDefault = jest.fn(); |
| 23 | + let wrapper; |
| 24 | + |
| 25 | + const event = createBaseEvent('click'); |
| 26 | + event.preventDefault = preventDefault; |
| 27 | + document.dispatchEvent(event); |
| 28 | + |
| 29 | + const createWrapper = (extraMocks) => { |
| 30 | + wrapper = shallowMount({ |
| 31 | + name: 'MyComponent', |
| 32 | + mixins: [scrollToElement], |
| 33 | + render() { |
| 34 | + return `<div id="${anchor}"/>`; |
| 35 | + }, |
| 36 | + }, { |
| 37 | + mocks: { |
| 38 | + $router: { |
| 39 | + resolve: ({ hash }) => ({ route: { hash } }), |
| 40 | + push: ({ hash }) => pushMock(hash), |
| 41 | + options: { |
| 42 | + scrollBehavior(to) { |
| 43 | + return new Promise(resolve => ( |
| 44 | + resolve({ selector: to.hash, offset: scrollOffset }) |
| 45 | + )); |
| 46 | + }, |
36 | 47 | },
|
37 | 48 | },
|
| 49 | + ...extraMocks, |
38 | 50 | },
|
39 |
| - }, |
| 51 | + }); |
| 52 | + }; |
| 53 | + |
| 54 | + beforeEach(() => { |
| 55 | + createWrapper(); |
| 56 | + jest.clearAllMocks(); |
40 | 57 | });
|
41 | 58 |
|
42 | 59 | it('scrolls to the correct element when "scrollToElement" is called', async () => {
|
@@ -89,6 +106,48 @@ describe('scrollToElement', () => {
|
89 | 106 | getElementSpy.mockRestore();
|
90 | 107 | });
|
91 | 108 |
|
| 109 | + it('prevents default event, if preventDefault provides an event', async () => { |
| 110 | + createWrapper({ |
| 111 | + $route: { |
| 112 | + hash: '#foo', |
| 113 | + }, |
| 114 | + }); |
| 115 | + |
| 116 | + const mockObject = { focus: jest.fn() }; |
| 117 | + jest.spyOn(document, 'getElementById').mockReturnValue(mockObject); |
| 118 | + |
| 119 | + await wrapper.vm.handleFocusAndScroll('#blah', { event }); |
| 120 | + expect(preventDefault).toHaveBeenCalledTimes(1); |
| 121 | + }); |
| 122 | + |
| 123 | + it('pushes hash on router, if preventDefault is true and hash is different than current one', async () => { |
| 124 | + createWrapper({ |
| 125 | + $route: { |
| 126 | + hash: '#foo', |
| 127 | + }, |
| 128 | + }); |
| 129 | + |
| 130 | + const mockObject = { focus: jest.fn() }; |
| 131 | + jest.spyOn(document, 'getElementById').mockReturnValue(mockObject); |
| 132 | + |
| 133 | + await wrapper.vm.handleFocusAndScroll('blah', { event }); |
| 134 | + expect(pushMock).toHaveBeenCalledTimes(1); |
| 135 | + }); |
| 136 | + |
| 137 | + it('does not push hash on router, if preventDefault is true and hash is the same as current one', async () => { |
| 138 | + createWrapper({ |
| 139 | + $route: { |
| 140 | + hash: '#foo', |
| 141 | + }, |
| 142 | + }); |
| 143 | + |
| 144 | + const mockObject = { focus: jest.fn() }; |
| 145 | + jest.spyOn(document, 'getElementById').mockReturnValue(mockObject); |
| 146 | + |
| 147 | + await wrapper.vm.handleFocusAndScroll('foo', { event }); |
| 148 | + expect(pushMock).toHaveBeenCalledTimes(0); |
| 149 | + }); |
| 150 | + |
92 | 151 | it('does not focus element and scroll if element is not in the document', async () => {
|
93 | 152 | wrapper.vm.scrollToElement = jest.fn();
|
94 | 153 | const hash = 'foo';
|
|
0 commit comments