This repository was archived by the owner on Jan 15, 2026. It is now read-only.
forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.test.ts
More file actions
210 lines (178 loc) · 7.76 KB
/
Copy pathtest.test.ts
File metadata and controls
210 lines (178 loc) · 7.76 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
import { filterFunctionFromFilter, createEventFromButton } from '../src/util';
describe('Button Click Tracking Plugin', () => {
describe('filterToFunction', () => {
const createDiv = (className: string): HTMLDivElement => {
const ret = document.createElement('div');
ret.className = className;
return ret;
};
it('should return a function that returns true if no filter is provided', () => {
const filter = filterFunctionFromFilter(undefined);
expect(filter(createDiv(''))).toBe(true);
});
it('should turn an allowlist into a filter function', () => {
const filter = filterFunctionFromFilter({ allowlist: ['foo'] });
expect(filter(createDiv('foo'))).toBe(true);
expect(filter(createDiv('bar'))).toBe(false);
});
it('should turn a denylist into a filter function', () => {
const filter = filterFunctionFromFilter({ denylist: ['foo'] });
expect(filter(createDiv('foo'))).toBe(false);
expect(filter(createDiv('bar'))).toBe(true);
});
});
describe('getFilterPassingButtons', () => {
type TestDocument = {
testDocument: Document;
include_btn: HTMLButtonElement | HTMLInputElement;
exclude_btn: HTMLButtonElement | HTMLInputElement;
};
describe('HTMLButtonElements', () => {
const getTestDocument = (): TestDocument => {
const testDocument = document.implementation.createHTMLDocument('test');
const include_btn = testDocument.createElement('button');
include_btn.className = 'include';
testDocument.body.appendChild(include_btn);
const exclude_btn = testDocument.createElement('button');
exclude_btn.className = 'exclude';
testDocument.body.appendChild(exclude_btn);
return {
testDocument,
include_btn,
exclude_btn,
};
};
const { testDocument, include_btn, exclude_btn } = getTestDocument();
const buttons = Array.from(testDocument.getElementsByTagName('button'));
it('should return all elements if no filter is provided', () => {
const buttonFilter = filterFunctionFromFilter(undefined);
const filteredButtons = buttons.filter(buttonFilter);
expect(filteredButtons).toEqual([include_btn, exclude_btn]);
});
it('should only return elements that match the `filter` function', () => {
const filter = filterFunctionFromFilter((element?: HTMLElement) => element?.className === 'include');
const filteredButtons = buttons.filter(filter);
expect(filteredButtons).toEqual([include_btn]);
});
it('should return all elements with the classes in `allowlist`', () => {
const filter = filterFunctionFromFilter({ allowlist: ['include'] });
const filteredButtons = buttons.filter(filter);
expect(filteredButtons).toEqual([include_btn]);
});
it('should not return any elements with the classes in `denylist`', () => {
const filter = filterFunctionFromFilter({ denylist: ['exclude'] });
const filteredButtons = buttons.filter(filter);
expect(filteredButtons).toEqual([include_btn]);
});
});
describe('HTMLInputElement', () => {
const getTestDocument = (): TestDocument => {
const testDocument = document.implementation.createHTMLDocument('test');
const include_btn = testDocument.createElement('input');
include_btn.type = 'button';
include_btn.className = 'include';
testDocument.body.appendChild(include_btn);
const exclude_btn = testDocument.createElement('input');
exclude_btn.type = 'button';
exclude_btn.className = 'exclude';
testDocument.body.appendChild(exclude_btn);
return {
testDocument,
include_btn,
exclude_btn,
};
};
const { testDocument, include_btn, exclude_btn } = getTestDocument();
const buttons = Array.from(testDocument.getElementsByTagName('input')).filter((input) => input.type === 'button');
it('should return all elements if no filter is provided', () => {
const buttonFilter = filterFunctionFromFilter(undefined);
const filteredButtons = buttons.filter(buttonFilter);
expect(filteredButtons).toEqual([include_btn, exclude_btn]);
});
it('should only return elements that match the `filter` function', () => {
const filter = filterFunctionFromFilter((element?: HTMLElement) => element?.className === 'include');
const filteredButtons = buttons.filter(filter);
expect(filteredButtons).toEqual([include_btn]);
});
it('should return all elements with the classes in `allowlist`', () => {
const filter = filterFunctionFromFilter({ allowlist: ['include'] });
const filteredButtons = buttons.filter(filter);
expect(filteredButtons).toEqual([include_btn]);
});
it('should not return any elements with the classes in `denylist`', () => {
const filter = filterFunctionFromFilter({ denylist: ['exclude'] });
const filteredButtons = buttons.filter(filter);
expect(filteredButtons).toEqual([include_btn]);
});
});
});
describe('createEventFromButton', () => {
describe('HTMLButtonElement', () => {
it('should create an event from a button', () => {
const button = document.createElement('button');
button.innerText = 'testLabel';
button.id = 'testId';
button.className = 'testClass testClass2';
button.name = 'testName';
const event = {
label: 'testLabel',
id: 'testId',
classes: ['testClass', 'testClass2'],
name: 'testName',
};
expect(createEventFromButton(button)).toEqual(event);
});
it('should use specified default label', () => {
const button = document.createElement('button');
button.innerText = '';
button.id = 'testId';
button.className = 'testClass testClass2';
button.name = 'testName';
const event1 = {
label: 'defaultLabel',
id: 'testId',
classes: ['testClass', 'testClass2'],
name: 'testName',
};
expect(createEventFromButton(button, 'defaultLabel')).toEqual(event1);
const event2 = {
label: 'testClass testClass2',
id: 'testId',
classes: ['testClass', 'testClass2'],
name: 'testName',
};
expect(createEventFromButton(button, (btn) => btn.className)).toEqual(event2);
});
it('should prefer the data-sp-button-label attribute over the innerText', () => {
const button = document.createElement('button');
button.innerText = 'testLabel';
button.setAttribute('data-sp-button-label', 'testLabelFromAttribute');
expect(createEventFromButton(button).label).toEqual('testLabelFromAttribute');
});
});
describe('HTMLInputElement', () => {
it('should create an event from an input', () => {
const input = document.createElement('input');
input.type = 'button';
input.value = 'testLabel';
input.id = 'testId';
input.className = 'testClass testClass2';
input.name = 'testName';
const event = {
label: 'testLabel',
id: 'testId',
classes: ['testClass', 'testClass2'],
name: 'testName',
};
expect(createEventFromButton(input)).toEqual(event);
});
it('should prefer the data-sp-button-label attribute over the value', () => {
const input = document.createElement('input');
input.type = 'button';
input.value = 'testLabel';
input.setAttribute('data-sp-button-label', 'testLabelFromAttribute');
expect(createEventFromButton(input).label).toEqual('testLabelFromAttribute');
});
});
});
});