node/test/parallel/test-whatwg-url-searchparams-has.js
Timothy Gu 61d6293033 url: improve URLSearchParams spec compliance
- Make URLSearchParams constructor spec-compliant
- Strip leading `?` in URL#search's setter
- Spec-compliant iterable interface
- More precise handling of update steps as mandated by the spec
- Add class strings to URLSearchParams objects and their prototype
- Make sure `this instanceof URLSearchParams` in methods

Also included are relevant tests from W3C's Web Platform Tests
(https://github.com/w3c/web-platform-tests/tree/master/url).

Fixes: https://github.com/nodejs/node/issues/9302
PR-URL: https://github.com/nodejs/node/pull/9484
Reviewed-By: James M Snell <jasnell@gmail.com>
2016-12-07 12:19:23 -05:00

40 lines
1.3 KiB
JavaScript

'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;
// Has basics
params = new URLSearchParams('a=b&c=d');
assert.strictEqual(true, params.has('a'));
assert.strictEqual(true, params.has('c'));
assert.strictEqual(false, params.has('e'));
params = new URLSearchParams('a=b&c=d&a=e');
assert.strictEqual(true, params.has('a'));
params = new URLSearchParams('=b&c=d');
assert.strictEqual(true, params.has(''));
params = new URLSearchParams('null=a');
assert.strictEqual(true, params.has(null));
// has() following delete()
params = new URLSearchParams('a=b&c=d&&');
params.append('first', 1);
params.append('first', 2);
assert.strictEqual(true, params.has('a'),
'Search params object has name "a"');
assert.strictEqual(true, params.has('c'),
'Search params object has name "c"');
assert.strictEqual(true, params.has('first'),
'Search params object has name "first"');
assert.strictEqual(false, params.has('d'),
'Search params object has no name "d"');
params.delete('first');
assert.strictEqual(false, params.has('first'),
'Search params object has no name "first"');