test: add tests for searchParams
PR-URL: https://github.com/nodejs/node/pull/10952 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
parent
18d4ee97d8
commit
e7f4825c8a
@ -46,3 +46,10 @@ assert.strictEqual(params.get('third'), '',
|
||||
params.append('first', 10);
|
||||
assert.strictEqual(params.get('first'), '1',
|
||||
'Search params object has name "first" with value "1"');
|
||||
|
||||
assert.throws(() => {
|
||||
params.append.call(undefined);
|
||||
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
|
||||
assert.throws(() => {
|
||||
params.set('a');
|
||||
}, /^TypeError: "name" and "value" arguments must be specified$/);
|
||||
|
@ -41,6 +41,13 @@ params.delete('first');
|
||||
assert.strictEqual(false, params.has('first'),
|
||||
'Search params object has no "first" name');
|
||||
|
||||
assert.throws(() => {
|
||||
params.delete.call(undefined);
|
||||
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
|
||||
assert.throws(() => {
|
||||
params.delete();
|
||||
}, /^TypeError: "name" argument must be specified$/);
|
||||
|
||||
// https://github.com/nodejs/node/issues/10480
|
||||
// Emptying searchParams should correctly update url's query
|
||||
{
|
||||
|
33
test/parallel/test-whatwg-url-searchparams-entries.js
Normal file
33
test/parallel/test-whatwg-url-searchparams-entries.js
Normal file
@ -0,0 +1,33 @@
|
||||
'use strict';
|
||||
|
||||
require('../common');
|
||||
const assert = require('assert');
|
||||
const URLSearchParams = require('url').URLSearchParams;
|
||||
|
||||
const params = new URLSearchParams('a=b&c=d');
|
||||
const entries = params.entries();
|
||||
assert.strictEqual(typeof entries[Symbol.iterator], 'function');
|
||||
assert.strictEqual(entries[Symbol.iterator](), entries);
|
||||
assert.deepStrictEqual(entries.next(), {
|
||||
value: ['a', 'b'],
|
||||
done: false
|
||||
});
|
||||
assert.deepStrictEqual(entries.next(), {
|
||||
value: ['c', 'd'],
|
||||
done: false
|
||||
});
|
||||
assert.deepStrictEqual(entries.next(), {
|
||||
value: undefined,
|
||||
done: true
|
||||
});
|
||||
assert.deepStrictEqual(entries.next(), {
|
||||
value: undefined,
|
||||
done: true
|
||||
});
|
||||
|
||||
assert.throws(() => {
|
||||
entries.next.call(undefined);
|
||||
}, /^TypeError: Value of `this` is not a URLSearchParamsIterator$/);
|
||||
assert.throws(() => {
|
||||
params.entries.call(undefined);
|
||||
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
|
@ -12,7 +12,7 @@ let a, b, i;
|
||||
const params = new URLSearchParams('a=1&b=2&c=3');
|
||||
const keys = [];
|
||||
const values = [];
|
||||
params.forEach(function(value, key) {
|
||||
params.forEach((value, key) => {
|
||||
keys.push(key);
|
||||
values.push(value);
|
||||
});
|
||||
@ -37,3 +37,7 @@ b = a.searchParams;
|
||||
for (i of b) {
|
||||
common.fail('should not be reached');
|
||||
}
|
||||
|
||||
assert.throws(() => {
|
||||
params.forEach.call(undefined);
|
||||
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
|
||||
|
@ -29,3 +29,10 @@ assert.strictEqual(params.get('third'), '',
|
||||
'Search params object has name "third" with empty value.');
|
||||
assert.strictEqual(params.get('fourth'), null,
|
||||
'Search params object has no "fourth" name and value.');
|
||||
|
||||
assert.throws(() => {
|
||||
params.get.call(undefined);
|
||||
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
|
||||
assert.throws(() => {
|
||||
params.get();
|
||||
}, /^TypeError: "name" argument must be specified$/);
|
||||
|
@ -36,3 +36,10 @@ assert(matches && matches.length == 1,
|
||||
'Search params object has values for name "a"');
|
||||
assert.deepStrictEqual(matches, ['one'],
|
||||
'Search params object has expected name "a" values');
|
||||
|
||||
assert.throws(() => {
|
||||
params.getAll.call(undefined);
|
||||
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
|
||||
assert.throws(() => {
|
||||
params.getAll();
|
||||
}, /^TypeError: "name" argument must be specified$/);
|
||||
|
@ -33,3 +33,10 @@ assert.strictEqual(false, params.has('d'),
|
||||
params.delete('first');
|
||||
assert.strictEqual(false, params.has('first'),
|
||||
'Search params object has no name "first"');
|
||||
|
||||
assert.throws(() => {
|
||||
params.has.call(undefined);
|
||||
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
|
||||
assert.throws(() => {
|
||||
params.has();
|
||||
}, /^TypeError: "name" argument must be specified$/);
|
||||
|
34
test/parallel/test-whatwg-url-searchparams-keys.js
Normal file
34
test/parallel/test-whatwg-url-searchparams-keys.js
Normal file
@ -0,0 +1,34 @@
|
||||
'use strict';
|
||||
|
||||
require('../common');
|
||||
const assert = require('assert');
|
||||
const URLSearchParams = require('url').URLSearchParams;
|
||||
|
||||
const params = new URLSearchParams('a=b&c=d');
|
||||
const keys = params.keys();
|
||||
|
||||
assert.strictEqual(typeof keys[Symbol.iterator], 'function');
|
||||
assert.strictEqual(keys[Symbol.iterator](), keys);
|
||||
assert.deepStrictEqual(keys.next(), {
|
||||
value: 'a',
|
||||
done: false
|
||||
});
|
||||
assert.deepStrictEqual(keys.next(), {
|
||||
value: 'c',
|
||||
done: false
|
||||
});
|
||||
assert.deepStrictEqual(keys.next(), {
|
||||
value: undefined,
|
||||
done: true
|
||||
});
|
||||
assert.deepStrictEqual(keys.next(), {
|
||||
value: undefined,
|
||||
done: true
|
||||
});
|
||||
|
||||
assert.throws(() => {
|
||||
keys.next.call(undefined);
|
||||
}, /^TypeError: Value of `this` is not a URLSearchParamsIterator$/);
|
||||
assert.throws(() => {
|
||||
params.keys.call(undefined);
|
||||
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
|
@ -32,3 +32,10 @@ assert.strictEqual(true, params.has('a'),
|
||||
'Search params object has name "a"');
|
||||
assert.strictEqual(params.get('a'), '4',
|
||||
'Search params object has name "a" with value "4"');
|
||||
|
||||
assert.throws(() => {
|
||||
params.set.call(undefined);
|
||||
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
|
||||
assert.throws(() => {
|
||||
params.set('a');
|
||||
}, /^TypeError: "name" and "value" arguments must be specified$/);
|
||||
|
@ -110,3 +110,6 @@ assert.strictEqual(params + '', 'a%F0%9F%92%A9b=c');
|
||||
// The lone '=' _does_ survive the roundtrip.
|
||||
params = new URLSearchParams('a=&a=b');
|
||||
assert.strictEqual(params.toString(), 'a=&a=b');
|
||||
assert.throws(() => {
|
||||
params.toString.call(undefined);
|
||||
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
|
||||
|
34
test/parallel/test-whatwg-url-searchparams-values.js
Normal file
34
test/parallel/test-whatwg-url-searchparams-values.js
Normal file
@ -0,0 +1,34 @@
|
||||
'use strict';
|
||||
|
||||
require('../common');
|
||||
const assert = require('assert');
|
||||
const URLSearchParams = require('url').URLSearchParams;
|
||||
|
||||
const params = new URLSearchParams('a=b&c=d');
|
||||
const values = params.values();
|
||||
|
||||
assert.strictEqual(typeof values[Symbol.iterator], 'function');
|
||||
assert.strictEqual(values[Symbol.iterator](), values);
|
||||
assert.deepStrictEqual(values.next(), {
|
||||
value: 'b',
|
||||
done: false
|
||||
});
|
||||
assert.deepStrictEqual(values.next(), {
|
||||
value: 'd',
|
||||
done: false
|
||||
});
|
||||
assert.deepStrictEqual(values.next(), {
|
||||
value: undefined,
|
||||
done: true
|
||||
});
|
||||
assert.deepStrictEqual(values.next(), {
|
||||
value: undefined,
|
||||
done: true
|
||||
});
|
||||
|
||||
assert.throws(() => {
|
||||
values.next.call(undefined);
|
||||
}, /^TypeError: Value of `this` is not a URLSearchParamsIterator$/);
|
||||
assert.throws(() => {
|
||||
params.values.call(undefined);
|
||||
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
|
Loading…
x
Reference in New Issue
Block a user