Skip to content

Commit

Permalink
Write tests for storage
Browse files Browse the repository at this point in the history
  • Loading branch information
averycrespi committed Jul 17, 2020
1 parent bbfda57 commit bbb6789
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/api/storage.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
17 changes: 17 additions & 0 deletions src/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

0 comments on commit bbb6789

Please sign in to comment.