Repository navigation
Expand file tree
/
Copy pathquerystring.test.js
More file actions
73 lines (54 loc) · 1.89 KB
/
Copy pathquerystring.test.js
File metadata and controls
73 lines (54 loc) · 1.89 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// In the prep, we implemented a function to parse query strings.
// Unfortunately, it contains several bugs!
// Below are some test cases the implementation doesn't handle well.
// Fix the implementation for these tests, and try to think of as many other edge cases as possible - write tests and fix those too.
const parseQueryString = require("./querystring.js");
test("should parse values containing '='", () => {
expect(parseQueryString("equation=a=b-2")).toEqual({
equation: "a=b-2",
});
});
test("should ignore empty key-value pairs", () => {
const input = "key1=value1&&key2=value2&";
const currentOutput = parseQueryString(input);
const targetOutput = {
key1: "value1",
key2: "value2",
};
expect(currentOutput).toEqual(targetOutput);
});
test("should accept empty string as key or as value", () => {
expect(parseQueryString("=value")).toEqual({
"": "value",
});
expect(parseQueryString("key=")).toEqual({
key: "",
});
expect(parseQueryString("=")).toEqual({
"": "",
});
});
test("should decode percent-encoded characters", () => {
expect(parseQueryString("%24half=1%2F2")).toEqual({
$half: "1/2",
});
});
test("should replace '+' by ' '", () => {
const input = "full+name=John+Doe";
const currentOutput = parseQueryString(input);
const expectedOutput = {
"full name": "John Doe",
};
expect(currentOutput).toEqual(expectedOutput);
});
// Stretch exercise: Handling query strings that contain identical keys
// Delete this test if you are not working on this optional case
// test("should store values of a key in an array when the key has 2 or more values", () => {
// const input = "key=value1&key=value2&key=value3&foo=bar";
// const currentOutput = parseQueryString(input);
// const expectedOutput = {
// key: ["value1", "value2", "value3"],
// foo: "bar",
// };
// expect(currentOutput).toEqual(expectedOutput);
// });