aboutsummaryrefslogtreecommitdiff
path: root/test/parallel/test-whatwg-url-searchparams-foreach.js
blob: b6d684b06743b08188febd41580eed36aeb3817c (plain)
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
'use strict';

require('../common');
const assert = require('assert');
const URL = require('url').URL;

const m = new URL('http://example.org');
let params = m.searchParams;

// Until we export URLSearchParams
const URLSearchParams = params.constructor;

let a, b, i;

// ForEach Check
params = new URLSearchParams('a=1&b=2&c=3');
const keys = [];
const values = [];
params.forEach(function(value, key) {
  keys.push(key);
  values.push(value);
});
assert.deepStrictEqual(keys, ['a', 'b', 'c']);
assert.deepStrictEqual(values, ['1', '2', '3']);

// For-of Check
a = new URL('http://a.b/c?a=1&b=2&c=3&d=4');
b = a.searchParams;
const c = [];
for (i of b) {
  a.search = 'x=1&y=2&z=3';
  c.push(i);
}
assert.deepStrictEqual(c[0], ['a', '1']);
assert.deepStrictEqual(c[1], ['y', '2']);
assert.deepStrictEqual(c[2], ['z', '3']);

// empty
a = new URL('http://a.b/c');
b = a.searchParams;
for (i of b) {
  assert(false, 'should not be reached');
}