-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathanimation_events_spec.js
More file actions
263 lines (206 loc) · 6.24 KB
/
Copy pathanimation_events_spec.js
File metadata and controls
263 lines (206 loc) · 6.24 KB
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
describe('Animation Events', function() {
context('Basic event triggering', function() {
let aosInStub;
let aosOutStub;
beforeEach(() => {
aosInStub = cy.stub();
aosOutStub = cy.stub();
cy.visit('/')
.document()
.then(document => {
document.addEventListener('aos:in', aosInStub);
document.addEventListener('aos:out', aosOutStub);
});
});
it('Should trigger aos:in event for visible elements on init', function() {
cy.initAOS();
cy.wait(0, () => {
expect(aosInStub).to.have.callCount(6);
expect(aosOutStub).to.not.be.called;
});
});
it('Should trigger aos:in event when elements become visible on scroll', function() {
cy.initAOS();
cy.wait(0, () => {
expect(aosInStub).to.have.callCount(6);
});
cy.scrollTo(0, 400);
cy.wait(0, () => {
expect(aosInStub.callCount).to.be.greaterThan(6);
});
});
it('Should trigger aos:out event when elements become invisible on scroll', function() {
cy.initAOS();
cy.wait(0, () => {
expect(aosOutStub).to.not.be.called;
});
cy.scrollTo(0, 800);
cy.wait(100);
cy.scrollTo('top');
cy.wait(0, () => {
expect(aosOutStub).to.be.called;
});
});
it('Should pass element detail in event', function() {
let receivedElement;
cy.visit('/')
.document()
.then(document => {
document.addEventListener('aos:in', (e) => {
receivedElement = e.detail;
});
})
.initAOS();
cy.wait(0, () => {
expect(receivedElement).to.exist;
expect(receivedElement.classList).to.include('aos-item');
expect(receivedElement.hasAttribute('data-aos')).to.be.true;
});
});
});
context('Custom ID events', function() {
let customInStub;
let customOutStub;
beforeEach(() => {
customInStub = cy.stub();
customOutStub = cy.stub();
cy.visit('/')
.document()
.then(document => {
document.addEventListener('aos:in:super-duper', customInStub);
document.addEventListener('aos:out:super-duper', customOutStub);
});
});
it('Should trigger custom ID event when element with data-aos-id is animated', function() {
cy.initAOS();
cy.wait(0, () => {
expect(customInStub).to.not.be.called;
});
cy.scrollTo(0, 350);
cy.wait(0, () => {
expect(customInStub).to.be.calledOnce;
});
});
it('Should trigger custom ID out event when element scrolls out', function() {
cy.initAOS();
cy.scrollTo(0, 350);
cy.wait(100);
cy.scrollTo('top');
cy.wait(0, () => {
expect(customOutStub).to.be.called;
});
});
});
context('Event behavior with once option', function() {
let aosInStub;
let aosOutStub;
beforeEach(() => {
aosInStub = cy.stub();
aosOutStub = cy.stub();
cy.visit('/')
.document()
.then(document => {
document.addEventListener('aos:in', aosInStub);
document.addEventListener('aos:out', aosOutStub);
});
});
it('Should not trigger aos:out when once option is true', function() {
cy.initAOS({
once: true
});
cy.wait(0, () => {
const initialCallCount = aosInStub.callCount;
expect(aosOutStub).to.not.be.called;
});
cy.scrollTo(0, 800);
cy.wait(100);
cy.scrollTo('top');
cy.wait(0, () => {
expect(aosOutStub).to.not.be.called;
});
});
it('Should trigger aos:in only once per element', function() {
cy.initAOS({
once: true
});
cy.wait(0, () => {
const initialCallCount = aosInStub.callCount;
cy.scrollTo(0, 800);
cy.wait(100);
cy.scrollTo('top');
cy.wait(100);
cy.scrollTo(0, 800);
cy.wait(0, () => {
expect(aosInStub.callCount).to.be.lessThan(initialCallCount * 3);
});
});
});
});
context('Event behavior with mirror option', function() {
let aosInStub;
let aosOutStub;
beforeEach(() => {
aosInStub = cy.stub();
aosOutStub = cy.stub();
cy.visit('/')
.document()
.then(document => {
document.addEventListener('aos:in', aosInStub);
document.addEventListener('aos:out', aosOutStub);
});
});
it('Should trigger multiple in/out events with mirror option', function() {
cy.initAOS({
mirror: true
});
cy.wait(0, () => {
const initialInCount = aosInStub.callCount;
cy.scrollTo(0, 800);
cy.wait(100);
cy.scrollTo('top');
cy.wait(0, () => {
expect(aosOutStub).to.be.called;
});
});
});
});
context('Event dispatch details', function() {
it('Should dispatch events as CustomEvent with proper detail', function() {
let eventType, eventDetail;
cy.visit('/')
.document()
.then(document => {
document.addEventListener('aos:in', (e) => {
eventType = e.constructor.name;
eventDetail = e.detail;
});
})
.initAOS();
cy.wait(0, () => {
expect(eventType).to.equal('CustomEvent');
expect(eventDetail).to.exist;
expect(eventDetail.nodeType).to.equal(1);
});
});
it('Should not trigger duplicate events for already animated elements', function() {
let callCounts = [];
cy.visit('/')
.document()
.then(document => {
document.addEventListener('aos:in', () => {
callCounts.push(Date.now());
});
})
.initAOS();
cy.wait(0, () => {
const firstCount = callCounts.length;
cy.window().then(({ AOS }) => {
AOS.refresh();
});
cy.wait(0, () => {
expect(callCounts.length).to.equal(firstCount);
});
});
});
});
});