Skip to content

Commit

Permalink
test: add tests for searchParams
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
hiroppy authored and evanlucas committed Jan 31, 2017
1 parent c329843 commit fcd08b8
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 1 deletion.
7 changes: 7 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-append.js
Original file line number Diff line number Diff line change
Expand Up @@ -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$/);
7 changes: 7 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
33 changes: 33 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-entries.js
Original file line number Diff line number Diff line change
@@ -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$/);
6 changes: 5 additions & 1 deletion test/parallel/test-whatwg-url-searchparams-foreach.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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$/);
7 changes: 7 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-get.js
Original file line number Diff line number Diff line change
Expand Up @@ -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$/);
7 changes: 7 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-getall.js
Original file line number Diff line number Diff line change
Expand Up @@ -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$/);
7 changes: 7 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-has.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 34 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-keys.js
Original file line number Diff line number Diff line change
@@ -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$/);
7 changes: 7 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -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$/);
3 changes: 3 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-stringifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 34 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-values.js
Original file line number Diff line number Diff line change
@@ -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$/);

0 comments on commit fcd08b8

Please sign in to comment.