/
preset.test.ts
269 lines (218 loc) 路 7.18 KB
/
preset.test.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { isVisible } from '../preset';
describe('isVisible', () => {
const getWindow = () =>
({
innerHeight: 1000,
innerWidth: 1000
} as any);
const generateElement = (top: number, left: number, height = 300, width = 300): any => ({
getBoundingClientRect: () => ({
top,
left,
right: left + width,
bottom: top + height
})
});
it('Should be vissible when top is inside viewport and no offset', () => {
const attribute = {
event: undefined as any,
element: generateElement(999, 100),
offset: 0
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(true);
});
it('Should not be vissible when the image is outside viewport', () => {
const attribute = {
event: undefined as any,
element: generateElement(1001, 100),
offset: 0
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(false);
});
it('Should be vissible when the image is outside viewport but have a offset', () => {
const attribute = {
event: undefined as any,
element: generateElement(-399, 100),
offset: 100
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(true);
});
it('Should not be vissible when the image is inside viewport but have a offset', () => {
const attribute = {
event: undefined as any,
element: generateElement(901, 100),
offset: -100
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(false);
});
it('Should be vissible when the image is inside viewport and have a offset', () => {
const attribute = {
event: undefined as any,
element: generateElement(899, 100),
offset: -100
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(true);
});
it('Should not be vissible when the bottom of the image is inside viewport but have a offset', () => {
const attribute = {
event: undefined as any,
element: generateElement(-201, 100),
offset: -100
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(false);
});
it('Should be vissible when the bottom of the image is inside viewport and have a offset', () => {
const attribute = {
event: undefined as any,
element: generateElement(-199, 100),
offset: -100
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(true);
});
it('Should be vissible when the image is larger than the viewport', () => {
const attribute = {
event: undefined as any,
element: generateElement(-100, -100, 1200, 1200),
offset: 0
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(true);
});
it('Should not be vissible when the image is to the left of the viewport', () => {
const attribute = {
event: undefined as any,
element: generateElement(100, -301),
offset: 0
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(false);
});
it('Should not be vissible when the image is to the right of the viewport', () => {
const attribute = {
event: undefined as any,
element: generateElement(100, 1001),
offset: 0
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(false);
});
it('Should be vissible when the left side is in viewport', () => {
const attribute = {
event: undefined as any,
element: generateElement(200, 899),
offset: -100
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(true);
});
it('Should be vissible when the right side is in viewport', () => {
const attribute = {
event: undefined as any,
element: generateElement(200, -199),
offset: -100
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(true);
});
it('Should be vissible when only left side with no corners is in the viewport', () => {
const attribute = {
event: undefined as any,
element: generateElement(-100, 500, 1200, 1200),
offset: 0
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(true);
});
it('Should be vissible when only top side with no corners is in the viewport', () => {
const attribute = {
event: undefined as any,
element: generateElement(500, -100, 1200, 1200),
offset: 0
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(true);
});
it('Should be vissible when only right side with no corners is in the viewport', () => {
const attribute = {
event: undefined as any,
element: generateElement(-100, -500, 1200, 1200),
offset: 0
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(true);
});
it('Should be vissible when only bottom side with no corners is in the viewport', () => {
const attribute = {
event: undefined as any,
element: generateElement(-500, -100, 1200, 1200),
offset: 0
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(true);
});
it("Should not be visible when image is horizontally in window's view, but not in scroll-container's", () => {
const attribute = {
event: undefined as any,
element: generateElement(800, 0, 1200, 1200),
offset: 0,
scrollContainer: generateElement(0, 0, 700, 1200)
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(false);
});
it("Should not be visible when image is vertically in window's view, but not in scroll-container's", () => {
const attribute = {
event: undefined as any,
element: generateElement(0, 800, 1200, 1200),
offset: 0,
scrollContainer: generateElement(0, 0, 1200, 700)
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(false);
});
it("Should not be visible when image is not in window's view, but is in scroll-container's", () => {
const attribute = {
event: undefined as any,
element: generateElement(1400, 0, 1200, 1200),
offset: 0,
scrollContainer: generateElement(1300, 0, 1200, 1200)
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(false);
});
it("Should be visible when image is in window's and scroll-container's view", () => {
const attribute = {
event: undefined as any,
element: generateElement(100, 0, 1200, 1200),
offset: 0,
scrollContainer: generateElement(0, 0, 700, 1200)
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(true);
});
it("Should not be visible when image's rect is empty", () => {
const attribute = {
event: undefined as any,
element: generateElement(0, 0, 0, 0),
offset: 0
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(false);
});
it("Should not be visible when image's rect is empty and has an offset", () => {
const attribute = {
event: undefined as any,
element: generateElement(0, 0, 0, 0),
offset: 100
};
const result = isVisible(attribute, getWindow);
expect(result).toBe(false);
});
});