|
3 | 3 | // Below is one test case for an edge case the implementation doesn't handle well. |
4 | 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. |
5 | 5 |
|
6 | | -const parseQueryString = require("./querystring.js") |
| 6 | +const parseQueryString = require("./querystring.js"); |
7 | 7 |
|
8 | 8 | test("parses querystring values containing =", () => { |
9 | 9 | 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", |
11 | 59 | }); |
12 | 60 | }); |
0 commit comments