forked from CodeYourFuture/Module-Data-Groups
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquerystring.test.js
More file actions
49 lines (45 loc) · 1.59 KB
/
Copy pathquerystring.test.js
File metadata and controls
49 lines (45 loc) · 1.59 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
// 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",
});
expect(parseQueryString("page=1")).toEqual({
page: "1",
});
expect(parseQueryString("name=Ali")).toEqual({
name: "Ali",
});
});
test("parses querystring values containing &", () => {
expect(parseQueryString("key1=value1&key2=value2")).toEqual({
key1: "value1",
key2: "value2",
});
expect(parseQueryString("name=Ayub&colour=blue")).toEqual({
name: "Ayub",
colour: "blue",
});
});
test("handles malformed querystrings", () => {
// if value is empty, return empty string for value.
expect(parseQueryString("key=")).toEqual({
key: "",
});
// if key is empty don't return anything.
expect(parseQueryString("=value")).toEqual({});
// if there is a key but no equals, return empty string for value.
expect(parseQueryString("key")).toEqual({ key: "" });
// if key is empty and value is empty, don't return anything for that item
expect(parseQueryString("key1=value1&=&key2=value2")).toEqual({
key1: "value1",
key2: "value2",
});
expect(parseQueryString("key1=value1&&key2=value2")).toEqual({
key1: "value1",
key2: "value2",
});
});