|
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 | +// testing when the query string with a value contains = signs |
8 | 9 | test("parses querystring values containing =", () => { |
9 | 10 | expect(parseQueryString("equation=x=y+1")).toEqual({ |
10 | | - "equation": "x=y+1", |
| 11 | + equation: "x=y+1", |
| 12 | + }); |
| 13 | +}); |
| 14 | + |
| 15 | +// testing when the query string has multiple ampersands |
| 16 | +test("parses query string with multiple parameters", () => { |
| 17 | + expect(parseQueryString("name=Ben&age=45&city=new%20city")).toEqual({ |
| 18 | + name: "Ben", |
| 19 | + age: "45", |
| 20 | + city: "new%20city", |
| 21 | + }); |
| 22 | +}); |
| 23 | + |
| 24 | +// testing when the query string has two ampersands in a row |
| 25 | +test("parses query string with two ampersands in a row", () => { |
| 26 | + expect(parseQueryString("name=Ben&&age=45")).toEqual({ |
| 27 | + name: "Ben", |
| 28 | + "": "", |
| 29 | + age: "45", |
| 30 | + }); |
| 31 | +}); |
| 32 | + |
| 33 | +// testing when the query string has no ampersands |
| 34 | +test("parses query string with no ampersands", () => { |
| 35 | + expect(parseQueryString("name=Ben")).toEqual({ |
| 36 | + name: "Ben", |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +// testing when the query string has no ampersands and no equal signs |
| 41 | +test("parses query string with no ampersands and no equal signs", () => { |
| 42 | + expect(parseQueryString("name")).toEqual({ |
| 43 | + name: "", |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +// testing when the query string is empty |
| 48 | +test("parses query string with empty value", () => { |
| 49 | + expect(parseQueryString("")).toEqual({}); |
| 50 | +}); |
| 51 | + |
| 52 | +// testing when the query string is null |
| 53 | +test("parses query string with null value", () => { |
| 54 | + expect(parseQueryString(null)).toEqual("invalid query string"); |
| 55 | +}); |
| 56 | + |
| 57 | +// testing when the query string is undefined |
| 58 | +test("parses query string with undefined value", () => { |
| 59 | + expect(parseQueryString(undefined)).toEqual("invalid query string"); |
| 60 | +}); |
| 61 | + |
| 62 | +// testing when a pair ends with equal sign (no value) |
| 63 | +test("parses query string with no equal sign", () => { |
| 64 | + expect(parseQueryString("name=Ben&age=&city=")).toEqual({ |
| 65 | + name: "Ben", |
| 66 | + age: "", |
| 67 | + city: "", |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +// testing when a pair starts with equal sign (no key) |
| 72 | +test("parses query string starting with equal sign", () => { |
| 73 | + expect(parseQueryString("name=Ben&=45")).toEqual({ |
| 74 | + name: "Ben", |
| 75 | + "": "45", |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +// testing when a pair has no equal sign (no value and no key) |
| 80 | +// the pair will be the key and the value will be an empty string |
| 81 | +test("parses query string with no equal sign", () => { |
| 82 | + expect(parseQueryString("name&age=45&city")).toEqual({ |
| 83 | + name: "", |
| 84 | + age: "45", |
| 85 | + city: "", |
11 | 86 | }); |
12 | 87 | }); |
0 commit comments