From b336ac6813e209245717564220e2dd1c1ad50d98 Mon Sep 17 00:00:00 2001 From: EmmaLRussell Date: Tue, 10 Oct 2023 17:28:57 +0100 Subject: [PATCH] more unit tests --- .../tests/unit/localStorageManager.test.ts | 39 ++++++++++++++++++- .../unit/store/appState/mutations.test.ts | 10 +++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/app/static/tests/unit/localStorageManager.test.ts b/app/static/tests/unit/localStorageManager.test.ts index 906bf20cb..a8a7a6071 100644 --- a/app/static/tests/unit/localStorageManager.test.ts +++ b/app/static/tests/unit/localStorageManager.test.ts @@ -1,6 +1,6 @@ import { localStorageManager } from "../../src/app/localStorageManager"; -describe("localStorageManager", () => { +describe("localStorageManager for sessions", () => { const spyOnGetItem = jest.spyOn(Storage.prototype, "getItem").mockReturnValue("[\"session1\", \"session2\"]"); const spyOnSetItem = jest.spyOn(Storage.prototype, "setItem"); @@ -46,3 +46,40 @@ describe("localStorageManager", () => { expect(spyOnSetItem.mock.calls[0][1]).toBe(JSON.stringify(["session1"])); }); }); + +describe("localStorageManager gets and saves user preferences", () => { + const spyOnGetItem = jest.spyOn(Storage.prototype, "getItem") + .mockReturnValue("{\"showUnlabelledSessions\": false}"); + const spyOnSetItem = jest.spyOn(Storage.prototype, "setItem"); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it("can get user preferences", () => { + const result = localStorageManager.getUserPreferences(); + expect(result).toStrictEqual({ showUnlabelledSessions: false }); + expect(spyOnGetItem).toHaveBeenCalledWith("preferences"); + }); + + it("can set user preferences", () => { + const prefs = { showUnlabelledSessions: false }; + localStorageManager.setUserPreferences(prefs); + expect(spyOnSetItem).toHaveBeenCalledWith("preferences", JSON.stringify(prefs)); + }); +}); + +describe("localStorageManager gets default user preferences", () => { + const spyOnGetItem = jest.spyOn(Storage.prototype, "getItem") + .mockReturnValue(null); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it("can get default user preferences", () => { + const result = localStorageManager.getUserPreferences(); + expect(result).toStrictEqual({ showUnlabelledSessions: true }); + expect(spyOnGetItem).toHaveBeenCalledWith("preferences"); + }); +}); diff --git a/app/static/tests/unit/store/appState/mutations.test.ts b/app/static/tests/unit/store/appState/mutations.test.ts index bccaaaf97..ed2fed5dd 100644 --- a/app/static/tests/unit/store/appState/mutations.test.ts +++ b/app/static/tests/unit/store/appState/mutations.test.ts @@ -76,4 +76,14 @@ describe("AppState mutations", () => { appStateMutations.SetPersisted(state); expect(state.persisted).toBe(true); }); + + it("sets userPreferences", () => { + const state = mockBasicState(); + const committed = { + showUnlabelledSessions: false, + someValue: "other" + }; + appStateMutations.SetUserPreferences(state, committed); + expect(state.userPreferences).toBe(committed); + }); });