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