-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathClockTest.php
More file actions
111 lines (77 loc) · 2.93 KB
/
ClockTest.php
File metadata and controls
111 lines (77 loc) · 2.93 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
<?php
declare(strict_types=1);
use DateTimeImmutable;
use Illuminate\Support\Facades\Route;
it('can install clock and advance time with runFor', function (): void {
Route::get('/', fn (): string => '
<div id="timestamp"></div>
<script>
document.getElementById("timestamp").textContent = Date.now();
</script>
');
$page = visit('/');
// Install clock with a specific time
$page->clock()->install(['time' => new DateTimeImmutable('2021-01-01 00:00:00')]);
// Reload to see the fake time
$page->reload();
$initialTime = (int) $page->evaluate('() => Date.now()');
// Advance time by 5 seconds (5000ms)
$page->clock()->runFor(5000);
$newTime = (int) $page->evaluate('() => Date.now()');
expect($newTime - $initialTime)->toBe(5000);
});
it('can set fixed time', function (): void {
Route::get('/', fn (): string => '<div></div>');
$page = visit('/');
$fixedTime = new DateTimeImmutable('2021-01-01 00:00:00');
$page->clock()->setFixedTime($fixedTime);
$currentTime = (int) $page->evaluate('() => Date.now()');
expect($currentTime)->toBe($fixedTime->getTimestamp() * 1000);
});
it('can pause and resume timers', function (): void {
Route::get('/', fn (): string => '
<div id="counter">0</div>
<script>
let counter = 0;
setInterval(() => {
counter++;
document.getElementById("counter").textContent = counter;
}, 1000);
</script>
');
$page = visit('/');
$page->clock()->install();
$page->reload();
// Pause time at current moment
$currentTime = (int) $page->evaluate('() => Date.now()');
$page->clock()->pauseAt($currentTime);
// Wait a bit and run timers manually
$page->clock()->runFor(3000); // 3 seconds
$counter = (int) $page->evaluate('() => document.getElementById("counter").textContent');
expect($counter)->toBe(3); // Should have ticked 3 times
});
it('can work with DateTimeInterface', function (): void {
Route::get('/', fn (): string => '<div></div>');
$page = visit('/');
$dateTime = new DateTimeImmutable('2021-01-01T10:30:00Z');
$page->clock()->setFixedTime($dateTime);
$isoString = (string) $page->evaluate('() => new Date().toISOString()');
expect($isoString)->toBe('2021-01-01T10:30:00.000Z');
});
it('can fast forward time', function (): void {
Route::get('/', fn (): string => '
<div id="result">waiting</div>
<script>
setTimeout(() => {
document.getElementById("result").textContent = "done";
}, 10000); // 10 seconds
</script>
');
$page = visit('/');
$page->clock()->install();
$page->reload();
// Fast forward 15 seconds
$page->clock()->fastForward(15000);
$result = (string) $page->evaluate('() => document.getElementById("result").textContent');
expect($result)->toBe('done');
});