Skip to content

Commit

Permalink
more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaLRussell committed Oct 10, 2023
1 parent cc74a55 commit b336ac6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
39 changes: 38 additions & 1 deletion app/static/tests/unit/localStorageManager.test.ts
Original file line number Diff line number Diff line change
@@ -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");

Expand Down Expand Up @@ -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");
});
});
10 changes: 10 additions & 0 deletions app/static/tests/unit/store/appState/mutations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit b336ac6

Please sign in to comment.