This repository has been archived by the owner on May 30, 2023. It is now read-only.
/
25-5-clock-tests.js
751 lines (639 loc) · 24.5 KB
/
25-5-clock-tests.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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
import { assert } from 'chai';
import { clickButtonsById, getInputValue } from '../utils/element-utils';
import { frontEndLibrariesStack } from '../utils/shared-test-strings';
import { timeout } from '../utils/threading';
export default function create25Plus5ClockTests() {
const breakMin = 'break-decrement';
const breakPlus = 'break-increment';
const seshMin = 'session-decrement';
const seshPlus = 'session-increment';
const reset = 'reset';
const startStop = 'start_stop';
const originalTimerLabel =
document.getElementById('timer-label') &&
document.getElementById('timer-label').innerText;
function resetTimer() {
clickButtonsById([reset]);
}
// The regex checks for correct time format ([mm]mm:ss)
const timerRe = new RegExp(/^(\d{2,4})[\.:,\/](\d{2})$/);
function getMinutes(str) {
return timerRe.exec(str)[1];
}
function getSeconds(str) {
return timerRe.exec(str)[2];
}
/* eslint-enable max-len*/
function observeElement(target, callback) {
// create an observer instance
var observer = new MutationObserver(callback);
// configuration of the observer:
var config = {
childList: true,
characterData: true,
subtree: true
};
// pass in the target node, as well as the observer options
observer.observe(target, config);
return observer;
}
const waitForState = (target, check, errMsg, timeout = 2000) =>
new Promise((resolve, reject) => {
const timerId = savedSetTimeout(() => {
observer.disconnect();
clearTimeout(waitForChangesTimerId);
reject(new Error(errMsg));
}, timeout);
// If a target does not change for 5 seconds,
// then stop waiting for changes.
const waitForChangesTimerId = savedSetTimeout(() => {
observer.disconnect();
clearTimeout(timerId);
reject(new Error(errMsg));
}, 5000);
const observer = observeElement(target, () => {
clearTimeout(waitForChangesTimerId);
if (check()) {
observer.disconnect();
clearTimeout(timerId);
resolve();
}
});
});
// Resolves when the timer reaches 00:00.
const timerHasReachedZero = (timeout = 90000) => {
const target = document.getElementById('time-left');
return waitForState(
target,
() => /^00[\.:,\/]00$/.test(target.innerText),
'Timer has not reached 00:00.',
timeout
);
};
// Resolves when the timer changes.
const timerStateHasChanged = () => {
const target = document.getElementById('time-left');
return waitForState(target, () => true, 'Timer has not changed.');
};
// We "Hack" the global setTimeout and setInterval functions so time elapses
// faster (delay is forced to 30ms)
// The problem is that we still don't know if it's acceptable to use this
// hack, because it implies forcing the campers to use setTimeout and
// setInterval functions to measure time in their 25 + 5 clock.
// In cases where hacking does not work, we wait for the timer
// as much time as is usually required for it.
const savedSetTimeout = window.setTimeout;
const savedSetInterval = window.setInterval;
function hackGlobalTimerFunctions() {
window.setTimeout = (fun) => {
return savedSetTimeout(fun, 30);
};
window.setInterval = (fun) => {
return savedSetInterval(fun, 30);
};
}
function restoreGlobalTimerFunctions() {
window.setTimeout = savedSetTimeout;
window.setInterval = savedSetInterval;
}
// Test suite
describe('#25 + 5 Clock tests', function () {
beforeEach(function () {
resetTimer();
});
afterEach(function () {
restoreGlobalTimerFunctions();
});
after(function () {
resetTimer();
restoreGlobalTimerFunctions();
});
describe('#Technology Stack', function () {
it(frontEndLibrariesStack, function () {
return true;
});
});
describe('#Content', function () {
it(`I can see an element with id="break-label" that contains a
string (e.g. “Break Length”).`, function () {
const breakTitle = document.getElementById('break-label');
assert.isAbove(
breakTitle.innerText.length,
0,
'Element does not contain a string'
);
});
it(`I can see an element with id="session-label" that contains
a string (e.g. "Session Length”).`, function () {
const sessionTitle = document.getElementById('session-label');
assert.isAbove(
sessionTitle.innerText.length,
0,
'Element does not contain a string'
);
});
it(`I can see two clickable elements with corresponding IDs:
id="break-decrement" and id="session-decrement".`, function () {
assert.isNotNull(document.getElementById('break-decrement'));
assert.isNotNull(document.getElementById('session-decrement'));
});
it(`I can see two clickable elements with corresponding IDs:
id="break-increment" and id="session-increment".`, function () {
assert.isNotNull(document.getElementById('break-increment'));
assert.isNotNull(document.getElementById('session-increment'));
});
it(`I can see an element, with corresponding id="break-length",
which by default (on load) displays a value of 5.`, function () {
const breakLength = document.getElementById('break-length');
assert.strictEqual(
getInputValue(breakLength),
'5',
'A value of 5 is not displayed by default'
);
});
it(`I can see an element, with corresponding
id="session-length", which by default displays a value of 25.`, function () {
const sessionLength = document.getElementById('session-length');
assert.strictEqual(
getInputValue(sessionLength),
'25',
'A value of 25 is not displayed by default'
);
});
it(`I can see an element, with corresponding id="timer-label",
that contains a string indicating a session is initialized
(e.g. "Session").`, function () {
const timerLabel = document.getElementById('timer-label');
assert.isAbove(
timerLabel.innerText.length,
0,
'Element does not contain a string'
);
});
it(`I can see an element with corresponding id="time-left".
NOTE: Paused or running, the value in this field should always be
displayed in mm:ss format (i.e. 25:00).`, async function () {
const target = document.getElementById('time-left');
assert.isNotNull(target);
assert.strictEqual(
getMinutes(target.innerText),
'25',
'time-left is not formatted correctly'
);
// Set session length to 60
clickButtonsById(Array(35).fill(seshPlus));
// wait for 1.5 seconds to allow any re-renders to catch up
await timeout(1500);
assert.strictEqual(
getMinutes(target.innerText),
'60',
'time-left is not formatted correctly'
);
});
it(`I can see a clickable element with corresponding
id="start_stop".`, function () {
assert.isNotNull(document.getElementById('start_stop'));
});
it(`I can see a clickable element with corresponding
id="reset".`, function () {
assert.isNotNull(document.getElementById('reset'));
});
});
describe('#Timer', function () {
it(`When I click the element with the id of "reset", any
running timer should be stopped, the value within id="break-length" should
return to 5, the value within id="session-length" should return to 25, and
the element with id="time-left" should reset to it's default state.`, async function () {
this.timeout(100000);
hackGlobalTimerFunctions();
// decrement session and break length
clickButtonsById(Array(60).fill(seshMin));
clickButtonsById(Array(60).fill(breakMin));
// start the 25 + 5 clock
clickButtonsById([startStop]);
// wait while timer reaches 00:00
await timerHasReachedZero();
restoreGlobalTimerFunctions();
// once timer has reached zero wait 1.5 seconds then reset and
// see if every default value is reset
await timeout(1500);
resetTimer();
const timerLabelAfterReset = document.getElementById('timer-label')
.innerText;
const secondsAfterReset = getSeconds(
document.getElementById('time-left').innerText
);
// see if timer label changed back
assert.strictEqual(
timerLabelAfterReset,
originalTimerLabel,
'Default timer label was not properly reset '
);
// wait another 1.5 seconds to be sure value has not changed
// (25 + 5 clock is stopped)
await timeout(1500);
assert.strictEqual(
getInputValue(document.getElementById('break-length')),
'5',
'Default values for break length were not properly reset'
);
assert.strictEqual(
getInputValue(document.getElementById('session-length')),
'25',
'Default values for session length were not properly reset'
);
const secondsAfterAWhile = getSeconds(
document.getElementById('time-left').innerText
);
assert.strictEqual(
secondsAfterAWhile,
secondsAfterReset,
'25 + 5 has paused but time continued elapsing'
);
});
it(`When I click the element with the id of "break-decrement",
the value within id="break-length" decrements by a value of 1, and I can
see the updated value.`, function () {
clickButtonsById([breakMin, breakMin, breakMin, breakMin]);
assert.strictEqual(
getInputValue(document.getElementById('break-length')),
'1'
);
resetTimer();
clickButtonsById([breakMin]);
assert.strictEqual(
getInputValue(document.getElementById('break-length')),
'4'
);
});
it(`When I click the element with the id of "break-increment",
the value within id="break-length" increments by a value of 1, and I can
see the updated value.`, function () {
clickButtonsById(Array(4).fill(breakPlus));
assert.strictEqual(
getInputValue(document.getElementById('break-length')),
'9'
);
resetTimer();
clickButtonsById([breakPlus]);
assert.strictEqual(
getInputValue(document.getElementById('break-length')),
'6'
);
});
it(`When I click the element with the id of
"session-decrement", the value within id="session-length" decrements by a
value of 1, and I can see the updated value.`, function () {
clickButtonsById(Array(4).fill(seshMin));
assert.strictEqual(
getInputValue(document.getElementById('session-length')),
'21'
);
resetTimer();
clickButtonsById([seshMin]);
assert.strictEqual(
getInputValue(document.getElementById('session-length')),
'24'
);
});
it(`When I click the element with the id of
"session-increment", the value within id="session-length" increments by a
value of 1, and I can see the updated value.`, function () {
clickButtonsById(Array(4).fill(seshPlus));
assert.strictEqual(
getInputValue(document.getElementById('session-length')),
'29'
);
resetTimer();
clickButtonsById([seshPlus]);
assert.strictEqual(
getInputValue(document.getElementById('session-length')),
'26'
);
});
it(`I should not be able to set a session or break length to
<= 0.`, function () {
clickButtonsById(Array(10).fill(breakMin));
assert.strictEqual(
getInputValue(document.getElementById('break-length')),
'1',
'Value in element with id of "break-length" is less than 1.'
);
resetTimer();
clickButtonsById(Array(30).fill(seshMin));
assert.strictEqual(
getInputValue(document.getElementById('session-length')),
'1',
'Value in element with id of "session-length" is less than 1.'
);
});
it(`I should not be able to set a session or break length to
> 60.`, function () {
clickButtonsById(Array(60).fill(breakPlus));
assert.strictEqual(
getInputValue(document.getElementById('break-length')),
'60',
'Value in element with id of "break-length" is greater than 60.'
);
resetTimer();
clickButtonsById(Array(40).fill(seshPlus));
assert.strictEqual(
getInputValue(document.getElementById('session-length')),
'60',
'Value in element with id of "session-length" is greater than 60.'
);
});
it(`When I first click the element with id="start_stop", the
timer should begin running from the value currently displayed in
id="session-length", even if the value has been incremented or
decremented from the original value of 25.`, function () {
clickButtonsById([startStop]);
assert.strictEqual(
getMinutes(document.getElementById('time-left').innerText),
getInputValue(document.getElementById('session-length'))
);
});
it(`If the timer is running, the element with the id of
"time-left" should display the remaining time in mm:ss format
(decrementing by a value of 1 and updating the display every 1000ms).`, async function () {
this.timeout(2500);
// start the 25 + 5 clock
clickButtonsById([startStop]);
const secondsBefore = getSeconds(
document.getElementById('time-left').innerText
);
// wait 1.5 seconds then see if displayed time has changed
// (decremented)
await timeout(1500);
const secondsAfter = getSeconds(
document.getElementById('time-left').innerText
);
assert.isAbove(
+secondsAfter,
+secondsBefore,
'25 + 5 clock has started but time displayed is not changing '
);
});
it(`If the timer is running and I click the element with
id="start_stop", the countdown should pause.`, async function () {
this.timeout(4000);
// start the 25 + 5 clock
clickButtonsById([startStop]);
const secondsBefore = getSeconds(
document.getElementById('time-left').innerText
);
// wait 1.5 seconds then see if displayed time has changed
await timeout(1500);
const secondsAfter = getSeconds(
document.getElementById('time-left').innerText
);
assert.notStrictEqual(
secondsAfter,
secondsBefore,
'25 + 5 has started but time displayed is not changing'
);
// Pause the 25 + 5 clock
clickButtonsById([startStop]);
// wait another 1.5 seconds to be sure value has not changed
await timeout(1500);
const secondsAfterPause = getSeconds(
document.getElementById('time-left').innerText
);
assert.strictEqual(
secondsAfterPause,
secondsAfter,
'25 + 5 clock has paused but time continued elapsing'
);
});
it(`If the timer is paused and I click the element with
id="start_stop", the countdown should resume running from the point at
which it was paused.`, async function () {
this.timeout(6000);
// start the 25 + 5 clock
clickButtonsById([startStop]);
const secondsBefore = getSeconds(
document.getElementById('time-left').innerText
);
// wait 1.5 seconds then see if displayed time has changed
await timeout(1500);
const secondsAfter = getSeconds(
document.getElementById('time-left').innerText
);
assert.notStrictEqual(
secondsAfter,
secondsBefore,
'25 + 5 clock has started but time displayed is not changing'
);
// Pause the 25 + 5 clock
clickButtonsById([startStop]);
// wait another 1.5 seconds to be sure value has not changed
await timeout(1500);
const secondsAfterPause = getSeconds(
document.getElementById('time-left').innerText
);
assert.strictEqual(
secondsAfterPause,
secondsAfter,
'25 + 5 clock has paused but time continued elapsing'
);
// Resume the 25 + 5 clock
clickButtonsById([startStop]);
// wait another 1.5 seconds to be sure time is decrementing again
await timeout(1500);
const secondsAfterResume = getSeconds(
document.getElementById('time-left').innerText
);
assert.isBelow(
+secondsAfterResume,
+secondsAfterPause,
'25 + 5 clock has resumed but displayed time is not changing '
);
});
it(`When a session countdown reaches zero (NOTE: timer MUST
reach 00:00), and a new countdown begins, the element with the id of
"timer-label" should display a string indicating a break has begun.`, async function () {
this.timeout(100000);
hackGlobalTimerFunctions();
// we decrement session time to the minimum (1 minute)
clickButtonsById(Array(60).fill(seshMin));
// start the 25 + 5 clock
clickButtonsById([startStop]);
let tLabelA = document.getElementById('timer-label').innerHTML;
await timerHasReachedZero();
await timerStateHasChanged();
const breakLength = +getInputValue(
document.getElementById('break-length')
);
const breakTime = +getMinutes(
document.getElementById('time-left').innerText
);
assert.isAtMost(
breakTime,
breakLength,
"Break time didn't start with the correct value."
);
let tLabelB = document.getElementById('timer-label').innerHTML;
assert.notStrictEqual(
tLabelB,
tLabelA,
"Timer has reached zero but didn't switch to Break time"
);
});
it(`When a session countdown reaches zero (NOTE: timer MUST
reach 00:00), a new break countdown should begin, counting down from the
value currently displayed in the id="break-length" element.`, async function () {
this.timeout(100000);
hackGlobalTimerFunctions();
// we decrement session time to the minimum (1 minute)
clickButtonsById(Array(60).fill(seshMin));
// start the 25 + 5 clock
clickButtonsById([startStop]);
let sessionLabel = document.getElementById('timer-label').innerHTML;
await timerHasReachedZero();
await timerStateHasChanged();
const currentTimer = document.getElementById('timer-label').innerHTML;
assert.notStrictEqual(
currentTimer,
sessionLabel,
"Timer has reached zero but didn't switch to Break time"
);
const breakLength = +getInputValue(
document.getElementById('break-length')
);
const breakTime = +getMinutes(
document.getElementById('time-left').innerText
);
assert.strictEqual(
breakTime,
breakLength,
"Timer has switched to Break time, but it didn't start with " +
'the correct value.'
);
});
it(`When a break countdown reaches zero (NOTE: timer MUST reach
00:00), and a new countdown begins, the element with the id of
"timer-label" should display a string indicating a session has begun.`, async function () {
this.timeout(200000);
hackGlobalTimerFunctions();
// decrement session length and break length to the minimum (1 minute)
clickButtonsById(Array(60).fill(seshMin));
clickButtonsById(Array(60).fill(breakMin));
// start the 25 + 5 clock
clickButtonsById([startStop]);
await timerHasReachedZero();
await timerStateHasChanged();
const breakLabel = document.getElementById('timer-label').innerHTML;
await timerHasReachedZero();
await timerStateHasChanged();
const timerLabel = document.getElementById('timer-label').innerHTML;
assert.notStrictEqual(
timerLabel,
breakLabel,
"Timer has reached zero but didn't switch back to Session time."
);
});
it(`When a break countdown reaches zero (NOTE: timer MUST
reach 00:00), a new session countdown should begin, counting down from
the value currently displayed in the id="session-length" element.`, async function () {
this.timeout(200000);
hackGlobalTimerFunctions();
// decrement session length and break length to the minimum (1 minute)
clickButtonsById(Array(60).fill(seshMin));
clickButtonsById(Array(60).fill(breakMin));
// start the 25 + 5 clock
clickButtonsById([startStop]);
await timerHasReachedZero();
await timerStateHasChanged();
const breakLabel = document.getElementById('timer-label').innerHTML;
await timerHasReachedZero();
await timerStateHasChanged();
const timerLabel = document.getElementById('timer-label').innerHTML;
assert.notStrictEqual(
timerLabel,
breakLabel,
"Timer has reached zero but didn't switch back to Session time."
);
const sessionLength = +getInputValue(
document.getElementById('session-length')
);
const currentTime = +getMinutes(
document.getElementById('time-left').innerText
);
assert.strictEqual(
currentTime,
sessionLength,
'Timer has switched back to Session time, but it ' +
"didn't start with the correct value."
);
});
});
describe('#Audio', function () {
it(`When a countdown reaches zero (NOTE: timer MUST reach
00:00), a sound indicating that time is up should play. This should
utilize an HTML5 <audio> tag and have a corresponding id="beep".`, async function () {
this.timeout(100000);
assert.isNotNull(
document.querySelector('audio#beep'),
'There is no audio tag with ID "beep" on the page.'
);
hackGlobalTimerFunctions();
// decrement session time to the minimum (1 minute)
clickButtonsById(Array(60).fill(seshMin));
// start the 25 + 5 clock
clickButtonsById([startStop]);
await timerHasReachedZero();
restoreGlobalTimerFunctions();
await timeout(200);
assert.isFalse(
document.getElementById('beep').paused,
'Timer has reached zero but audio is not playing while it should.'
);
});
it(`The audio element with id="beep" must be 1 second or
longer.`, async function () {
const audio = document.querySelector('audio#beep');
assert.isNotNull(
audio,
'There is no audio tag with ID "beep" on the page.'
);
if (audio.readyState === 0) {
// Wait for the audio to load.
await new Promise((resolve) => {
const listener = audio.addEventListener('loadeddata', () => {
if (audio.readyState > 0) {
audio.removeEventListener('loadeddata', listener);
resolve();
}
});
});
}
assert.isAbove(
document.getElementById('beep').duration,
1,
'Audio element with id="beep" is not at least 1 second long.'
);
});
it(`The audio element with id of "beep" must stop playing and
be rewound to the beginning when the element with the id of "reset" is
clicked.`, function () {
// Call document.getElementById('beep') each time to overcome framework
// cache
document.getElementById('beep').play();
resetTimer();
assert.isTrue(
document.getElementById('beep').paused,
'Audio element was not stopped when reset was clicked.'
);
assert.strictEqual(
document.getElementById('beep').currentTime,
0,
'Audio element was not rewound when reset was clicked. HINT: use ' +
'the currentTime property of the audio element to rewind.'
);
});
// END #Audio
});
// END #25Plus5ClockTests
});
// END create25Plus5ClockTests()
}