Skip to content

Commit

Permalink
XHR: restructure FormData tests
Browse files Browse the repository at this point in the history
This commit restrucutres xhr/formdata tests that do not require a form element to be .any.js tests, and thus runnable outside the browser, and in workers (like in Deno). This commit only moves tests around (no changes or removals), with the exception of the "Passing a String object to FormData.set should work" and "Passing a String object to FormData.append should work." tests because the tests are exact replicas of the "formdata with string" test in xhr/formdata.html. Because of that they have been removed.
  • Loading branch information
lucacasonato authored Apr 15, 2021
1 parent 15874bf commit e45a9f9
Show file tree
Hide file tree
Showing 18 changed files with 277 additions and 358 deletions.
107 changes: 0 additions & 107 deletions xhr/FormData-append.html

This file was deleted.

46 changes: 0 additions & 46 deletions xhr/formdata-blob.htm

This file was deleted.

106 changes: 0 additions & 106 deletions xhr/formdata-set.htm

This file was deleted.

17 changes: 12 additions & 5 deletions xhr/formdata.htm → xhr/formdata.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,23 @@
return fd;
}

test(() => {
assert_throws_js(TypeError, () => { new FormData(null); });
assert_throws_js(TypeError, () => { new FormData("string"); });
}, "Constructors should throw a type error");

do_test("empty formdata", new FormData(), '\n');
do_test("formdata with string", create_formdata(['key', 'value']), 'key=value,\n');
do_test("formdata with named string", create_formdata(['key', new Blob(['value'], {type: 'text/plain'}), 'kv.txt']), '\nkey=kv.txt:text/plain:5,');
do_test("formdata from form", new FormData(document.getElementById('form')), 'key=value,\n');

do_test("formdata with blob", create_formdata(['key', new Blob(['value'], {type: 'text/x-value'})]), '\nkey=blob:text/x-value:5,');
do_test("formdata with named blob", create_formdata(['key', new Blob(['value'], {type: 'text/x-value'}), 'blob.txt']), '\nkey=blob.txt:text/x-value:5,');

// If 3rd argument is given and 2nd is not a Blob, formdata.append() should throw
const append_test = async_test('formdata.append() should throw if value is string and file name is given'); // needs to be async just because the others above are
append_test.step(function(){
assert_throws_js(TypeError, function(){
create_formdata('a', 'b', 'c');
});
});
append_test.done();

test(() => {
let form = populateForm('<input name=n1 value=v1>');
let formDataInEvent = null;
Expand Down
52 changes: 52 additions & 0 deletions xhr/formdata/append-formelement.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!doctype html>
<meta charset="utf-8">
<title>FormData.append (with form element)
</title>
<link rel="help" href="https://xhr.spec.whatwg.org/#dom-formdata-append">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<form id="form"></form>
<script>
test(function() {
var fd = new FormData(document.getElementById("form"));
fd.append('key', 'value1');
assert_equals(fd.get('key'), "value1");
}, 'testFormDataAppendToForm1');
test(function() {
var fd = new FormData(document.getElementById("form"));
fd.append('key', 'value2');
fd.append('key', 'value1');
assert_equals(fd.get('key'), "value2");
}, 'testFormDataAppendToForm2');
test(function() {
var fd = new FormData(document.getElementById("form"));
fd.append('key', undefined);
assert_equals(fd.get('key'), "undefined");
}, 'testFormDataAppendToFormUndefined1');
test(function() {
var fd = new FormData(document.getElementById("form"));
fd.append('key', undefined);
fd.append('key', 'value1');
assert_equals(fd.get('key'), "undefined");
}, 'testFormDataAppendToFormUndefined2');
test(function() {
var fd = new FormData(document.getElementById("form"));
fd.append('key', null);
assert_equals(fd.get('key'), "null");
}, 'testFormDataAppendToFormNull1');
test(function() {
var fd = new FormData(document.getElementById("form"));
fd.append('key', null);
fd.append('key', 'value1');
assert_equals(fd.get('key'), "null");
}, 'testFormDataAppendToFormNull2');
test(function() {
var fd = new FormData(document.getElementById("form"));
assert_throws_js(TypeError, () => {fd.append('name', "string", 'filename')});
}, 'testFormDataAppendToFormString');
test(function() {
var fd = new FormData(document.getElementById("form"));
assert_throws_js(TypeError, () => {fd.append('name', new URLSearchParams(), 'filename')});
}, 'testFormDataAppendToFormWrongPlatformObject');
</script>
37 changes: 37 additions & 0 deletions xhr/formdata/append.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// META: title=FormData.append

test(function() {
assert_equals(create_formdata(['key', 'value1']).get('key'), "value1");
}, 'testFormDataAppend1');
test(function() {
assert_equals(create_formdata(['key', 'value2'], ['key', 'value1']).get('key'), "value2");
}, 'testFormDataAppend2');
test(function() {
assert_equals(create_formdata(['key', undefined]).get('key'), "undefined");
}, 'testFormDataAppendUndefined1');
test(function() {
assert_equals(create_formdata(['key', undefined], ['key', 'value1']).get('key'), "undefined");
}, 'testFormDataAppendUndefined2');
test(function() {
assert_equals(create_formdata(['key', null]).get('key'), "null");
}, 'testFormDataAppendNull1');
test(function() {
assert_equals(create_formdata(['key', null], ['key', 'value1']).get('key'), "null");
}, 'testFormDataAppendNull2');
test(function() {
var before = new Date(new Date().getTime() - 2000); // two seconds ago, in case there's clock drift
var fd = create_formdata(['key', new Blob(), 'blank.txt']).get('key');
assert_equals(fd.name, "blank.txt");
assert_equals(fd.type, "");
assert_equals(fd.size, 0);
assert_greater_than_equal(fd.lastModified, before);
assert_less_than_equal(fd.lastModified, new Date());
}, 'testFormDataAppendEmptyBlob');

function create_formdata() {
var fd = new FormData();
for (var i = 0; i < arguments.length; i++) {
fd.append.apply(fd, arguments[i]);
};
return fd;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<title>FormData construction from a form element</title>
<title>FormData: constructor (with form element)</title>
<link rel="author" title="Domenic Denicola" href="mailto:d@domenic.me">
<link rel="help" href="https://xhr.spec.whatwg.org/#dom-formdata">
<link rel="help" href="https://html.spec.whatwg.org/multipage/#constructing-form-data-set">
Expand Down
6 changes: 6 additions & 0 deletions xhr/formdata/constructor.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// META: title=FormData: constructor

test(() => {
assert_throws_js(TypeError, () => { new FormData(null); });
assert_throws_js(TypeError, () => { new FormData("string"); });
}, "Constructors should throw a type error");
Loading

0 comments on commit e45a9f9

Please sign in to comment.