diff --git a/assets/static/__mocks__/localStorageMock.js b/assets/static/__mocks__/localStorageMock.js index 9935e1d36..78e61825a 100644 --- a/assets/static/__mocks__/localStorageMock.js +++ b/assets/static/__mocks__/localStorageMock.js @@ -1,22 +1,21 @@ -// source: https://github.com/facebook/jest/issues/2098 +class LocalStorageMock { -var localStorageMock = (function() { - var store = {}; + constructor() { + this.store = {}; + } - return { - getItem: function(key) { - return store[key] || null; - }, - setItem: function(key, value) { - store[key] = value.toString(); - }, - clear: function() { - store = {}; - } - }; + getItem(key) { + return this.store[key] || null; + } -})(); + setItem(key, value) { + this.store[key] = value.toString(); + } -Object.defineProperty(window, "localStorage", { - value: localStorageMock -}); + clear() { + this.store = {}; + } + +} + +module.exports = new LocalStorageMock(); diff --git a/assets/static/filters.js b/assets/static/filters.js index 3a99f6d2b..a6fe35373 100644 --- a/assets/static/filters.js +++ b/assets/static/filters.js @@ -21,6 +21,7 @@ var selectors = { historyMenu: "#historyMenu" }; var appendsEnabled = true; +var historyStorage; const historyKey = "filterHistory"; function addBadge(text) { @@ -69,13 +70,11 @@ function setPause() { } function renderHistory() { - const storage = window.localStorage; - var historicFilters = []; const currentFilterText = getFilters().join(","); - const history = storage.getItem(historyKey); + const history = historyStorage.getItem(historyKey); if (history) { historicFilters = history.split("\n"); } @@ -93,14 +92,12 @@ function appendFilterToHistory(text) { // require non empty text and enabled appends if (!text || !appendsEnabled) return false; - const storage = window.localStorage; - // final filter list we'll save to storage var filterList = [ text ]; // get current history list from storage and append it to our final list // of filters, but avoid duplicates - const history = storage.getItem(historyKey); + const history = historyStorage.getItem(historyKey); if (history) { const historyArr = history.split("\n"); for (var i = 0; i < historyArr.length; i++) { @@ -114,7 +111,7 @@ function appendFilterToHistory(text) { // truncate the history to up to 11 elements filterList = filterList.slice(0, 11); - storage.setItem(historyKey, filterList.join("\n")); + historyStorage.setItem(historyKey, filterList.join("\n")); } function setFilters() { @@ -131,7 +128,8 @@ function setFilters() { unsee.triggerReload(); } -function init() { +function init(historyStore) { + historyStorage = historyStore; var initialFilter; if ($(selectors.filter).data("default-used") == "false" || $(selectors.filter).data("default-used") === false) { diff --git a/assets/static/filters.test.js b/assets/static/filters.test.js index f9fc45edc..d01fb3c1e 100644 --- a/assets/static/filters.test.js +++ b/assets/static/filters.test.js @@ -1,5 +1,5 @@ const $ = window.jQuery = require("jquery"); -require("./__mocks__/localStorageMock"); +const LocalStorageMock = require("./__mocks__/localStorageMock"); test("filters addBadge()", () => { const filters = require("./filters"); @@ -7,7 +7,7 @@ test("filters addBadge()", () => { }); test("default filter should be in history after setting filter to foo", () => { - localStorage.clear(); + LocalStorageMock.clear(); document.body.innerHTML = "