Skip to content

Commit 2e1f45a

Browse files
committed
Add comprehensive test coverage for queryString.exclude()
Fixes #402
1 parent dc13d74 commit 2e1f45a

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

test/exclude.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,68 @@ test('excludes elements in a URL with a filter predicate', t => {
1919
test('excludes elements in a URL without encoding fragment identifiers', t => {
2020
t.is(queryString.exclude('https://example.com?a=b#/home', ['a']), 'https://example.com#/home');
2121
});
22+
23+
test('handles empty filter array', t => {
24+
t.is(queryString.exclude('http://example.com/?a=1&b=2&c=3', []), 'http://example.com/?a=1&b=2&c=3');
25+
});
26+
27+
test('handles excluding non-existent parameters', t => {
28+
t.is(queryString.exclude('http://example.com/?a=1&b=2', ['c', 'd']), 'http://example.com/?a=1&b=2');
29+
});
30+
31+
test('handles multiple values for same parameter', t => {
32+
t.is(queryString.exclude('http://example.com/?a=1&a=2&b=3', ['b']), 'http://example.com/?a=1&a=2');
33+
});
34+
35+
test('handles special characters in parameter names', t => {
36+
t.is(queryString.exclude('http://example.com/?foo%5Bbar%5D=1&normal=2', ['foo[bar]']), 'http://example.com/?normal=2');
37+
});
38+
39+
test('handles URL without query parameters', t => {
40+
t.is(queryString.exclude('http://example.com/', ['a']), 'http://example.com/');
41+
t.is(queryString.exclude('http://example.com/#hash', ['a']), 'http://example.com/#hash');
42+
});
43+
44+
test('excludes all parameters when filter matches everything', t => {
45+
t.is(queryString.exclude('http://example.com/?a=1&b=2&c=3', ['a', 'b', 'c']), 'http://example.com/');
46+
});
47+
48+
test('preserves URL path when excluding parameters', t => {
49+
t.is(queryString.exclude('http://example.com/path/to/page?a=1&b=2', ['a']), 'http://example.com/path/to/page?b=2');
50+
});
51+
52+
test('handles empty string values', t => {
53+
t.is(queryString.exclude('http://example.com/?a=&b=2&c=', ['b']), 'http://example.com/?a=&c=');
54+
});
55+
56+
test('handles parameters without values', t => {
57+
t.is(queryString.exclude('http://example.com/?a&b=2&c', ['b']), 'http://example.com/?a&c');
58+
});
59+
60+
test('filter predicate receives correct arguments for array values', t => {
61+
const url = 'http://example.com/?a=1&a=2&b=3';
62+
const result = queryString.exclude(url, (name, value) => {
63+
if (name === 'a') {
64+
t.true(Array.isArray(value));
65+
t.deepEqual(value, ['1', '2']);
66+
return true;
67+
}
68+
69+
return false;
70+
});
71+
t.is(result, 'http://example.com/?b=3');
72+
});
73+
74+
test('handles relative URLs', t => {
75+
t.is(queryString.exclude('/path?a=1&b=2', ['a']), '/path?b=2');
76+
t.is(queryString.exclude('?a=1&b=2', ['b']), '?a=1');
77+
});
78+
79+
test('handles fragments with special characters', t => {
80+
t.is(queryString.exclude('http://example.com/?a=1#section/subsection', ['a']), 'http://example.com/#section/subsection');
81+
});
82+
83+
test('handles complex nested parameter names', t => {
84+
const result = queryString.exclude('http://example.com/?user[name]=John&user[age]=30&id=1', ['user[name]']);
85+
t.true(result === 'http://example.com/?user%5Bage%5D=30&id=1' || result === 'http://example.com/?id=1&user%5Bage%5D=30');
86+
});

0 commit comments

Comments
 (0)