-
-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathquerystring.test.js
More file actions
66 lines (56 loc) · 2.2 KB
/
querystring.test.js
File metadata and controls
66 lines (56 loc) · 2.2 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
// 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");
//Case 1 : equal sign within the value of query string
test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
equation: "x=y+1",
});
});
//Case 2 : multiple params in the query string
test("parses querystring containing multiple params", () => {
expect(parseQueryString("equation=x=y+1&job=student")).toEqual({
equation: "x=y+1",
job: "student",
});
});
//Case 3 : empty or missing value in the param
test("parses querystring containing empty or missing value in one of the params", () => {
expect(parseQueryString("equation=x=y+1&job=")).toEqual({
equation: "x=y+1",
job: "",
});
});
//Case 4 : extra & in the start, mid or at the end
test("parses querystring containing extra & in the start, mid or at the end", () => {
expect(parseQueryString("&equation=x=y+1&&job=&")).toEqual({
equation: "x=y+1",
job: "",
});
expect(parseQueryString("a=b&&c=d")).toEqual({
a: "b",
c: "d",
});
});
//Case 4 : querystring is empty
test("an empty query string should return an empty object", () => {
expect(parseQueryString("")).toEqual({});
});
//Case 5 : querystring is null
test("if the querystring is null then an error should be thrown", () => {
expect(() => parseQueryString(null)).toThrow("Invalid Input");
});
//Case 6 : querystring is undefined
test("if the querystring is undefined then an error should be thrown", () => {
expect(() => parseQueryString(undefined)).toThrow("Invalid Input");
});
//Case 7 : querystring is a number
test("if the querystring is number then an error should be thrown", () => {
expect(() => parseQueryString(123)).toThrow("Invalid Input");
});
//Case 8 : querystring is an object
test("if the querystring is an object then an error should be thrown", () => {
expect(() => parseQueryString({})).toThrow("Invalid Input");
});