-
-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathquerystring.test.js
More file actions
40 lines (33 loc) · 1.23 KB
/
querystring.test.js
File metadata and controls
40 lines (33 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// In the prep, we implemented a function to parse query strings.
// Unfortunately, it contains several bugs!
// Below is one test case for an edge case the implementation doesn't handle well.
// Fix the implementation for this test, and try to think of as many other edge cases as possible - write tests and fix those too.
const parseQueryString = require("./querystring.js")
test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
"equation": "x=y+1",
});
});
// Duplicate keys, last one wins
test('duplicate keys overwrite previous', () => {
expect(parseQueryString('color=blue&color=red')).toEqual({
color: 'red'
});
});
// Empty string input
test('returns empty object for empty string', () => {
expect(parseQueryString('')).toEqual({});
});
// Null/invalid input
test('returns empty object for null or non-string', () => {
expect(parseQueryString(null)).toEqual({});
expect(parseQueryString(123)).toEqual({});
});
// Missing value
test('handles keys with empty values', () => {
expect(parseQueryString('empty=')).toEqual({ empty: '' });
});
// Missing key
test('ignores pairs without keys', () => {
expect(parseQueryString('=novalue')).toEqual({ '': 'novalue' });
});