@pdanpdan/virtual-scroll 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,228 +0,0 @@
1
- import { describe, expect, it, vi } from 'vitest';
2
-
3
- import { getPaddingX, getPaddingY, isBody, isElement, isScrollableElement, isScrollToIndexOptions, isWindow, isWindowLike, scrollTo } from './scroll';
4
-
5
- describe('scroll utils', () => {
6
- describe('element type guards', () => {
7
- describe('is window', () => {
8
- it('returns true for null', () => {
9
- expect(isWindow(null)).toBe(true);
10
- });
11
-
12
- it('returns true for window object', () => {
13
- expect(isWindow(window)).toBe(true);
14
- });
15
-
16
- it('returns true for document.documentelement object', () => {
17
- expect(isWindow(document.documentElement)).toBe(true);
18
- });
19
-
20
- it('returns false for an element', () => {
21
- const el = document.createElement('div');
22
- expect(isWindow(el)).toBe(false);
23
- });
24
-
25
- it('returns false for undefined', () => {
26
- expect(isWindow(undefined)).toBe(false);
27
- });
28
- });
29
-
30
- describe('is body', () => {
31
- it('returns true for document.body', () => {
32
- expect(isBody(document.body)).toBe(true);
33
- });
34
-
35
- it('returns false for null', () => {
36
- expect(isBody(null)).toBe(false);
37
- });
38
-
39
- it('returns false for undefined', () => {
40
- expect(isBody(undefined)).toBe(false);
41
- });
42
-
43
- it('returns false for a string', () => {
44
- // @ts-expect-error testing invalid input
45
- expect(isBody('not an object')).toBe(false);
46
- });
47
-
48
- it('returns false for a plain object', () => {
49
- // @ts-expect-error testing invalid input
50
- expect(isBody({})).toBe(false);
51
- });
52
-
53
- it('returns false for a div', () => {
54
- const el = document.createElement('div');
55
- expect(isBody(el)).toBe(false);
56
- });
57
-
58
- it('returns false for window', () => {
59
- expect(isBody(window)).toBe(false);
60
- });
61
-
62
- it('returns false for document.documentelement', () => {
63
- expect(isBody(document.documentElement)).toBe(false);
64
- });
65
- });
66
-
67
- describe('is window like', () => {
68
- it('returns true for window', () => {
69
- expect(isWindowLike(window)).toBe(true);
70
- });
71
-
72
- it('returns true for document.documentelement', () => {
73
- expect(isWindowLike(document.documentElement)).toBe(true);
74
- });
75
-
76
- it('returns true for body', () => {
77
- expect(isWindowLike(document.body)).toBe(true);
78
- });
79
-
80
- it('returns true for null', () => {
81
- expect(isWindowLike(null)).toBe(true);
82
- });
83
-
84
- it('returns false for a div', () => {
85
- const el = document.createElement('div');
86
- expect(isWindowLike(el)).toBe(false);
87
- });
88
- });
89
-
90
- describe('is element', () => {
91
- it('returns true for a div', () => {
92
- const el = document.createElement('div');
93
- expect(isElement(el)).toBe(true);
94
- });
95
-
96
- it('returns true for document.documentelement', () => {
97
- expect(isElement(document.documentElement)).toBe(true);
98
- });
99
-
100
- it('returns false for window', () => {
101
- expect(isElement(window)).toBe(false);
102
- });
103
-
104
- it('returns false for null', () => {
105
- expect(isElement(null)).toBe(false);
106
- });
107
- });
108
-
109
- describe('is scrollable element', () => {
110
- it('returns true for a div', () => {
111
- const el = document.createElement('div');
112
- expect(isScrollableElement(el)).toBe(true);
113
- });
114
-
115
- it('returns false for null', () => {
116
- expect(isScrollableElement(null)).toBe(false);
117
- });
118
- });
119
- });
120
-
121
- describe('options type guards', () => {
122
- describe('is scroll to index options', () => {
123
- it('returns true for valid options', () => {
124
- expect(isScrollToIndexOptions({ align: 'start' })).toBe(true);
125
- expect(isScrollToIndexOptions({ behavior: 'smooth' })).toBe(true);
126
- expect(isScrollToIndexOptions({ isCorrection: true })).toBe(true);
127
- });
128
-
129
- it('returns false for other values', () => {
130
- expect(isScrollToIndexOptions(null)).toBe(false);
131
- expect(isScrollToIndexOptions('start')).toBe(false);
132
- expect(isScrollToIndexOptions({})).toBe(false);
133
- });
134
- });
135
- });
136
-
137
- describe('padding utilities', () => {
138
- describe('get padding x', () => {
139
- it('handles numeric padding', () => {
140
- expect(getPaddingX(10, 'horizontal')).toBe(10);
141
- expect(getPaddingX(10, 'both')).toBe(10);
142
- expect(getPaddingX(10, 'vertical')).toBe(0);
143
- expect(getPaddingX(0, 'horizontal')).toBe(0);
144
- });
145
-
146
- it('handles object padding', () => {
147
- expect(getPaddingX({ x: 15 }, 'vertical')).toBe(15);
148
- expect(getPaddingX({ y: 20 }, 'horizontal')).toBe(0);
149
- });
150
-
151
- it('returns 0 for undefined', () => {
152
- expect(getPaddingX(undefined)).toBe(0);
153
- });
154
- });
155
-
156
- describe('get padding y', () => {
157
- it('handles numeric padding', () => {
158
- expect(getPaddingY(10, 'vertical')).toBe(10);
159
- expect(getPaddingY(10, 'both')).toBe(10);
160
- expect(getPaddingY(10, 'horizontal')).toBe(0);
161
- expect(getPaddingY(0, 'vertical')).toBe(0);
162
- });
163
-
164
- it('handles object padding', () => {
165
- expect(getPaddingY({ y: 15 }, 'horizontal')).toBe(15);
166
- expect(getPaddingY({ x: 20 }, 'vertical')).toBe(0);
167
- });
168
-
169
- it('returns 0 for undefined', () => {
170
- expect(getPaddingY(undefined)).toBe(0);
171
- });
172
- });
173
- });
174
-
175
- describe('scrollTo utility', () => {
176
- it('does nothing if container is undefined', () => {
177
- const spy = vi.spyOn(window, 'scrollTo');
178
- scrollTo(undefined, { top: 100 });
179
- expect(spy).not.toHaveBeenCalled();
180
- });
181
-
182
- it('scrolls the window if container is null', () => {
183
- const spy = vi.spyOn(window, 'scrollTo');
184
- scrollTo(null, { top: 100 });
185
- expect(spy).toHaveBeenCalledWith({ top: 100 });
186
- });
187
-
188
- it('scrolls the window if container is window', () => {
189
- const spy = vi.spyOn(window, 'scrollTo');
190
- scrollTo(window, { top: 100 });
191
- expect(spy).toHaveBeenCalledWith({ top: 100 });
192
- });
193
-
194
- it('scrolls the window if container is document.documentElement', () => {
195
- const spy = vi.spyOn(window, 'scrollTo');
196
- scrollTo(document.documentElement, { top: 100 });
197
- expect(spy).toHaveBeenCalledWith({ top: 100 });
198
- });
199
-
200
- it('scrolls an element using scrollTo if available', () => {
201
- const el = document.createElement('div');
202
- const spy = vi.fn();
203
- el.scrollTo = spy;
204
- scrollTo(el, { left: 50, top: 100 });
205
- expect(spy).toHaveBeenCalledWith({ left: 50, top: 100 });
206
- });
207
-
208
- it('scrolls an element using scrollLeft/scrollTop if scrollTo is missing', () => {
209
- const el = document.createElement('div');
210
- // @ts-expect-error forcing missing scrollTo
211
- el.scrollTo = undefined;
212
- scrollTo(el, { left: 50, top: 100 });
213
- expect(el.scrollLeft).toBe(50);
214
- expect(el.scrollTop).toBe(100);
215
- });
216
-
217
- it('does not set undefined values on scrollLeft/scrollTop', () => {
218
- const el = document.createElement('div');
219
- el.scrollLeft = 10;
220
- el.scrollTop = 20;
221
- // @ts-expect-error forcing missing scrollTo
222
- el.scrollTo = undefined;
223
- scrollTo(el, { behavior: 'smooth' });
224
- expect(el.scrollLeft).toBe(10);
225
- expect(el.scrollTop).toBe(20);
226
- });
227
- });
228
- });