Skip to content

Commit a4b781b

Browse files
committed
test: added edge case tests for parseQueryString and fixed typo in age value
1 parent 6b31d1d commit a4b781b

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

Sprint-2/implement/querystring.test.js

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,58 @@
33
// Below is one test case for an edge case the implementation doesn't handle well.
44
// Fix the implementation for this test, and try to think of as many other edge cases as possible - write tests and fix those too.
55

6-
const parseQueryString = require("./querystring.js")
6+
const parseQueryString = require("./querystring.js");
77

88
test("parses querystring values containing =", () => {
99
expect(parseQueryString("equation=x=y+1")).toEqual({
10-
"equation": "x=y+1",
10+
equation: "x=y+1",
11+
});
12+
});
13+
14+
test("handles multiple parameters", () => {
15+
expect(parseQueryString("name=Alex&age=25&city=London")).toEqual({
16+
name: "Alex",
17+
age: "25",
18+
city: "London",
19+
});
20+
});
21+
22+
test("handles empty value (key=)", () => {
23+
expect(parseQueryString("search=&page=3")).toEqual({
24+
search: "",
25+
page: "3",
26+
});
27+
});
28+
29+
test("handles key without value", () => {
30+
expect(parseQueryString("debug&sort=asc")).toEqual({
31+
debug: "",
32+
sort: "asc",
33+
});
34+
});
35+
36+
test("handles leading question mark", () => {
37+
expect(parseQueryString("?q=javascript&lang=en")).toEqual({
38+
q: "javascript",
39+
lang: "en",
40+
});
41+
});
42+
43+
test("returns empty object when input is empty or just ?", () => {
44+
expect(parseQueryString("")).toEqual({});
45+
expect(parseQueryString("?")).toEqual({});
46+
});
47+
48+
test("ignores trailing &", () => {
49+
expect(parseQueryString("a=1&b=2&")).toEqual({
50+
a: "1",
51+
b: "2",
52+
});
53+
});
54+
55+
test("ignores repeated &", () => {
56+
expect(parseQueryString("x=10&&y=20")).toEqual({
57+
x: "10",
58+
y: "20",
1159
});
1260
});

0 commit comments

Comments
 (0)