migrate more tests to htmlRunner

This commit is contained in:
Karl Seguin
2025-09-10 20:32:15 +08:00
parent edd41b37f0
commit f77693d768
10 changed files with 284 additions and 331 deletions

View File

@@ -92,6 +92,15 @@
_registerErrorCallback();
}
async function async(promise, cb) {
const script_id = document.currentScript.id;
const stack = new Error().stack;
const value = await promise;
this._captured = {script_id: script_id, stack: stack};
cb(value);
this._captured = null;
}
function _recordExecution() {
if (testing._status === 'fail') {
return;
@@ -161,6 +170,7 @@
_executed_scripts: new Set(),
_captured: null,
skip: skip,
async: async,
getStatus: getStatus,
eventually: eventually,
expectEqual: expectEqual,
@@ -177,4 +187,9 @@
window.$$ = function(sel) {
return document.querySelectorAll(sel);
}
if (!console.lp) {
// make this work in the browser
console.lp = console.log;
}
})();

5
src/tests/xhr/file.html Normal file
View File

@@ -0,0 +1,5 @@
<script src="../testing.js"></script>
<script id=file>
let f = new File()
testing.expectEqual(true, f instanceof File);
</script>

View File

@@ -0,0 +1,129 @@
<script src="../testing.js"></script>
<script id=formData>
let f = new FormData();
testing.expectEqual(null, f.get('a'));
testing.expectEqual(false, f.has('a'));
testing.expectEqual([], f.getAll('a'));
testing.expectEqual(undefined, f.delete('a'));
f.set('a', 1);
testing.expectEqual(true, f.has('a'));
testing.expectEqual('1', f.get('a'));
testing.expectEqual(['1'], f.getAll('a'));
f.append('a', 2);
testing.expectEqual(true, f.has('a'));
testing.expectEqual('1', f.get('a'));
testing.expectEqual(['1', '2'], f.getAll('a'));
f.append('b', '3');
testing.expectEqual(true, f.has('a'));
testing.expectEqual('1', f.get('a'));
testing.expectEqual(['1', '2'], f.getAll('a'));
testing.expectEqual(true, f.has('b'));
testing.expectEqual('3', f.get('b'));
testing.expectEqual(['3'], f.getAll('b'));
let acc = [];
for (const key of f.keys()) { acc.push(key) }
testing.expectEqual(['a', 'a', 'b'], acc);
acc = [];
for (const value of f.values()) { acc.push(value) }
testing.expectEqual(['1', '2', '3'], acc);
acc = [];
for (const entry of f.entries()) { acc.push(entry) }
testing.expectEqual([['a', '1'], ['a', '2'], ['b', '3']], acc);
acc = [];
for (const entry of f) { acc.push(entry) };
testing.expectEqual([['a', '1'], ['a', '2'], ['b', '3']], acc);
f.delete('a');
testing.expectEqual(false, f.has('a'));
testing.expectEqual(true, f.has('b'));
acc = [];
for (const key of f.keys()) { acc.push(key) }
testing.expectEqual(['b'], acc);
acc = [];
for (const value of f.values()) { acc.push(value) }
testing.expectEqual(['3'], acc);
acc = [];
for (const entry of f.entries()) { acc.push(entry) }
testing.expectEqual([['b', '3']], acc);
acc = [];
for (const entry of f) { acc.push(entry) }
testing.expectEqual([['b', '3']], acc);
</script>
<script id=serialize>
let form1 = $('#form1');
let submit1 = $('#s1');
let input = document.createElement('input');
input.name = 'dyn';
input.value = 'dyn-v';
form1.appendChild(input);
let f2 = new FormData(form1, submit1);
acc = [];
for (const entry of f2) {
acc.push(entry);
};
testing.expectEqual(['txt-1', 'txt-1-v'], acc[0]);
testing.expectEqual(['txt-2', 'txt-~-v'], acc[1]);
testing.expectEqual(['chk-3', 'chk-3-vb'], acc[2]);
testing.expectEqual(['chk-3', 'chk-3-vc'], acc[3]);
testing.expectEqual(['rdi-1', 'rdi-1-vc'], acc[4]);
testing.expectEqual(['ta-1', ' ta-1-v'], acc[5]);
testing.expectEqual(['ta', ''], acc[6]);
testing.expectEqual(['h1', 'h1-v'], acc[7]);
testing.expectEqual(['sel-1', 'blue'], acc[8]);
testing.expectEqual(['sel-2', 'sel-2-v'], acc[9]);
testing.expectEqual(['mlt-2', 'water'], acc[10]);
testing.expectEqual(['mlt-2', 'tea'], acc[11]);
testing.expectEqual(['s1', 's1-v'], acc[12]);
testing.expectEqual(['dyn', 'dyn-v'], acc[13]);
</script>
<form id="form1">
<input id="has_no_name" value="nope1">
<input id="is_disabled" disabled value="nope2">
<input name="txt-1" value="txt-1-v">
<input name="txt-2" value="txt-~-v" type=password>
<input name="chk-3" value="chk-3-va" type=checkbox>
<input name="chk-3" value="chk-3-vb" type=checkbox checked>
<input name="chk-3" value="chk-3-vc" type=checkbox checked>
<input name="chk-4" value="chk-4-va" type=checkbox>
<input name="chk-4" value="chk-4-va" type=checkbox>
<input name="rdi-1" value="rdi-1-va" type=radio>
<input name="rdi-1" value="rdi-1-vb" type=radio>
<input name="rdi-1" value="rdi-1-vc" type=radio checked>
<input name="rdi-2" value="rdi-2-va" type=radio>
<input name="rdi-2" value="rdi-2-vb" type=radio>
<textarea name="ta-1"> ta-1-v</textarea>
<textarea name="ta"></textarea>
<input type=hidden name=h1 value="h1-v">
<input type=hidden name=h2 value="h2-v" disabled=disabled>
<select name="sel-1"><option>blue<option>red</select>
<select name="sel-2"><option>blue<option value=sel-2-v selected>red</select>
<select name="sel-3"><option disabled>nope1<option>nope2</select>
<select name="mlt-1" multiple><option>water<option>tea</select>
<select name="mlt-2" multiple><option selected>water<option selected>tea<option>coffee</select>
<input type=submit id=s1 name=s1 value=s1-v>
<input type=submit name=s2 value=s2-v>
<input type=image name=i1 value=i1-v>
</form>
<input type=text name=abc value=123 form=form1>

