-
Notifications
You must be signed in to change notification settings - Fork 31.2k
/
Copy pathtest-whatwg-url-searchparams-foreach.js
59 lines (54 loc) · 1.58 KB
/
test-whatwg-url-searchparams-foreach.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// whatwg url test should not be modified.
'use strict';
const common = require('../common');
const assert = require('assert');
const { URL, URLSearchParams } = require('url');
const { test, assert_array_equals, assert_unreached } = common.WPT;
/* eslint-disable */
var i; // Strict mode fix for WPT.
/* WPT Refs:
https://github.com/w3c/web-platform-tests/blob/a8b2b1e/url/urlsearchparams-foreach.html
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
*/
test(function() {
var params = new URLSearchParams('a=1&b=2&c=3');
var keys = [];
var values = [];
params.forEach(function(value, key) {
keys.push(key);
values.push(value);
});
assert_array_equals(keys, ['a', 'b', 'c']);
assert_array_equals(values, ['1', '2', '3']);
}, "ForEach Check");
test(function() {
let a = new URL("http://a.b/c?a=1&b=2&c=3&d=4");
let b = a.searchParams;
var c = [];
for (i of b) {
a.search = "x=1&y=2&z=3";
c.push(i);
}
assert_array_equals(c[0], ["a","1"]);
assert_array_equals(c[1], ["y","2"]);
assert_array_equals(c[2], ["z","3"]);
}, "For-of Check");
test(function() {
let a = new URL("http://a.b/c");
let b = a.searchParams;
for (i of b) {
assert_unreached(i);
}
}, "empty");
/* eslint-enable */
// Tests below are not from WPT.
{
const params = new URLSearchParams();
assert.throws(() => {
params.forEach.call(undefined);
}, common.expectsError({
code: 'ERR_INVALID_THIS',
type: TypeError,
message: 'Value of "this" must be of type URLSearchParams'
}));
}