Skip to content

Commit 464f42a

Browse files
committed
add history tests
1 parent 05e7079 commit 464f42a

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed

src/testing.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,13 @@ pub fn htmlRunner(file: []const u8) !void {
404404
try page.navigate(url, .{});
405405
_ = page.wait(2000);
406406

407+
const needs_second_wait = try js_context.exec("testing._onPageWait.length > 0", "check_onPageWait");
408+
if (needs_second_wait.value.toBool(page.main_context.isolate)) {
409+
// sets the isSecondWait flag in testing.
410+
_ = js_context.exec("testing._isSecondWait = true", "set_second_wait_flag") catch {};
411+
_ = page.wait(2000);
412+
}
413+
407414
@import("root").js_runner_duration += std.time.Instant.since(try std.time.Instant.now(), start);
408415

409416
const value = js_context.exec("testing.getStatus()", "testing.getStatus()") catch |err| {

src/tests/html/history.html

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@
22
<script src="../testing.js"></script>
33

44
<script id=history>
5-
testing.expectEqual('auto', history.scrollRestoration);
5+
testing.expectEqual('auto', history.scrollRestoration);
66

7-
history.scrollRestoration = 'manual';
8-
history.scrollRestoration = 'foo';
9-
testing.expectEqual('manual', history.scrollRestoration);
7+
history.scrollRestoration = 'manual';
8+
history.scrollRestoration = 'foo';
9+
testing.expectEqual('manual', history.scrollRestoration);
1010

11-
history.scrollRestoration = 'auto';
12-
testing.expectEqual('auto', history.scrollRestoration);
11+
history.scrollRestoration = 'auto';
12+
testing.expectEqual('auto', history.scrollRestoration);
13+
testing.expectEqual(null, history.state)
1314

14-
testing.expectEqual(null, history.state)
15+
history.pushState({ testInProgress: true }, null, 'http://127.0.0.1:9582/xhr/json');
16+
testing.expectEqual({ testInProgress: true }, history.state);
1517

16-
history.pushState({}, null, '');
17-
history.replaceState({}, null, '');
18+
history.replaceState({ "new": "field", testComplete: true }, null);
19+
let state = { "new": "field", testComplete: true };
20+
testing.expectEqual(state, history.state);
1821

19-
testing.expectEqual(undefined, history.go());
20-
testing.expectEqual(undefined, history.go(1));
21-
testing.expectEqual(undefined, history.go(-1));
22-
testing.expectEqual(undefined, history.forward());
23-
testing.expectEqual(undefined, history.back());
24-
</script>
22+
testing.expectEqual(undefined, history.back());
23+
testing.expectEqual(undefined, history.forward());
2524

26-
<script id=history-states>
27-
testing.expectEqual(null, history.state)
25+
testing.onPageWait(() => {
26+
testing.expectEqual(true, history.state && history.state.testComplete);
27+
testing.expectEqual(state, history.state);
28+
});
2829

29-
history.pushState({}, { "abc": "def" }, '');
30-
testing.expectEqual({ "abc": "def"}, history.state);
30+
testing.expectEqual(undefined, history.go());
3131
</script>

src/tests/testing.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@
5050
function getStatus() {
5151
// if we're already in a fail state, return fail, nothing can recover this
5252
if (testing._status === 'fail') return 'fail';
53+
54+
if (testing._isSecondWait) {
55+
for (const pw of (testing._onPageWait)) {
56+
testing._captured = pw[1];
57+
pw[0]();
58+
testing._captured = null;
59+
}
60+
}
61+
5362
// run any eventually's that we've captured
5463
for (const ev of testing._eventually) {
5564
testing._captured = ev[1];
@@ -92,6 +101,18 @@
92101
_registerErrorCallback();
93102
}
94103

104+
// Set expectations to happen on the next time that `page.wait` is executed.
105+
//
106+
// History specifically uses this as it queues navigation that needs to be checked
107+
// when the next page is loaded.
108+
function onPageWait(fn) {
109+
// Store callbacks to run when page.wait() happens
110+
testing._onPageWait.push([fn, {
111+
script_id: document.currentScript.id,
112+
stack: new Error().stack,
113+
}]);
114+
}
115+
95116
async function async(promise, cb) {
96117
const script_id = document.currentScript ? document.currentScript.id : '<script id is unavailable in browsers>';
97118
const stack = new Error().stack;
@@ -171,12 +192,15 @@
171192
window.testing = {
172193
_status: 'empty',
173194
_eventually: [],
195+
_onPageWait: [],
174196
_executed_scripts: new Set(),
175197
_captured: null,
198+
_isSecondWait: false,
176199
skip: skip,
177200
async: async,
178201
getStatus: getStatus,
179202
eventually: eventually,
203+
onPageWait: onPageWait,
180204
expectEqual: expectEqual,
181205
expectError: expectError,
182206
withError: withError,

0 commit comments

Comments
 (0)