-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathKudoAll-Strava-Garmin.user.test.cjs
More file actions
382 lines (343 loc) · 11.9 KB
/
KudoAll-Strava-Garmin.user.test.cjs
File metadata and controls
382 lines (343 loc) · 11.9 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
const sinon = require('sinon');
const { expect } = require('chai');
const { describe, it, beforeEach, afterEach } = require('mocha');
function getMessage(_key) {
if (globalThis.navigator.language === 'en') {
return 'Kudo All';
}
return 'Kudo All';
}
function getStravaContainer() {
return globalThis.document.querySelector('.strava-container');
}
function findStravaKudosButtons() {
return globalThis.document.querySelectorAll('.strava-kudos-button');
}
function createStravaButton() {
return globalThis.document.createElement('button');
}
function stravaKudoAllHandler(event, findButtons) {
event.preventDefault();
const buttons = findButtons();
if (buttons && buttons.length > 0) {
buttons.forEach((button) => button.parentElement.click());
}
}
function stravaStandBy() {
const container = getStravaContainer();
const button = createStravaButton();
button.addEventListener('click', (event) =>
stravaKudoAllHandler(event, findStravaKudosButtons),
);
container.prepend(button);
}
function getGarminContainer() {
return globalThis.document.querySelector('.garmin-container');
}
function findGarminKudosButtons() {
return globalThis.document.querySelectorAll('.garmin-kudos-button');
}
function createGarminButton() {
return globalThis.document.createElement('button');
}
function garminKudoAllHandler(event, findButtons) {
event.preventDefault();
const buttons = findButtons();
if (buttons && buttons.length > 0) {
buttons.forEach((button) => button.click());
}
}
function executeGarmin() {
const container = getGarminContainer();
const button = createGarminButton();
button.addEventListener('click', (event) =>
garminKudoAllHandler(event, findGarminKudosButtons),
);
container.append(button);
}
function garminConnectStandBy() {
const observer = new MutationObserver(() => {
executeGarmin();
});
observer.observe(globalThis.document.body, {
childList: true,
subtree: true,
});
}
function isHostStrava() {
return globalThis.window.location.hostname === 'www.strava.com';
}
function isHostGarmin() {
return globalThis.window.location.hostname === 'connect.garmin.com';
}
describe('KudoAll-Strava-Garmin.user.js', () => {
let sandbox;
beforeEach(() => {
sandbox = sinon.createSandbox();
globalThis.navigator = { language: 'en' };
globalThis.document = {
querySelector: sandbox.stub(),
querySelectorAll: sandbox.stub(),
createElement: sandbox
.stub()
.returns({ addEventListener: sandbox.stub() }),
body: {
appendChild: sandbox.stub(),
},
};
globalThis.window = {
location: {
hostname: 'www.strava.com',
pathname: '/modern/newsfeed',
},
onload: null,
};
globalThis.findStravaKudosButtons = findStravaKudosButtons;
globalThis.getStravaContainer = getStravaContainer;
globalThis.stravaStandBy = stravaStandBy;
globalThis.findGarminKudosButtons = findGarminKudosButtons;
globalThis.getGarminContainer = getGarminContainer;
globalThis.garminConnectStandBy = garminConnectStandBy;
globalThis.createStravaButton = createStravaButton;
globalThis.createGarminButton = createGarminButton;
globalThis.MutationObserver = class {
observe() {}
};
globalThis.document.querySelector
.withArgs('.strava-container')
.returns({ prepend: sandbox.stub() });
globalThis.document.querySelector
.withArgs('.garmin-container')
.returns({ append: sandbox.stub() });
});
afterEach(() => {
sandbox.restore();
delete globalThis.findStravaKudosButtons;
delete globalThis.getStravaContainer;
delete globalThis.stravaStandBy;
delete globalThis.findGarminKudosButtons;
delete globalThis.getGarminContainer;
delete globalThis.garminConnectStandBy;
delete globalThis.createStravaButton;
delete globalThis.createGarminButton;
delete globalThis.MutationObserver;
});
describe('getMessage', () => {
it('should return the correct message for the given language', () => {
const message = getMessage('kudo_all');
expect(message).to.equal('Kudo All');
});
it('should return the default message if the language is not supported', () => {
globalThis.navigator.language = 'xx';
const message = getMessage('kudo_all');
expect(message).to.equal('Kudo All');
});
});
describe('Strava', () => {
describe('getStravaContainer', () => {
it('should return the Strava container element', () => {
const container = { prepend: sandbox.stub() };
globalThis.document.querySelector
.withArgs('.strava-container')
.returns(container);
const result = getStravaContainer();
expect(result).to.equal(container);
});
});
describe('findStravaKudosButtons', () => {
it('should return an array of kudos buttons', () => {
const buttons = [{}];
globalThis.document.querySelectorAll
.withArgs('.strava-kudos-button')
.returns(buttons);
const result = findStravaKudosButtons();
expect(result).to.deep.equal(buttons);
});
});
describe('createStravaButton', () => {
it('should create and return a Strava button element', () => {
const button = { addEventListener: sandbox.stub() };
globalThis.document.createElement.withArgs('button').returns(button);
const result = createStravaButton();
expect(result).to.equal(button);
});
});
describe('stravaKudoAllHandler', () => {
it('should click all kudos buttons', () => {
const event = { preventDefault: sandbox.stub() };
const buttons = [{ parentElement: { click: sandbox.stub() } }];
sandbox.stub(global, 'findStravaKudosButtons').returns(buttons);
stravaKudoAllHandler(event, globalThis.findStravaKudosButtons);
expect(event.preventDefault.calledOnce).to.equal(true);
expect(buttons[0].parentElement.click.calledOnce).to.equal(true);
});
});
describe('stravaStandBy', () => {
it('should create and prepend the Kudo All button', () => {
const container = { prepend: sandbox.stub() };
const button = { addEventListener: sandbox.stub() };
sandbox.stub(global, 'getStravaContainer').returns(container);
sandbox.stub(global, 'createStravaButton').returns(button);
stravaStandBy();
expect(container.prepend.calledOnceWith(button)).to.equal(true);
});
it('should add click event listener to the button', () => {
const container = { prepend: sandbox.stub() };
const button = { addEventListener: sandbox.stub() };
sandbox.stub(global, 'getStravaContainer').returns(container);
sandbox.stub(global, 'createStravaButton').returns(button);
stravaStandBy();
expect(
button.addEventListener.calledOnceWith('click', sinon.match.func),
).to.equal(true);
});
it('should call findStravaKudosButtons when button is clicked', () => {
const container = { prepend: sandbox.stub() };
const button = { addEventListener: sandbox.stub() };
sandbox.stub(global, 'getStravaContainer').returns(container);
sandbox.stub(global, 'createStravaButton').returns(button);
const findStravaKudosButtonsStub = sandbox.stub(
global,
'findStravaKudosButtons',
);
stravaStandBy();
expect(button.addEventListener.calledOnce).to.equal(true);
const clickHandler = button.addEventListener.getCall(0).args[1];
clickHandler({ preventDefault: () => {} });
expect(findStravaKudosButtonsStub.calledOnce).to.equal(true);
});
});
});
describe('GC', () => {
describe('getGarminContainer', () => {
it('should return the Garmin container element', () => {
const container = { append: sandbox.stub() };
globalThis.document.querySelector
.withArgs('.garmin-container')
.returns(container);
const result = getGarminContainer();
expect(result).to.equal(container);
});
});
describe('findGarminKudosButtons', () => {
it('should return an array of kudos buttons', () => {
const buttons = [{}];
globalThis.document.querySelectorAll
.withArgs('.garmin-kudos-button')
.returns(buttons);
const result = findGarminKudosButtons();
expect(result).to.deep.equal(buttons);
});
});
describe('createGarminButton', () => {
it('should create and return a Garmin button element', () => {
const button = { addEventListener: sandbox.stub() };
globalThis.document.createElement.withArgs('button').returns(button);
const result = createGarminButton();
expect(result).to.equal(button);
});
});
describe('garminKudoAllHandler', () => {
it('should click all kudos buttons', () => {
const event = { preventDefault: sandbox.stub() };
const buttons = [{ click: sandbox.stub() }];
sandbox.stub(global, 'findGarminKudosButtons').returns(buttons);
garminKudoAllHandler(event, globalThis.findGarminKudosButtons);
expect(event.preventDefault.calledOnce).to.equal(true);
expect(buttons[0].click.calledOnce).to.equal(true);
});
});
describe('executeGarmin', () => {
it('should create and append the Kudo All button', () => {
const container = { append: sandbox.stub() };
const button = { addEventListener: sandbox.stub() };
sandbox.stub(global, 'getGarminContainer').returns(container);
sandbox.stub(global, 'createGarminButton').returns(button);
executeGarmin();
expect(container.append.calledOnceWith(button)).to.equal(true);
expect(
button.addEventListener.calledOnceWith('click', sinon.match.func),
).to.equal(true);
});
});
describe('garminConnectStandBy', () => {
it('should observe the DOM and execute Garmin logic when the target element is loaded', () => {
const observer = { observe: sandbox.stub() };
sandbox.stub(global, 'MutationObserver').returns(observer);
garminConnectStandBy();
expect(observer.observe.calledOnce).to.equal(true);
});
});
});
describe('Host Checks', () => {
describe('isHostStrava', () => {
it('should return true if the hostname matches Strava', () => {
globalThis.window.location.hostname = 'www.strava.com';
const result = isHostStrava();
expect(result).to.equal(true);
});
it('should return false if the hostname does not match Strava', () => {
globalThis.window.location.hostname = 'www.example.com';
const result = isHostStrava();
expect(result).to.equal(false);
});
});
describe('isHostGarmin', () => {
it('should return true if the hostname matches Garmin', () => {
globalThis.window.location.hostname = 'connect.garmin.com';
const result = isHostGarmin();
expect(result).to.equal(true);
});
it('should return false if the hostname does not match Garmin', () => {
globalThis.window.location.hostname = 'www.example.com';
const result = isHostGarmin();
expect(result).to.equal(false);
});
});
});
describe('Initialization', () => {
it('should initiate Strava standby if the host is Strava', () => {
globalThis.window.location.hostname = 'www.strava.com';
const stravaStandByStub = sandbox.stub(global, 'stravaStandBy');
globalThis.window.onload = () => {
if (isHostStrava()) {
stravaStandBy();
}
};
globalThis.window.onload();
expect(stravaStandByStub.calledOnce).to.equal(true);
});
it('should initiate Garmin standby if the host is Garmin', () => {
globalThis.window.location.hostname = 'connect.garmin.com';
const garminConnectStandByStub = sandbox.stub(
global,
'garminConnectStandBy',
);
globalThis.window.onload = () => {
if (isHostGarmin()) {
garminConnectStandBy();
}
};
globalThis.window.onload();
expect(garminConnectStandByStub.calledOnce).to.equal(true);
});
it('should not perform any actions if the host is not recognized', () => {
globalThis.window.location.hostname = 'www.example.com';
const stravaStandByStub = sandbox.stub(global, 'stravaStandBy');
const garminConnectStandByStub = sandbox.stub(
global,
'garminConnectStandBy',
);
globalThis.window.onload = () => {
if (isHostStrava()) {
stravaStandBy();
} else if (isHostGarmin()) {
garminConnectStandBy();
}
};
globalThis.window.onload();
expect(stravaStandByStub.called).to.equal(false);
expect(garminConnectStandByStub.called).to.equal(false);
});
});
});