| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
import cockpit from "cockpit"; |
| 3 |
import QUnit from "qunit-tests"; |
| 4 |
|
| 5 |
function test_storage (assert, storage, cockpitStorage) { |
| 6 |
assert.expect(29); |
| 7 |
storage.clear(); |
| 8 |
window.mock = { |
| 9 |
pathname: "/cockpit+test/test" |
| 10 |
}; |
| 11 |
|
| 12 |
assert.equal(cockpitStorage.prefixedKey("key1"), "cockpit+test:key1", "prefixed key has application"); |
| 13 |
|
| 14 |
/* setting */ |
| 15 |
cockpitStorage.setItem("key1", "value1", false); |
| 16 |
assert.equal(storage.getItem("cockpit+test:key1"), "value1", "set single: application key set"); |
| 17 |
assert.equal(storage.getItem("key1"), null, "set single: key not set"); |
| 18 |
cockpitStorage.setItem("key1", "value2", true); |
| 19 |
assert.equal(storage.getItem("cockpit+test:key1"), "value2", "set both: application key set"); |
| 20 |
assert.equal(storage.getItem("key1"), "value2", "set both: key set"); |
| 21 |
storage.clear(); |
| 22 |
|
| 23 |
/* getting */ |
| 24 |
storage.setItem("key1", "value1"); |
| 25 |
assert.equal(cockpitStorage.getItem("key1", false), null, "get single doesn't default to bare key"); |
| 26 |
assert.equal(cockpitStorage.getItem("key1", true), "value1", "get both defaults to bare key"); |
| 27 |
storage.setItem("cockpit+test:key1", "value2"); |
| 28 |
assert.equal(storage.getItem("key1"), "value1", "bare key not changed"); |
| 29 |
assert.equal(cockpitStorage.getItem("key1", false), "value2", "get single gets application prefixed value"); |
| 30 |
assert.equal(cockpitStorage.getItem("key1", true), "value2", "get both prefers application prefixed value"); |
| 31 |
|
| 32 |
/* removing */ |
| 33 |
cockpitStorage.removeItem("key1", false); |
| 34 |
assert.equal(storage.getItem("key1"), "value1", "remove single doesn't remove bare key"); |
| 35 |
assert.equal(storage.getItem("cockpit+test:key1"), null, "remove single removes application prefixed key"); |
| 36 |
storage.setItem("cockpit+test:key1", "value1"); |
| 37 |
assert.equal(storage.getItem("cockpit+test:key1"), "value1", "application prefixed value reset"); |
| 38 |
cockpitStorage.removeItem("key1", true); |
| 39 |
assert.equal(storage.getItem("key1"), null, "remove both removes bare key"); |
| 40 |
assert.equal(storage.getItem("cockpit+test:key1"), null, "remove both removes application prefixed key"); |
| 41 |
storage.clear(); |
| 42 |
|
| 43 |
/* clearing */ |
| 44 |
storage.setItem("key1", "value"); |
| 45 |
storage.setItem("key2", "value"); |
| 46 |
storage.setItem("cockpit+other:key1", "value"); |
| 47 |
storage.setItem("cockpit+other:key2", "value"); |
| 48 |
storage.setItem("cockpit+test:key1", "value"); |
| 49 |
storage.setItem("cockpit+test:key2", "value"); |
| 50 |
|
| 51 |
cockpitStorage.clear(false); |
| 52 |
assert.equal(storage.getItem("key1"), "value", "clear doesn't remove bare key1"); |
| 53 |
assert.equal(storage.getItem("key2"), "value", "clear doesn't remove bare key2"); |
| 54 |
assert.equal(storage.getItem("cockpit+other:key1"), "value", "clear doesn't remove other application's key1"); |
| 55 |
assert.equal(storage.getItem("cockpit+other:key2"), "value", "clear doesn't remove other application's key2"); |
| 56 |
assert.equal(storage.getItem("cockpit+test:key1"), null, "clear doesn't remove our application's key1"); |
| 57 |
assert.equal(storage.getItem("cockpit+test:key2"), null, "clear doesn't remove our application's key2"); |
| 58 |
|
| 59 |
storage.setItem("cockpit+test:key1", "value"); |
| 60 |
storage.setItem("cockpit+test:key2", "value"); |
| 61 |
assert.equal(storage.getItem("cockpit+test:key1"), "value", "our application's key1 reset"); |
| 62 |
assert.equal(storage.getItem("cockpit+test:key2"), "value", "our application's key2 reset"); |
| 63 |
|
| 64 |
cockpitStorage.clear(true); |
| 65 |
assert.equal(storage.getItem("key1"), null, "clear full removes bare key1"); |
| 66 |
assert.equal(storage.getItem("key2"), null, "clear full removes bare key2"); |
| 67 |
assert.equal(storage.getItem("cockpit+other:key1"), "value", "clear full doesn't remove other application's key1"); |
| 68 |
assert.equal(storage.getItem("cockpit+other:key2"), "value", "clear full doesn't remove other application's key2"); |
| 69 |
assert.equal(storage.getItem("cockpit+test:key1"), null, "clear full removes our application's key1"); |
| 70 |
assert.equal(storage.getItem("cockpit+test:key2"), null, "clear full removes our application's key2"); |
| 71 |
} |
| 72 |
|
| 73 |
QUnit.test("local-storage", function (assert) { |
| 74 |
test_storage(assert, window.localStorage, cockpit.localStorage); |
| 75 |
}); |
| 76 |
|
| 77 |
QUnit.test("session-storage", function (assert) { |
| 78 |
test_storage(assert, window.sessionStorage, cockpit.sessionStorage); |
| 79 |
}); |
| 80 |
|
| 81 |
// Start tests after we have a user object |
| 82 |
cockpit.user().then(QUnit.start); |