View File

@@ -0,0 +1,16 @@
<script src="../testing.js"></script>
<script id=progressEvent>
let pevt = new ProgressEvent('foo');
testing.expectEqual(0, pevt.loaded);
testing.expectEqual(true, pevt instanceof ProgressEvent);
var eevt = null;
function ccbk(event) {
eevt = event;
}
document.addEventListener('foo', ccbk)
document.dispatchEvent(pevt);
testing.expectEqual('foo', eevt.type);
testing.expectEqual(true, eevt instanceof ProgressEvent);
</script>

109
src/tests/xhr/xhr.html Normal file
View File

@@ -0,0 +1,109 @@
<script src="../testing.js"></script>
<script id=xhr type=module>
const req = new XMLHttpRequest();
const promise1 = new Promise((resolve) => {
function cbk(event) {
resolve(event)
}
req.onload = cbk;
testing.expectEqual(cbk, req.onload);
req.onload = cbk;
req.open('GET', 'http://127.0.0.1:9582/xhr');
testing.expectEqual(0, req.status);
testing.expectEqual('', req.statusText);
testing.expectEqual('', req.getAllResponseHeaders());
testing.expectEqual(null, req.getResponseHeader('Content-Type'));
testing.expectEqual('', req.responseText);
req.send();
});
testing.async(promise1, (event) => {
testing.expectEqual('load', event.type);
testing.expectEqual(true, event.loaded > 0);
testing.expectEqual(true, event instanceof ProgressEvent);
testing.expectEqual(200, req.status);
testing.expectEqual('OK', req.statusText);
testing.expectEqual('text/html; charset=utf-8', req.getResponseHeader('Content-Type'));
testing.expectEqual('content-length: 100\r\nContent-Type: text/html; charset=utf-8\r\n', req.getAllResponseHeaders());
testing.expectEqual(100, req.responseText.length);
testing.expectEqual(req.responseText.length, req.response.length);
testing.expectEqual(true, req.responseXML instanceof Document);
});
</script>
<script id=xhr2 type=module>
const req2 = new XMLHttpRequest()
const promise2 = new Promise((resolve) => {
req2.onload = resolve;
req2.open('GET', 'http://127.0.0.1:9582/xhr')
req2.responseType = 'document';
req2.send()
});
testing.async(promise2, () => {
testing.expectEqual(200, req2.status);
testing.expectEqual('OK', req2.statusText);
testing.expectEqual(true, req2.response instanceof Document);
testing.expectEqual(true, req2.responseXML instanceof Document);
});
</script>
<script id=xhr3 type=module>
const req3 = new XMLHttpRequest()
const promise3 = new Promise((resolve) => {
req3.onload = resolve;
req3.open('GET', 'http://127.0.0.1:9582/xhr/json')
req3.responseType = 'json';
req3.send()
});
testing.async(promise3, () => {
testing.expectEqual(200, req3.status);
testing.expectEqual('OK', req3.statusText);
testing.expectEqual('9000!!!', req3.response.over);
});
</script>
<script id=xhr4 type=module>
const req4 = new XMLHttpRequest()
const promise4 = new Promise((resolve) => {
req4.onload = resolve;
req4.open('POST', 'http://127.0.0.1:9582/xhr')
req4.send('foo')
});
testing.async(promise4, () => {
testing.expectEqual(200, req4.status);
testing.expectEqual('OK', req4.statusText);
testing.expectEqual(true, req4.responseText.length > 64);
});
</script>
<script id=xhr5 type=module>
const promise5 = new Promise((resolve) => {
var state = [];
const req5 = new XMLHttpRequest();
req5.onreadystatechange = (e) => {
state.push(req5.readyState);
if (req5.readyState === XMLHttpRequest.DONE) {
resolve({states: state, target: e.currentTarget});
}
}
req5.open('GET', 'http://127.0.0.1:9582/xhr');
req5.send();
});
testing.async(promise5, (result) => {
const {states: states, target: target} = result;
testing.expectEqual(4, states.length)
testing.expectEqual(XMLHttpRequest.OPENED, readyStates[0]);
testing.expectEqual(XMLHttpRequest.HEADERS_RECEIVED, readyStates[1]);
testing.expectEqual(XMLHttpRequest.LOADING, readyStates[2]);
testing.expectEqual(XMLHttpRequest.DONE, readyStates[3]);
testing.expectEqual(req5, target);
})
</script>