Skip to content

Commit 74a75c9

Browse files
authored
Test new URLSearchParams constructor features
The main thing here is new tests for https://github.com/whatwg/url/pull/175/files but I also found issues with some existing tests that I’m attempting to fix at the same time. The current tests assumed the constructor argument was mandatory, but it’s in fact optional. The other issue was that the current test assume stringifying DOMException.prototype throws, but I don’t think that’s the case.
1 parent 165824d commit 74a75c9

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

url/urlsearchparams-constructor.html

+40-6
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,25 @@
1818
}, 'Basic URLSearchParams construction');
1919

2020
test(function() {
21-
assert_throws(new TypeError(), function () { URLSearchParams(); },
22-
'Calling \'URLSearchParams\' without \'new\' should throw.');
23-
assert_throws(new TypeError(), function () { new URLSearchParams(DOMException.prototype); });
24-
var params = new URLSearchParams('');
21+
var params = new URLSearchParams()
22+
assert_equals(params.toString(), "")
23+
}, "URLSearchParams constructor, no arguments")
24+
25+
test(() => {
26+
params = new URLSearchParams(DOMException.prototype);
27+
assert_equals(params.toString(), "Error=")
28+
}, "URLSearchParams constructor, DOMException.prototype as argument")
29+
30+
test(() => {
31+
params = new URLSearchParams('');
2532
assert_true(params != null, 'constructor returned non-null value.');
2633
assert_equals(params.__proto__, URLSearchParams.prototype, 'expected URLSearchParams.prototype as prototype.');
34+
}, "URLSearchParams constructor, empty string as argument")
35+
36+
test(() => {
2737
params = new URLSearchParams({});
28-
assert_equals(params + '', '%5Bobject+Object%5D=');
29-
}, 'URLSearchParams constructor, empty.');
38+
assert_equals(params + '', "");
39+
}, 'URLSearchParams constructor, {} as argument');
3040

3141
test(function() {
3242
var params = new URLSearchParams('a=b');
@@ -124,6 +134,30 @@
124134
params = new URLSearchParams('a%f0%9f%92%a9b=c');
125135
assert_equals(params.get('a\uD83D\uDCA9b'), 'c');
126136
}, 'Parse %f0%9f%92%a9'); // Unicode Character 'PILE OF POO' (U+1F4A9)
137+
138+
;[
139+
{ "input": {"+": "%C2"}, "output": [[" ", "\uFFFD"]], "name": "object with +" },
140+
{ "input": {c: "x", a: "?"}, "output": [["c", "x"], ["a", "?"]], "name": "object with two keys" },
141+
{ "input": [["c", "x"], ["a", "?"]], "output": [["c", "x"], ["a", "?"]], "name": "array with two keys" }
142+
].forEach((val) => {
143+
test(() => {
144+
let params = new URLSearchParams(val.input),
145+
i = 0
146+
for (let param of params) {
147+
assert_array_equals(param, val.output[i])
148+
i++
149+
}
150+
}, "Construct with " + val.name)
151+
})
152+
153+
test(() => {
154+
params = new URLSearchParams()
155+
params[Symbol.iterator] = function *() {
156+
yield ["a", "b"]
157+
}
158+
let params2 = new URLSearchParams(params)
159+
assert_equals(params2.get("a"), "b")
160+
}, "Custom [Symbol.iterator]")
127161
</script>
128162
</head>
129163
</html>

0 commit comments

Comments
 (0)