|
3 | 3 | joinPaths,
|
4 | 4 | resolvePath,
|
5 | 5 | createMemoObject,
|
6 |
| - expandOptionals |
| 6 | + expandOptionals, |
| 7 | + mergeSearchString, |
| 8 | + extractSearchParams |
7 | 9 | } from "../src/utils";
|
8 | 10 |
|
9 | 11 | describe("resolvePath should", () => {
|
@@ -86,6 +88,85 @@ describe("resolvePath should", () => {
|
86 | 88 | });
|
87 | 89 | });
|
88 | 90 |
|
| 91 | +describe("mergeSearchString should", () => { |
| 92 | + test("return empty string when current and new params are empty", () => { |
| 93 | + const expected = ""; |
| 94 | + const actual = mergeSearchString("", {}); |
| 95 | + expect(actual).toBe(expected); |
| 96 | + }); |
| 97 | + |
| 98 | + test("return new params when current params are empty", () => { |
| 99 | + const expected = "?foo=bar"; |
| 100 | + const actual = mergeSearchString("", { foo: "bar" }); |
| 101 | + expect(actual).toBe(expected); |
| 102 | + }); |
| 103 | + |
| 104 | + test("return current params when new params are empty", () => { |
| 105 | + const expected = "?foo=bar"; |
| 106 | + const actual = mergeSearchString("?foo=bar", {}); |
| 107 | + expect(actual).toBe(expected); |
| 108 | + }); |
| 109 | + |
| 110 | + test("return merged params when current and new params are not empty", () => { |
| 111 | + const expected = "?foo=bar&baz=qux"; |
| 112 | + const actual = mergeSearchString("?foo=bar", { baz: "qux" }); |
| 113 | + expect(actual).toBe(expected); |
| 114 | + }); |
| 115 | + |
| 116 | + test("return ampersand-separated params when new params is an array", () => { |
| 117 | + const expected = "?foo=bar&foo=baz"; |
| 118 | + const actual = mergeSearchString("", { foo: ["bar", "baz"] }); |
| 119 | + expect(actual).toBe(expected); |
| 120 | + }); |
| 121 | + |
| 122 | + test("return ampersand-separated params when current params is an array of numbers", () => { |
| 123 | + const expected = "?foo=1&foo=2"; |
| 124 | + const actual = mergeSearchString("", { foo: [1, 2] }); |
| 125 | + expect(actual).toBe(expected); |
| 126 | + }); |
| 127 | + |
| 128 | + test("return empty string when new is an empty array", () => { |
| 129 | + const expected = ""; |
| 130 | + const actual = mergeSearchString("", { foo: [] }); |
| 131 | + expect(actual).toBe(expected); |
| 132 | + }); |
| 133 | + |
| 134 | + test("return empty string when current is present and new is an empty array", () => { |
| 135 | + const expected = ""; |
| 136 | + const actual = mergeSearchString("?foo=2&foo=3", { foo: [] }); |
| 137 | + expect(actual).toBe(expected); |
| 138 | + }); |
| 139 | + |
| 140 | + test("return array containing only new value when current is present and new is an array with one value", () => { |
| 141 | + const expected = "?foo=1&foo=2"; |
| 142 | + const actual = mergeSearchString("?foo=3&foo=4", { foo: [1, 2] }); |
| 143 | + expect(actual).toBe(expected); |
| 144 | + }); |
| 145 | +}); |
| 146 | + |
| 147 | +describe("extractSearchParams should", () => { |
| 148 | + test("return empty object when URL has no search params", () => { |
| 149 | + const url = new URL("http://localhost/"); |
| 150 | + const expected = {}; |
| 151 | + const actual = extractSearchParams(url); |
| 152 | + expect(actual).toEqual(expected); |
| 153 | + }); |
| 154 | + |
| 155 | + test("return search params as object", () => { |
| 156 | + const url = new URL("http://localhost/?foo=bar&baz=qux"); |
| 157 | + const expected = { foo: "bar", baz: "qux" }; |
| 158 | + const actual = extractSearchParams(url); |
| 159 | + expect(actual).toEqual(expected); |
| 160 | + }); |
| 161 | + |
| 162 | + test("return search params as object with array values", () => { |
| 163 | + const url = new URL("http://localhost/?foo=bar&foo=baz"); |
| 164 | + const expected = { foo: ["bar", "baz"] }; |
| 165 | + const actual = extractSearchParams(url); |
| 166 | + expect(actual).toEqual(expected); |
| 167 | + }); |
| 168 | +}); |
| 169 | + |
89 | 170 | describe("createMatcher should", () => {
|
90 | 171 | test("return empty object when location matches simple path", () => {
|
91 | 172 | const expected = { path: "/foo/bar", params: {} };
|
|
0 commit comments