Fix URLSearchParams set() and constructor() (#1368)

This commit is contained in:
迷渡 2018-12-19 09:14:59 +08:00 committed by Ryan Dahl
parent 528eb2adb3
commit 3dbd18af09
2 changed files with 57 additions and 4 deletions

View file

@ -59,11 +59,18 @@ test(function urlSearchParamsHasSuccess() {
assert(!searchParams.has("c"));
});
test(function urlSearchParamsSetSuccess() {
test(function urlSearchParamsSetReplaceFirstAndRemoveOthers() {
const init = "a=54&b=true&a=true";
const searchParams = new URLSearchParams(init);
searchParams.set("a", "false");
assertEqual(searchParams.toString(), "b=true&a=false");
assertEqual(searchParams.toString(), "a=false&b=true");
});
test(function urlSearchParamsSetAppendNew() {
const init = "a=54&b=true&a=true";
const searchParams = new URLSearchParams(init);
searchParams.set("c", "foo");
assertEqual(searchParams.toString(), "a=54&b=true&a=true&c=foo");
});
test(function urlSearchParamsSortSuccess() {
@ -112,3 +119,22 @@ test(function urlSearchParamsMissingPair() {
const searchParams = new URLSearchParams(init);
assertEqual(searchParams.toString(), "c=4&a=54");
});
// If pair does not contain exactly two items, then throw a TypeError.
// ref https://url.spec.whatwg.org/#interface-urlsearchparams
test(function urlSearchParamsShouldThrowTypeError() {
let hasThrown = 0;
try {
new URLSearchParams([["1"]]);
hasThrown = 1;
} catch (err) {
if (err instanceof TypeError) {
hasThrown = 2;
} else {
hasThrown = 3;
}
}
assertEqual(hasThrown, 2);
});