diff --git a/src/api/storage.test.ts b/src/api/storage.test.ts new file mode 100644 index 0000000..8f7a11c --- /dev/null +++ b/src/api/storage.test.ts @@ -0,0 +1,14 @@ +import { loadCredentials, saveCredentials } from "./storage"; + +import { Player } from "../logic"; + +test("load missing credentials", () => + expect(loadCredentials("match", Player.Red)).toBe(null)); + +test("save and load credentials", () => { + const matchID = "match"; + const player = Player.Red; + const credentials = "credentials"; + saveCredentials(matchID, player, credentials); + expect(loadCredentials(matchID, player)).toBe(credentials); +}); diff --git a/src/setupTests.ts b/src/setupTests.ts index 5fdf001..b0db851 100644 --- a/src/setupTests.ts +++ b/src/setupTests.ts @@ -3,3 +3,20 @@ // expect(element).toHaveTextContent(/react/i) // learn more: https://github.com/testing-library/jest-dom import "@testing-library/jest-dom/extend-expect"; + +var localStorageMock = (function () { + var storage: any = {}; + return { + getItem: (key: any) => storage[key] || null, + setItem: (key: string, value: any) => { + storage[key] = value.toString(); + }, + clear: () => { + storage = {}; + }, + }; +})(); + +Object.defineProperty(window, "localStorage", { + value: localStorageMock, +});