summaryrefslogtreecommitdiff
path: root/js/url_search_params_test.ts
diff options
context:
space:
mode:
author迷渡 <justjavac@gmail.com>2018-12-19 09:14:59 +0800
committerRyan Dahl <ry@tinyclouds.org>2018-12-18 20:14:59 -0500
commit3dbd18af0950400eed7063bef7cb1f7ff27c4a01 (patch)
tree8112967e9bf1aa11aa84799312a4d93f1b45b059 /js/url_search_params_test.ts
parent528eb2adb38b44def28aeab006cd0ba1e2654239 (diff)
Fix URLSearchParams set() and constructor() (#1368)
Diffstat (limited to 'js/url_search_params_test.ts')
-rw-r--r--js/url_search_params_test.ts30
1 files changed, 28 insertions, 2 deletions
diff --git a/js/url_search_params_test.ts b/js/url_search_params_test.ts
index 46e8103d9..802ab288b 100644
--- a/js/url_search_params_test.ts
+++ b/js/url_search_params_test.ts
@@ -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);
+});