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

This commit is contained in:
Jarek Radosz 2022-07-21 15:27:24 +02:00 committed by GitHub
parent 331874229e
commit cb40e4c322
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 6 deletions

View File

@ -1,3 +1,5 @@
import { isTesting } from "discourse-common/config/environment";
// A simple key value store that uses LocalStorage // A simple key value store that uses LocalStorage
let safeLocalStorage; let safeLocalStorage;
@ -10,7 +12,7 @@ try {
safeLocalStorage["safeLocalStorage"] = true; safeLocalStorage["safeLocalStorage"] = true;
} }
} catch (e) { } catch (e) {
// cookies disabled, we don't care // local storage disabled
safeLocalStorage = null; safeLocalStorage = null;
} }
@ -18,7 +20,7 @@ export default class KeyValueStore {
context = null; context = null;
constructor(ctx) { constructor(ctx) {
this.context = ctx; this.context = isTesting() ? `test_${ctx}` : ctx;
} }
abandonLocal() { abandonLocal() {

View File

@ -3,14 +3,14 @@ import KeyValueStore from "discourse/lib/key-value-store";
module("Unit | Utility | key-value-store", function () { module("Unit | Utility | key-value-store", function () {
test("is able to get the result back from the store", function (assert) { 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" }); store.set({ key: "bob", value: "uncle" });
assert.strictEqual(store.get("bob"), "uncle"); assert.strictEqual(store.get("bob"), "uncle");
}); });
test("is able remove items from the store", function (assert) { 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.set({ key: "bob", value: "uncle" });
store.remove("bob"); store.remove("bob");
@ -18,7 +18,7 @@ module("Unit | Utility | key-value-store", function () {
}); });
test("is able to nuke the store", function (assert) { 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.set({ key: "bob1", value: "uncle" });
store.abandonLocal(); store.abandonLocal();
localStorage.a = 1; localStorage.a = 1;
@ -28,7 +28,7 @@ module("Unit | Utility | key-value-store", function () {
}); });
test("is API-compatible with `localStorage`", function (assert) { test("is API-compatible with `localStorage`", function (assert) {
const store = new KeyValueStore("test_"); const store = new KeyValueStore("example");
store.setItem("bob", "uncle"); store.setItem("bob", "uncle");
assert.strictEqual(store.getItem("bob"), "uncle"); assert.strictEqual(store.getItem("bob"), "uncle");