This repository has been archived by the owner on Feb 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathattachHandlers_spec.js
133 lines (125 loc) · 4.02 KB
/
attachHandlers_spec.js
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { expect } from "chai";
import { attachHandlers } from "../src/attachHandlers";
import * as packager from "../src/packageLogs";
describe("attachHandlers", () => {
it("attaches all the event handlers without duplicates", (done) => {
let duplicateEvents = 0;
const initialDocument = global.document;
const initialWindow = global.window;
// List of supported document events
const missingDocumentEvents = [
"click",
"dblclick",
"mousedown",
"mouseup",
"focus",
"blur",
"input",
"change",
"dragstart",
"dragend",
"drag",
"drop",
"keydown",
"mouseover",
"submit",
];
// List of supported window events
const missingWindowEvents = [
"wheel",
"scroll",
"resize",
"load",
"blur",
"focus",
];
// List of supported interval events
const missingIntervalEvents = [
"click",
"focus",
"blur",
"input",
"change",
"mouseover",
"submit",
];
// Acts as a kind of Proxy for addEventListener. Keeps track of added listeners.
const listenerHook = (eventList) => (ev) => {
const evIndex = eventList.indexOf(ev);
if (evIndex !== -1) {
eventList.splice(evIndex, 1);
} else {
duplicateEvents++;
}
};
const missingDocumentAndIntervalEvents = missingDocumentEvents.concat(
missingIntervalEvents,
);
// MOCK
global.document = {
addEventListener: listenerHook(missingDocumentAndIntervalEvents),
};
global.window = { addEventListener: listenerHook(missingWindowEvents) };
attachHandlers({ logDetails: true });
expect(duplicateEvents).to.equal(0);
expect(missingDocumentAndIntervalEvents.length).to.equal(0);
expect(missingWindowEvents.length).to.equal(0);
// UNMOCK
global.document = initialDocument;
global.window = initialWindow;
done();
});
it("debounces bufferedEvents", (done) => {
let callCount = 0;
let testingEvent = false;
const bufferedEvents = ["wheel", "scroll", "resize"];
const initialWindow = global.window;
const initialDocument = global.document;
const rate = 500;
const initialPackage = packager.packageLog;
packager.packageLog = () => {
callCount++;
};
global.document = { addEventListener: () => {} };
// Tries to call an event 3 times. Twice in quick succession, then once after the set delay.
// Number of actual calls to packageLog are recorded in callCount. Should amount to exactly 2 calls.
global.window = {
addEventListener: (ev, fn, ...args) => {
if (!testingEvent && bufferedEvents.indexOf(ev) !== -1) {
testingEvent = true;
fn();
fn();
setTimeout(() => {
fn();
global.window = initialWindow;
global.document = initialDocument;
packager.packageLog = initialPackage;
expect(callCount).to.equal(2);
done();
}, rate + 1);
}
},
};
attachHandlers({ resolution: rate });
});
describe("defineDetails", () => {
// TODO: clarify what constitutes "high detail events" and what is "correct"
it("configures high detail events correctly");
});
});