|
| 1 | +import { JSDOM } from 'jsdom' |
| 2 | +import { scrollToPosition } from '../src/scrollBehavior' |
| 3 | +import { createDom } from './utils' |
| 4 | +import { mockWarn } from 'jest-mock-warn' |
| 5 | + |
| 6 | +describe('scrollBehavior', () => { |
| 7 | + mockWarn() |
| 8 | + let dom: JSDOM |
| 9 | + let scrollTo: jest.SpyInstance |
| 10 | + let getElementById: jest.SpyInstance |
| 11 | + let querySelector: jest.SpyInstance |
| 12 | + |
| 13 | + beforeAll(() => { |
| 14 | + dom = createDom() |
| 15 | + scrollTo = jest.spyOn(window, 'scrollTo').mockImplementation(() => {}) |
| 16 | + getElementById = jest.spyOn(document, 'getElementById') |
| 17 | + querySelector = jest.spyOn(document, 'querySelector') |
| 18 | + |
| 19 | + // #text |
| 20 | + let el = document.createElement('div') |
| 21 | + el.id = 'text' |
| 22 | + document.documentElement.appendChild(el) |
| 23 | + |
| 24 | + // [data-scroll] |
| 25 | + el = document.createElement('div') |
| 26 | + el.setAttribute('data-scroll', 'true') |
| 27 | + document.documentElement.appendChild(el) |
| 28 | + |
| 29 | + // #special~characters |
| 30 | + el = document.createElement('div') |
| 31 | + el.id = 'special~characters' |
| 32 | + document.documentElement.appendChild(el) |
| 33 | + |
| 34 | + // #text .container |
| 35 | + el = document.createElement('div') |
| 36 | + let child = document.createElement('div') |
| 37 | + child.classList.add('container') |
| 38 | + el.id = 'text' |
| 39 | + el.append(child) |
| 40 | + document.documentElement.appendChild(el) |
| 41 | + |
| 42 | + // .container #1 |
| 43 | + el = document.createElement('div') |
| 44 | + child = document.createElement('div') |
| 45 | + el.classList.add('container') |
| 46 | + child.id = '1' |
| 47 | + el.append(child) |
| 48 | + document.documentElement.appendChild(el) |
| 49 | + }) |
| 50 | + |
| 51 | + beforeEach(() => { |
| 52 | + scrollTo.mockClear() |
| 53 | + getElementById.mockClear() |
| 54 | + querySelector.mockClear() |
| 55 | + __DEV__ = false |
| 56 | + }) |
| 57 | + |
| 58 | + afterAll(() => { |
| 59 | + __DEV__ = true |
| 60 | + }) |
| 61 | + |
| 62 | + afterAll(() => { |
| 63 | + dom.window.close() |
| 64 | + scrollTo.mockRestore() |
| 65 | + getElementById.mockRestore() |
| 66 | + querySelector.mockRestore() |
| 67 | + }) |
| 68 | + |
| 69 | + describe('left and top', () => { |
| 70 | + it('scrolls to a position', () => { |
| 71 | + scrollToPosition({ left: 10, top: 100 }) |
| 72 | + expect(getElementById).not.toHaveBeenCalled() |
| 73 | + expect(getElementById).not.toHaveBeenCalled() |
| 74 | + expect(scrollTo).toHaveBeenCalledWith({ |
| 75 | + left: 10, |
| 76 | + top: 100, |
| 77 | + behavior: undefined, |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + it('scrolls to a partial position top', () => { |
| 82 | + scrollToPosition({ top: 10 }) |
| 83 | + expect(getElementById).not.toHaveBeenCalled() |
| 84 | + expect(getElementById).not.toHaveBeenCalled() |
| 85 | + expect(scrollTo).toHaveBeenCalledWith({ |
| 86 | + top: 10, |
| 87 | + behavior: undefined, |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + it('scrolls to a partial position left', () => { |
| 92 | + scrollToPosition({ left: 10 }) |
| 93 | + expect(getElementById).not.toHaveBeenCalled() |
| 94 | + expect(getElementById).not.toHaveBeenCalled() |
| 95 | + expect(scrollTo).toHaveBeenCalledWith({ |
| 96 | + left: 10, |
| 97 | + behavior: undefined, |
| 98 | + }) |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + describe('el option', () => { |
| 103 | + it('scrolls to an id', () => { |
| 104 | + scrollToPosition({ el: '#text' }) |
| 105 | + expect(getElementById).toHaveBeenCalledWith('text') |
| 106 | + expect(querySelector).not.toHaveBeenCalled() |
| 107 | + expect(scrollTo).toHaveBeenCalledWith({ |
| 108 | + left: 0, |
| 109 | + top: 0, |
| 110 | + behavior: undefined, |
| 111 | + }) |
| 112 | + }) |
| 113 | + |
| 114 | + it('scrolls to an element using querySelector', () => { |
| 115 | + scrollToPosition({ el: '[data-scroll=true]' }) |
| 116 | + expect(querySelector).toHaveBeenCalledWith('[data-scroll=true]') |
| 117 | + expect(getElementById).not.toHaveBeenCalled() |
| 118 | + expect(scrollTo).toHaveBeenCalledWith({ |
| 119 | + left: 0, |
| 120 | + top: 0, |
| 121 | + behavior: undefined, |
| 122 | + }) |
| 123 | + }) |
| 124 | + |
| 125 | + it('scrolls to an id with special characters', () => { |
| 126 | + scrollToPosition({ el: '#special~characters' }) |
| 127 | + expect(getElementById).toHaveBeenCalledWith('special~characters') |
| 128 | + expect(querySelector).not.toHaveBeenCalled() |
| 129 | + expect(scrollTo).toHaveBeenCalledWith({ |
| 130 | + left: 0, |
| 131 | + top: 0, |
| 132 | + behavior: undefined, |
| 133 | + }) |
| 134 | + }) |
| 135 | + |
| 136 | + it('scrolls to an id with special characters', () => { |
| 137 | + scrollToPosition({ el: '#special~characters' }) |
| 138 | + expect(getElementById).toHaveBeenCalledWith('special~characters') |
| 139 | + expect(querySelector).not.toHaveBeenCalled() |
| 140 | + expect(scrollTo).toHaveBeenCalledWith({ |
| 141 | + left: 0, |
| 142 | + top: 0, |
| 143 | + behavior: undefined, |
| 144 | + }) |
| 145 | + }) |
| 146 | + |
| 147 | + it('accepts a raw element', () => { |
| 148 | + scrollToPosition({ el: document.getElementById('special~characters')! }) |
| 149 | + expect(getElementById).toHaveBeenCalledWith('special~characters') |
| 150 | + expect(querySelector).not.toHaveBeenCalled() |
| 151 | + expect(scrollTo).toHaveBeenCalledWith({ |
| 152 | + left: 0, |
| 153 | + top: 0, |
| 154 | + behavior: undefined, |
| 155 | + }) |
| 156 | + }) |
| 157 | + |
| 158 | + describe('warnings', () => { |
| 159 | + beforeEach(() => { |
| 160 | + __DEV__ = true |
| 161 | + }) |
| 162 | + |
| 163 | + it('warns if element cannot be found with id', () => { |
| 164 | + scrollToPosition({ el: '#not-found' }) |
| 165 | + expect( |
| 166 | + `Couldn't find element using selector "#not-found"` |
| 167 | + ).toHaveBeenWarned() |
| 168 | + }) |
| 169 | + |
| 170 | + it('warns if element cannot be found with selector', () => { |
| 171 | + scrollToPosition({ el: '.not-found' }) |
| 172 | + expect( |
| 173 | + `Couldn't find element using selector ".not-found"` |
| 174 | + ).toHaveBeenWarned() |
| 175 | + }) |
| 176 | + |
| 177 | + it('warns if element cannot be found with id but can with selector', () => { |
| 178 | + scrollToPosition({ el: '#text .container' }) |
| 179 | + expect( |
| 180 | + `selector "#text .container" should be passed as "el: document.querySelector('#text .container')"` |
| 181 | + ).toHaveBeenWarned() |
| 182 | + }) |
| 183 | + |
| 184 | + it('warns if element cannot be found with id but can with selector', () => { |
| 185 | + scrollToPosition({ el: '#text .container' }) |
| 186 | + expect( |
| 187 | + `selector "#text .container" should be passed as "el: document.querySelector('#text .container')"` |
| 188 | + ).toHaveBeenWarned() |
| 189 | + }) |
| 190 | + |
| 191 | + it('warns if querySelector throws', () => { |
| 192 | + scrollToPosition({ el: '.container #1' }) |
| 193 | + expect(`selector ".container #1" is invalid`).toHaveBeenWarned() |
| 194 | + }) |
| 195 | + }) |
| 196 | + }) |
| 197 | +}) |
0 commit comments