Skip to content

Commit fbeb4ba

Browse files
authored
Update querystring.test.js to clarify spec
1 parent 10b3ac9 commit fbeb4ba

1 file changed

Lines changed: 41 additions & 5 deletions

File tree

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,48 @@
11
// In the prep, we implemented a function to parse query strings.
22
// Unfortunately, it contains several bugs!
3-
// Below is one test case for an edge case the implementation doesn't handle well.
4-
// Fix the implementation for this test, and try to think of as many other edge cases as possible - write tests and fix those too.
3+
// Below are some test cases the implementation doesn't handle well.
4+
// Fix the implementation for these tests, and try to think of as many other edge cases as possible - write tests and fix those too.
55

66
const parseQueryString = require("./querystring.js")
77

8-
test("parses querystring values containing =", () => {
9-
expect(parseQueryString("equation=x=y+1")).toEqual({
10-
"equation": "x=y+1",
8+
test("should parse values containing '='", () => {
9+
expect(parseQueryString("equation=a=b-2")).toEqual({
10+
equation: "a=b-2",
11+
});
12+
});
13+
14+
test("should ignore empty key-value pairs", () => {
15+
expect(parseQueryString("key1=value1&&key2=value2&")).toEqual({
16+
key1: "value1",
17+
key2: "value2",
18+
});
19+
});
20+
21+
test("should accept empty string as key or as value", () => {
22+
expect(parseQueryString("=value")).toEqual({ "": "value" });
23+
expect(parseQueryString("key")).toEqual({ key: "" });
24+
expect(parseQueryString("key=")).toEqual({ key: "" });
25+
expect(parseQueryString("=")).toEqual({ "": "" });
26+
});
27+
28+
test("should decode percent-encoded characters", () => {
29+
expect(parseQueryString("%24half=1%2F2")).toEqual({
30+
$half: "1/2",
31+
});
32+
});
33+
34+
test("should replace '+' by ' '", () => {
35+
expect(parseQueryString("full+name=John+Doe")).toEqual({
36+
"full name": "John Doe",
37+
});
38+
});
39+
40+
// Stretch exercise: Handling query strings that contain identical keys
41+
42+
// Delete this test if you are not working on this optional case
43+
test("should store values of a key in an array when the key has 2 or more values", () => {
44+
expect(parseQueryString("key=value1&key=value2&key=value3&foo=bar")).toEqual({
45+
key: ["value1", "value2", "value3"],
46+
foo: "bar",
1147
});
1248
});

0 commit comments

Comments
 (0)