Farmbot-Web-App/frontend/__tests__/session_test.ts

63 lines
1.7 KiB
TypeScript
Raw Permalink Normal View History

2017-12-27 09:24:50 -07:00
import {
isNumericSetting,
isBooleanSetting,
2018-12-18 11:25:17 -07:00
safeBooleanSetting,
2018-05-15 20:38:52 -06:00
safeNumericSetting,
Session,
2017-12-27 09:24:50 -07:00
} from "../session";
2018-05-15 20:38:52 -06:00
import { auth } from "../__test_support__/fake_state/token";
describe("fetchStoredToken", () => {
it("can't fetch token", () => {
expect(Session.fetchStoredToken()).toEqual(undefined);
});
it("can fetch token", () => {
2018-08-02 16:18:54 -06:00
localStorage.setItem("session", JSON.stringify(auth));
2018-05-15 20:38:52 -06:00
expect(Session.fetchStoredToken()).toEqual(auth);
});
});
2017-12-27 09:24:50 -07:00
describe("isNumericSetting", () => {
it("determines numericality", () => {
expect(isNumericSetting("zoom_level")).toBe(true);
2017-12-27 09:24:50 -07:00
expect(isNumericSetting("foo")).toBe(false);
});
});
describe("isBooleanSetting", () => {
it("determines boolean-ness of settings", () => {
expect(isBooleanSetting("x_axis_inverted")).toBe(true);
2017-12-27 09:24:50 -07:00
expect(isBooleanSetting("no")).toBe(false);
});
});
describe("safeBooleanSetting", () => {
it("safely fetches bool", () => {
2018-12-18 11:25:17 -07:00
expect(() => safeBooleanSetting("no")).toThrow();
expect(safeBooleanSetting("x_axis_inverted")).toBe("x_axis_inverted");
2017-12-27 09:24:50 -07:00
});
});
describe("safeNumericSetting", () => {
it("safely returns num", () => {
expect(() => safeNumericSetting("no")).toThrow();
expect(safeNumericSetting("zoom_level")).toBe("zoom_level");
2017-12-27 09:24:50 -07:00
});
});
describe("clear()", () => {
it("clears", () => {
jest.clearAllMocks();
localStorage.foo = "bar";
sessionStorage.foo = "bar";
location.assign = jest.fn();
expect(localStorage.foo).toBeTruthy();
expect(sessionStorage.foo).toBeTruthy();
expect(Session.clear()).toEqual(undefined);
expect(location.assign).toHaveBeenCalled();
expect(localStorage.foo).toBeFalsy();
expect(sessionStorage.foo).toBeFalsy();
});
});