Skip to content

A platform for community discussion. Free, open, simple.

DEV: Use a separate KVS namespace for tests (#17591)

Jarek Radoszcb40e4c322a5
diff --git a/app/assets/javascripts/discourse/app/lib/key-value-store.js b/app/assets/javascripts/discourse/app/lib/key-value-store.js
index 1b67bf52a3e..b5436cf853a 100644
--- a/app/assets/javascripts/discourse/app/lib/key-value-store.js
+++ b/app/assets/javascripts/discourse/app/lib/key-value-store.js
@@ -1,3 +1,5 @@
+import { isTesting } from "discourse-common/config/environment";
+
// A simple key value store that uses LocalStorage
let safeLocalStorage;
@@ -10,7 +12,7 @@ try {
safeLocalStorage["safeLocalStorage"] = true;
}
} catch (e) {
- // cookies disabled, we don't care
+ // local storage disabled
safeLocalStorage = null;
}
@@ -18,7 +20,7 @@ export default class KeyValueStore {
context = null;
constructor(ctx) {
- this.context = ctx;
+ this.context = isTesting() ? `test_${ctx}` : ctx;
}
abandonLocal() {
diff --git a/app/assets/javascripts/discourse/tests/unit/lib/key-value-store-test.js b/app/assets/javascripts/discourse/tests/unit/lib/key-value-store-test.js
index ed323d85b9a..7308120df82 100644
--- a/app/assets/javascripts/discourse/tests/unit/lib/key-value-store-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/lib/key-value-store-test.js
@@ -3,14 +3,14 @@ import KeyValueStore from "discourse/lib/key-value-store";
module("Unit | Utility | key-value-store", function () {
test("is able to get the result back from the store", function (assert) {
- const store = new KeyValueStore("test_");
+ const store = new KeyValueStore("example");
store.set({ key: "bob", value: "uncle" });
assert.strictEqual(store.get("bob"), "uncle");
});
test("is able remove items from the store", function (assert) {
- const store = new KeyValueStore("test_");
+ const store = new KeyValueStore("example");
store.set({ key: "bob", value: "uncle" });
store.remove("bob");
@@ -18,7 +18,7 @@ module("Unit | Utility | key-value-store", function () {
});
test("is able to nuke the store", function (assert) {
- const store = new KeyValueStore("test_");
+ const store = new KeyValueStore("example");
store.set({ key: "bob1", value: "uncle" });
store.abandonLocal();
localStorage.a = 1;
@@ -28,7 +28,7 @@ module("Unit | Utility | key-value-store", function () {
});
test("is API-compatible with `localStorage`", function (assert) {
- const store = new KeyValueStore("test_");
+ const store = new KeyValueStore("example");
store.setItem("bob", "uncle");
assert.strictEqual(store.getItem("bob"), "uncle");