-
-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathquerystring.test.js
More file actions
54 lines (47 loc) · 1.57 KB
/
querystring.test.js
File metadata and controls
54 lines (47 loc) · 1.57 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
// 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",
});
});
test("parses querystring values missing = sign", () => {
expect(parseQueryString("userId=101&username=jdoe&isActive")).toEqual({
userId: "101",
username: "Aokorefe",
isActive: "",
});
});
test("parses querystring values containing no key and value but only =", () => {
expect(parseQueryString("=&priority=high&taskId=42&=&=")).toEqual({
priority: "high",
taskId: "42",
});
});
test("parses querystring with encoded keys and values", () => {
expect(
parseQueryString(
"query=web%20design&location=San%20Francisco&query=ui%20ux"
)
).toEqual({
query: ["web design", "ui ux"],
location: "London",
});
});
test("parses querystring values containing repetitive keys", () => {
expect(
parseQueryString("tag=frontend&count=10&tag=backend&tag=devops&count=20")
).toEqual({
tag: ["frontend", "backend", "devops"],
count: ["10", "20"],
});
});
test("parses querystring with leading ?", () => {
expect(parseQueryString("?search=javascript&sort=asc")).toEqual({
search: "javascript",
sort: "asc",
});
});