Skip to content

Commit fcd08b8

Browse files
hiroppyevanlucas
authored andcommitted
test: add tests for searchParams
PR-URL: #10952 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent c329843 commit fcd08b8

11 files changed

+151
-1
lines changed

test/parallel/test-whatwg-url-searchparams-append.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,10 @@ assert.strictEqual(params.get('third'), '',
4646
params.append('first', 10);
4747
assert.strictEqual(params.get('first'), '1',
4848
'Search params object has name "first" with value "1"');
49+
50+
assert.throws(() => {
51+
params.append.call(undefined);
52+
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
53+
assert.throws(() => {
54+
params.set('a');
55+
}, /^TypeError: "name" and "value" arguments must be specified$/);

test/parallel/test-whatwg-url-searchparams-delete.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ params.delete('first');
4141
assert.strictEqual(false, params.has('first'),
4242
'Search params object has no "first" name');
4343

44+
assert.throws(() => {
45+
params.delete.call(undefined);
46+
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
47+
assert.throws(() => {
48+
params.delete();
49+
}, /^TypeError: "name" argument must be specified$/);
50+
4451
// https://github.com/nodejs/node/issues/10480
4552
// Emptying searchParams should correctly update url's query
4653
{
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const URLSearchParams = require('url').URLSearchParams;
6+
7+
const params = new URLSearchParams('a=b&c=d');
8+
const entries = params.entries();
9+
assert.strictEqual(typeof entries[Symbol.iterator], 'function');
10+
assert.strictEqual(entries[Symbol.iterator](), entries);
11+
assert.deepStrictEqual(entries.next(), {
12+
value: ['a', 'b'],
13+
done: false
14+
});
15+
assert.deepStrictEqual(entries.next(), {
16+
value: ['c', 'd'],
17+
done: false
18+
});
19+
assert.deepStrictEqual(entries.next(), {
20+
value: undefined,
21+
done: true
22+
});
23+
assert.deepStrictEqual(entries.next(), {
24+
value: undefined,
25+
done: true
26+
});
27+
28+
assert.throws(() => {
29+
entries.next.call(undefined);
30+
}, /^TypeError: Value of `this` is not a URLSearchParamsIterator$/);
31+
assert.throws(() => {
32+
params.entries.call(undefined);
33+
}, /^TypeError: Value of `this` is not a URLSearchParams$/);

test/parallel/test-whatwg-url-searchparams-foreach.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let a, b, i;
1212
const params = new URLSearchParams('a=1&b=2&c=3');
1313
const keys = [];
1414
const values = [];
15-
params.forEach(function(value, key) {
15+
params.forEach((value, key) => {
1616
keys.push(key);
1717
values.push(value);
1818
});
@@ -37,3 +37,7 @@ b = a.searchParams;
3737
for (i of b) {
3838
common.fail('should not be reached');
3939
}
40+
41+
assert.throws(() => {
42+
params.forEach.call(undefined);
43+
}, /^TypeError: Value of `this` is not a URLSearchParams$/);

test/parallel/test-whatwg-url-searchparams-get.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,10 @@ assert.strictEqual(params.get('third'), '',
2929
'Search params object has name "third" with empty value.');
3030
assert.strictEqual(params.get('fourth'), null,
3131
'Search params object has no "fourth" name and value.');
32+
33+
assert.throws(() => {
34+
params.get.call(undefined);
35+
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
36+
assert.throws(() => {
37+
params.get();
38+
}, /^TypeError: "name" argument must be specified$/);

test/parallel/test-whatwg-url-searchparams-getall.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,10 @@ assert(matches && matches.length == 1,
3636
'Search params object has values for name "a"');
3737
assert.deepStrictEqual(matches, ['one'],
3838
'Search params object has expected name "a" values');
39+
40+
assert.throws(() => {
41+
params.getAll.call(undefined);
42+
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
43+
assert.throws(() => {
44+
params.getAll();
45+
}, /^TypeError: "name" argument must be specified$/);

test/parallel/test-whatwg-url-searchparams-has.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,10 @@ assert.strictEqual(false, params.has('d'),
3333
params.delete('first');
3434
assert.strictEqual(false, params.has('first'),
3535
'Search params object has no name "first"');
36+
37+
assert.throws(() => {
38+
params.has.call(undefined);
39+
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
40+
assert.throws(() => {
41+
params.has();
42+
}, /^TypeError: "name" argument must be specified$/);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const URLSearchParams = require('url').URLSearchParams;
6+
7+
const params = new URLSearchParams('a=b&c=d');
8+
const keys = params.keys();
9+
10+
assert.strictEqual(typeof keys[Symbol.iterator], 'function');
11+
assert.strictEqual(keys[Symbol.iterator](), keys);
12+
assert.deepStrictEqual(keys.next(), {
13+
value: 'a',
14+
done: false
15+
});
16+
assert.deepStrictEqual(keys.next(), {
17+
value: 'c',
18+
done: false
19+
});
20+
assert.deepStrictEqual(keys.next(), {
21+
value: undefined,
22+
done: true
23+
});
24+
assert.deepStrictEqual(keys.next(), {
25+
value: undefined,
26+
done: true
27+
});
28+
29+
assert.throws(() => {
30+
keys.next.call(undefined);
31+
}, /^TypeError: Value of `this` is not a URLSearchParamsIterator$/);
32+
assert.throws(() => {
33+
params.keys.call(undefined);
34+
}, /^TypeError: Value of `this` is not a URLSearchParams$/);

test/parallel/test-whatwg-url-searchparams-set.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,10 @@ assert.strictEqual(true, params.has('a'),
3232
'Search params object has name "a"');
3333
assert.strictEqual(params.get('a'), '4',
3434
'Search params object has name "a" with value "4"');
35+
36+
assert.throws(() => {
37+
params.set.call(undefined);
38+
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
39+
assert.throws(() => {
40+
params.set('a');
41+
}, /^TypeError: "name" and "value" arguments must be specified$/);

test/parallel/test-whatwg-url-searchparams-stringifier.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,6 @@ assert.strictEqual(params + '', 'a%F0%9F%92%A9b=c');
110110
// The lone '=' _does_ survive the roundtrip.
111111
params = new URLSearchParams('a=&a=b');
112112
assert.strictEqual(params.toString(), 'a=&a=b');
113+
assert.throws(() => {
114+
params.toString.call(undefined);
115+
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const URLSearchParams = require('url').URLSearchParams;
6+
7+
const params = new URLSearchParams('a=b&c=d');
8+
const values = params.values();
9+
10+
assert.strictEqual(typeof values[Symbol.iterator], 'function');
11+
assert.strictEqual(values[Symbol.iterator](), values);
12+
assert.deepStrictEqual(values.next(), {
13+
value: 'b',
14+
done: false
15+
});
16+
assert.deepStrictEqual(values.next(), {
17+
value: 'd',
18+
done: false
19+
});
20+
assert.deepStrictEqual(values.next(), {
21+
value: undefined,
22+
done: true
23+
});
24+
assert.deepStrictEqual(values.next(), {
25+
value: undefined,
26+
done: true
27+
});
28+
29+
assert.throws(() => {
30+
values.next.call(undefined);
31+
}, /^TypeError: Value of `this` is not a URLSearchParamsIterator$/);
32+
assert.throws(() => {
33+
params.values.call(undefined);
34+
}, /^TypeError: Value of `this` is not a URLSearchParams$/);

0 commit comments

Comments
 (0)