|
1 | | -// In the prep, we implemented a function to parse query strings. |
2 | | -// 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. |
| 1 | +const parseQueryString = require("./querystring.js"); |
5 | 2 |
|
6 | | -const parseQueryString = require("./querystring.js") |
| 3 | +describe("parseQueryString()", () => { |
| 4 | + test("parses querystring values containing =", () => { |
| 5 | + expect(parseQueryString("equation=x=y+1")).toEqual({ |
| 6 | + equation: "x=y+1", |
| 7 | + }); |
| 8 | + }); |
| 9 | + |
| 10 | + test("returns an empty object for an empty string", () => { |
| 11 | + expect(parseQueryString("")).toEqual({}); |
| 12 | + }); |
| 13 | + |
| 14 | + test("parses a single key-value pair", () => { |
| 15 | + expect(parseQueryString("name=Richard")).toEqual({ |
| 16 | + name: "Richard", |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + test("parses multiple key-value pairs", () => { |
| 21 | + expect(parseQueryString("name=Richard&city=Sheffield")).toEqual({ |
| 22 | + name: "Richard", |
| 23 | + city: "Sheffield", |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + test("handles a key with an empty value", () => { |
| 28 | + expect(parseQueryString("name=")).toEqual({ |
| 29 | + name: "", |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + test("handles a key with no equals sign", () => { |
| 34 | + expect(parseQueryString("name")).toEqual({ |
| 35 | + name: "", |
| 36 | + }); |
| 37 | + }); |
7 | 38 |
|
8 | | -test("parses querystring values containing =", () => { |
9 | | - expect(parseQueryString("equation=x=y+1")).toEqual({ |
10 | | - "equation": "x=y+1", |
| 39 | + test("ignores an empty trailing pair", () => { |
| 40 | + expect(parseQueryString("name=Richard&")).toEqual({ |
| 41 | + name: "Richard", |
| 42 | + }); |
11 | 43 | }); |
12 | 44 | }); |
0 commit comments