Farmbot-Web-App/frontend/__tests__/routes_test.tsx

49 lines
1.4 KiB
TypeScript
Raw Normal View History

let mockAuth: AuthState | undefined = undefined;
2017-12-02 01:40:29 -07:00
jest.mock("../session", () => ({
Session: {
fetchStoredToken: jest.fn(() => mockAuth),
2017-12-02 01:40:29 -07:00
getAll: () => undefined,
2018-06-15 18:28:42 -06:00
clear: jest.fn()
2017-12-02 01:40:29 -07:00
}
}));
2018-05-15 20:38:52 -06:00
let mockPathname = "";
jest.mock("../history", () => ({
history: {
getCurrentLocation: () => ({ pathname: mockPathname })
}
}));
2017-12-02 01:40:29 -07:00
import * as React from "react";
import { shallow } from "enzyme";
import { RootComponent } from "../routes";
import { store } from "../redux/store";
import { AuthState } from "../auth/interfaces";
import { auth } from "../__test_support__/fake_state/token";
2018-06-15 18:28:42 -06:00
import { Session } from "../session";
2020-01-28 12:30:04 -07:00
import { FourOhFour } from "../404";
2017-12-02 01:40:29 -07:00
describe("<RootComponent />", () => {
it("clears session when not authorized", () => {
mockAuth = undefined;
2018-05-15 20:38:52 -06:00
mockPathname = "/app/account";
2018-10-12 12:14:49 -06:00
const wrapper = shallow(<RootComponent store={store} />);
2018-06-15 18:28:42 -06:00
expect(Session.clear).toHaveBeenCalled();
2018-10-12 12:14:49 -06:00
wrapper.unmount();
2017-12-02 01:40:29 -07:00
});
it("authorized", () => {
mockAuth = auth;
2018-05-15 20:38:52 -06:00
mockPathname = "/app/account";
2018-10-12 12:14:49 -06:00
const wrapper = shallow(<RootComponent store={store} />);
2018-06-15 18:28:42 -06:00
expect(Session.clear).not.toHaveBeenCalled();
2018-10-12 12:14:49 -06:00
wrapper.unmount();
2017-12-02 01:40:29 -07:00
});
2020-01-28 12:30:04 -07:00
it("changes route", () => {
const wrapper = shallow<RootComponent>(<RootComponent store={store} />);
wrapper.instance().changeRoute(FourOhFour);
expect(wrapper.state().Route).toEqual(FourOhFour);
});
2017-12-02 01:40:29 -07:00
});