-
-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathquerystring.test.js
More file actions
64 lines (54 loc) · 1.75 KB
/
querystring.test.js
File metadata and controls
64 lines (54 loc) · 1.75 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
// 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 multiple query parameters", () => {
expect(parseQueryString("genre=mystery&year=1947")).toEqual({
genre: "mystery",
year: "1947",
});
});
test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
equation: "x=y+1",
});
});
test("returns an empty object for an empty string", () => {
expect(parseQueryString("")).toEqual({});
});
test("handles keys with empty values", () => {
expect(parseQueryString("name=&position=developer")).toEqual({
name: "",
position: "developer",
});
});
test("handles a single key with an empty value", () => {
expect(parseQueryString("key=")).toEqual({
key: "",
});
});
test("treats parameters without = as empty values", () => {
expect(parseQueryString("name&position=developer")).toEqual({
name: "",
position: "developer",
});
});
test("handles parameters with a missing key", () => {
expect(parseQueryString("=value")).toEqual({
"": "value",
});
});
test("ignores empty parameters created by consecutive ampersands", () => {
expect(parseQueryString("key1=value1&&key2=value2")).toEqual({
key1: "value1",
key2: "value2",
});
});
test("handles an empty key with an empty value between valid parameters", () => {
expect(parseQueryString("key1=value1&=&key2=value2")).toEqual({
key1: "value1",
"": "",
key2: "value2",
});
});