| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
import cockpit from "cockpit"; |
| 3 |
import QUnit from "qunit-tests"; |
| 4 |
|
| 5 |
QUnit.test("basic", function (assert) { |
| 6 |
window.location.hash = ""; |
| 7 |
|
| 8 |
assert.equal(typeof cockpit.location, "object", "cockpit.location exists"); |
| 9 |
assert.ok(Array.isArray(cockpit.location.path), "cockpit.location.path exists"); |
| 10 |
assert.equal(typeof cockpit.location.options, "object", "cockpit.location.options exists"); |
| 11 |
assert.equal(typeof cockpit.location.go, "function", "cockpit.location.go exists"); |
| 12 |
assert.equal(typeof cockpit.location.replace, "function", "cockpit.location.replace exists"); |
| 13 |
assert.equal(typeof cockpit.location.decode, "function", "cockpit.location.decode exists"); |
| 14 |
assert.equal(typeof cockpit.location.encode, "function", "cockpit.location.encode exists"); |
| 15 |
|
| 16 |
assert.deepEqual(cockpit.location.path, [], "path is empty"); |
| 17 |
assert.deepEqual(cockpit.location.options, { }, "options are empty"); |
| 18 |
}); |
| 19 |
|
| 20 |
QUnit.test("decode", function (assert) { |
| 21 |
window.location.hash = "#/base/test"; |
| 22 |
|
| 23 |
const checks = [ |
| 24 |
["#/host/path/sub?a=1&b=2", { |
| 25 |
path: ["host", "path", "sub"], |
| 26 |
options: { a: "1", b: "2" } |
| 27 |
} |
| 28 |
], |
| 29 |
["", { |
| 30 |
path: [], |
| 31 |
options: { } |
| 32 |
} |
| 33 |
], |
| 34 |
["#", { |
| 35 |
path: [], |
| 36 |
options: { } |
| 37 |
} |
| 38 |
], |
| 39 |
["#/", { |
| 40 |
path: [], |
| 41 |
options: { } |
| 42 |
} |
| 43 |
], |
| 44 |
["/horst", { |
| 45 |
path: ["horst"], |
| 46 |
options: { } |
| 47 |
} |
| 48 |
], |
| 49 |
["//one", { |
| 50 |
path: ["one"], |
| 51 |
options: { } |
| 52 |
} |
| 53 |
], |
| 54 |
["//one/", { |
| 55 |
path: ["one"], |
| 56 |
options: { } |
| 57 |
} |
| 58 |
], |
| 59 |
["///two", { |
| 60 |
path: ["two"], |
| 61 |
options: { } |
| 62 |
} |
| 63 |
], |
| 64 |
["/slash/%2f", { |
| 65 |
path: ["slash", "/"], |
| 66 |
options: { } |
| 67 |
} |
| 68 |
], |
| 69 |
["?a=1", { |
| 70 |
path: [], |
| 71 |
options: { a: "1" } |
| 72 |
} |
| 73 |
], |
| 74 |
["?a=1&a=2", { |
| 75 |
path: [], |
| 76 |
options: { a: ["1", "2"] } |
| 77 |
} |
| 78 |
], |
| 79 |
["?%3f=%3d", { |
| 80 |
path: [], |
| 81 |
options: { "?": "=" } |
| 82 |
} |
| 83 |
], |
| 84 |
["#?=", { |
| 85 |
path: [], |
| 86 |
options: { "": "" } |
| 87 |
} |
| 88 |
], |
| 89 |
["?=", { |
| 90 |
path: [], |
| 91 |
options: { "": "" } |
| 92 |
} |
| 93 |
], |
| 94 |
["relative/sub", { |
| 95 |
path: ["base", "relative", "sub"], |
| 96 |
options: { } |
| 97 |
} |
| 98 |
], |
| 99 |
["./relative/sub", { |
| 100 |
path: ["base", "relative", "sub"], |
| 101 |
options: { } |
| 102 |
} |
| 103 |
], |
| 104 |
["../relative/sub", { |
| 105 |
path: ["relative", "sub"], |
| 106 |
options: { } |
| 107 |
} |
| 108 |
], |
| 109 |
["/top/../sub", { |
| 110 |
path: ["sub"], |
| 111 |
options: { } |
| 112 |
} |
| 113 |
], |
| 114 |
["/top/./sub/./", { |
| 115 |
path: ["top", "sub"], |
| 116 |
options: { } |
| 117 |
} |
| 118 |
], |
| 119 |
["relative/../sub", { |
| 120 |
path: ["base", "sub"], |
| 121 |
options: { } |
| 122 |
} |
| 123 |
] |
| 124 |
]; |
| 125 |
|
| 126 |
assert.expect(checks.length); |
| 127 |
for (let i = 0; i < checks.length; i++) { |
| 128 |
const options = { }; |
| 129 |
const path = cockpit.location.decode(checks[i][0], options); |
| 130 |
assert.deepEqual({ path, options }, checks[i][1], "decode(\"" + checks[i][0] + "\")"); |
| 131 |
} |
| 132 |
}); |
| 133 |
|
| 134 |
QUnit.test("encode", function (assert) { |
| 135 |
/* We don't check options here since we can't predict the order in |
| 136 |
which they appear in the hash. Encoding of options is covered |
| 137 |
in the "roundtrip" test. |
| 138 |
*/ |
| 139 |
const checks = [ |
| 140 |
["/host/path/sub?a=1&b=2", { |
| 141 |
path: ["host", "path", "sub"], |
| 142 |
options: { a: "1", b: "2" } |
| 143 |
} |
| 144 |
], |
| 145 |
["/one", { |
| 146 |
path: ["one"], |
| 147 |
options: { } |
| 148 |
} |
| 149 |
], |
| 150 |
["/one/two", { |
| 151 |
path: ["one", "two"], |
| 152 |
options: { } |
| 153 |
} |
| 154 |
], |
| 155 |
["/slash/%2F", { |
| 156 |
path: ["slash", "/"], |
| 157 |
options: { } |
| 158 |
} |
| 159 |
], |
| 160 |
["/p?a=1", { |
| 161 |
path: ["p"], |
| 162 |
options: { a: "1" } |
| 163 |
} |
| 164 |
], |
| 165 |
["/p?%3F=%3D", { |
| 166 |
path: ["p"], |
| 167 |
options: { "?": "=" } |
| 168 |
} |
| 169 |
], |
| 170 |
["/p?=", { |
| 171 |
path: ["p"], |
| 172 |
options: { "": "" } |
| 173 |
} |
| 174 |
], |
| 175 |
["/p?value=one&value=two", { |
| 176 |
path: ["p"], |
| 177 |
options: { value: ["one", "two"] } |
| 178 |
}] |
| 179 |
]; |
| 180 |
|
| 181 |
assert.expect(checks.length); |
| 182 |
for (let i = 0; i < checks.length; i++) { |
| 183 |
const encoded = cockpit.location.encode(checks[i][1].path, checks[i][1].options); |
| 184 |
assert.strictEqual(encoded, checks[i][0], "encode(" + JSON.stringify(checks[i][1]) + ")"); |
| 185 |
} |
| 186 |
}); |
| 187 |
|
| 188 |
QUnit.test("roundtrip", function (assert) { |
| 189 |
const checks = [ |
| 190 |
{ |
| 191 |
path: ["path", "sub"], |
| 192 |
options: { a: "1", b: "2" } |
| 193 |
}, |
| 194 |
{ |
| 195 |
path: ["päth", "süb"], |
| 196 |
options: { a: "1", b: "2" } |
| 197 |
}, |
| 198 |
{ |
| 199 |
path: ["/=()?", "$%&/"], |
| 200 |
options: { "": "=$&%", b: "=2%34" } |
| 201 |
}, |
| 202 |
]; |
| 203 |
|
| 204 |
assert.expect(checks.length); |
| 205 |
for (let i = 0; i < checks.length; i++) { |
| 206 |
const encoded = cockpit.location.encode(checks[i].path, checks[i].options); |
| 207 |
const decoded = { options: { } }; |
| 208 |
decoded.path = cockpit.location.decode(encoded, decoded.options); |
| 209 |
assert.deepEqual(decoded, checks[i], "roundtrip(" + JSON.stringify(checks[i]) + ")"); |
| 210 |
} |
| 211 |
}); |
| 212 |
|
| 213 |
QUnit.test("external change", function (assert) { |
| 214 |
const location = cockpit.location; |
| 215 |
|
| 216 |
window.location.hash = "#/a/b/c?x=1&y=2"; |
| 217 |
|
| 218 |
assert.notStrictEqual(cockpit.location, location, "cockpit.location is different object"); |
| 219 |
assert.deepEqual(cockpit.location.path, ["a", "b", "c"], "path is correct"); |
| 220 |
assert.strictEqual(cockpit.location.options.x, "1", "option x is correct"); |
| 221 |
assert.strictEqual(cockpit.location.options.y, "2", "option y is correct"); |
| 222 |
}); |
| 223 |
|
| 224 |
QUnit.test("internal change", function (assert) { |
| 225 |
cockpit.location.go(["x", "y", "z"]); |
| 226 |
|
| 227 |
assert.strictEqual(window.location.hash, "#/x/y/z", "hash is correct"); |
| 228 |
assert.deepEqual(cockpit.location.path, ["x", "y", "z"], "path is correct"); |
| 229 |
assert.deepEqual(cockpit.location.options, { }, "options are empty"); |
| 230 |
}); |
| 231 |
|
| 232 |
QUnit.test("string change", function (assert) { |
| 233 |
cockpit.location = "/p/x/../q/r?a=b"; |
| 234 |
|
| 235 |
assert.strictEqual(window.location.hash, "#/p/q/r?a=b", "hash is correct"); |
| 236 |
assert.deepEqual(cockpit.location.path, ["p", "q", "r"], "path is correct"); |
| 237 |
assert.deepEqual(cockpit.location.options, { a: "b" }, "options are empty"); |
| 238 |
}); |
| 239 |
|
| 240 |
QUnit.test("string change", function (assert) { |
| 241 |
window.location.href = "#/top/file"; |
| 242 |
cockpit.location = "another"; |
| 243 |
|
| 244 |
assert.strictEqual(window.location.hash, "#/top/another", "hash is correct"); |
| 245 |
assert.deepEqual(cockpit.location.path, ["top", "another"], "path is correct"); |
| 246 |
}); |
| 247 |
|
| 248 |
QUnit.test("change options", function (assert) { |
| 249 |
window.location.hash = ""; |
| 250 |
assert.deepEqual(cockpit.location.path, [], "path is empty"); |
| 251 |
assert.deepEqual(cockpit.location.options, { }, "options are empty"); |
| 252 |
|
| 253 |
cockpit.location.go(cockpit.location.path, { x: "1" }); |
| 254 |
assert.deepEqual(cockpit.location.options, { x: "1" }, "options are correct"); |
| 255 |
assert.strictEqual(window.location.hash, "#/?x=1", "hash is correct"); |
| 256 |
|
| 257 |
cockpit.location.go(cockpit.location.path); |
| 258 |
assert.deepEqual(cockpit.location.options, { }, "options are empty"); |
| 259 |
assert.strictEqual(window.location.hash, "#/", "hash is correct"); |
| 260 |
}); |
| 261 |
|
| 262 |
QUnit.test("test", function (assert) { |
| 263 |
const done = assert.async(); |
| 264 |
window.location.hash = "#/hello"; |
| 265 |
|
| 266 |
let triggered = false; |
| 267 |
|
| 268 |
assert.deepEqual(cockpit.location.path, ["hello"], "path is right"); |
| 269 |
const onLocationChanged = () => { |
| 270 |
assert.strictEqual(window.location.hash, "#/gonna-happen", "hash has changed"); |
| 271 |
cockpit.removeEventListener("locationchanged", onLocationChanged); |
| 272 |
triggered = true; |
| 273 |
done(); |
| 274 |
}; |
| 275 |
cockpit.addEventListener("locationchanged", onLocationChanged); |
| 276 |
|
| 277 |
cockpit.location.go(["gonna-happen"]); |
| 278 |
assert.ok(!triggered, "not yet triggered"); |
| 279 |
}); |
| 280 |
|
| 281 |
QUnit.test("test", function (assert) { |
| 282 |
const done = assert.async(); |
| 283 |
window.location.hash = "#/hello"; |
| 284 |
|
| 285 |
const location = cockpit.location; |
| 286 |
assert.deepEqual(cockpit.location.path, ["hello"], "path is right"); |
| 287 |
|
| 288 |
window.setTimeout(function() { |
| 289 |
/* window.location.hash has changed so the old `location` object is no longer valid and .go/replace are ignored */ |
| 290 |
location.go(["not-gonna-happen"]); |
| 291 |
assert.strictEqual(window.location.hash, "#/other", "hash is correct"); |
| 292 |
|
| 293 |
cockpit.location.go(["gonna-happen"]); |
| 294 |
assert.strictEqual(window.location.hash, "#/gonna-happen", "hash has changed"); |
| 295 |
|
| 296 |
done(); |
| 297 |
}, 100); |
| 298 |
|
| 299 |
/* User or something else navigates */ |
| 300 |
window.location.hash = "#/other"; |
| 301 |
}); |
| 302 |
|
| 303 |
/* Set url_root in beforeEach so the test does not flake */ |
| 304 |
QUnit.module("url_root tests", { |
| 305 |
beforeEach: () => { |
| 306 |
window.mock = { url_root: "cockpit" }; |
| 307 |
}, |
| 308 |
afterEach: () => { |
| 309 |
window.mock = null; |
| 310 |
} |
| 311 |
}); |
| 312 |
|
| 313 |
QUnit.test("encode", assert => { |
| 314 |
let path = cockpit.location.encode("path", "", true); |
| 315 |
assert.equal(path, "/cockpit/path", "path is correct"); |
| 316 |
/* an existing url_root should not be prepended */ |
| 317 |
path = cockpit.location.encode("/cockpit/path", "", true); |
| 318 |
assert.equal(path, "/cockpit/path", "path is correct"); |
| 319 |
}); |
| 320 |
|
| 321 |
QUnit.start(); |