2021-04-12 13:32:25 +00:00
|
|
|
import { mount } from "enzyme";
|
2016-10-19 20:22:18 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
import ConnectedApp from "./App";
|
|
|
|
|
import * as authActions from "../../redux/nodes/auth/actions";
|
|
|
|
|
import helpers from "../../test/helpers";
|
|
|
|
|
import local from "../../utilities/local";
|
2016-09-06 18:41:16 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
const { connectedComponent, reduxMockStore } = helpers;
|
2016-10-04 20:51:30 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
describe("App - component", () => {
|
2016-12-29 20:27:43 +00:00
|
|
|
const store = { app: {}, auth: {}, notifications: {} };
|
2016-10-04 20:51:30 +00:00
|
|
|
const mockStore = reduxMockStore(store);
|
2021-04-12 13:32:25 +00:00
|
|
|
const component = mount(connectedComponent(ConnectedApp, { mockStore }));
|
2016-09-06 18:41:16 +00:00
|
|
|
|
2016-09-19 23:43:35 +00:00
|
|
|
afterEach(() => {
|
2021-04-12 13:32:25 +00:00
|
|
|
local.setItem("auth_token", null);
|
2016-09-19 23:43:35 +00:00
|
|
|
});
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
it("renders", () => {
|
2020-12-01 18:15:12 +00:00
|
|
|
expect(component).toBeTruthy();
|
2016-09-06 18:41:16 +00:00
|
|
|
});
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
it("loads the current user if there is an auth token but no user", () => {
|
|
|
|
|
local.setItem("auth_token", "ABC123");
|
2016-09-19 23:43:35 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
const spy = jest
|
|
|
|
|
.spyOn(authActions, "fetchCurrentUser")
|
|
|
|
|
.mockImplementation(() => {
|
|
|
|
|
return (dispatch) => {
|
|
|
|
|
dispatch({ type: "LOAD_USER_ACTION" });
|
|
|
|
|
return Promise.resolve();
|
|
|
|
|
};
|
|
|
|
|
});
|
2016-10-04 20:51:30 +00:00
|
|
|
const application = connectedComponent(ConnectedApp, { mockStore });
|
2016-09-19 23:43:35 +00:00
|
|
|
|
|
|
|
|
mount(application);
|
|
|
|
|
expect(spy).toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
it("does not load the current user if is it already loaded", () => {
|
|
|
|
|
local.setItem("auth_token", "ABC123");
|
2016-09-19 23:43:35 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
const spy = jest
|
|
|
|
|
.spyOn(authActions, "fetchCurrentUser")
|
|
|
|
|
.mockImplementation(() => {
|
|
|
|
|
return { type: "LOAD_USER_ACTION" };
|
|
|
|
|
});
|
2016-10-04 20:51:30 +00:00
|
|
|
const storeWithUser = {
|
2016-09-23 21:20:02 +00:00
|
|
|
app: {},
|
2016-09-19 23:43:35 +00:00
|
|
|
auth: {
|
|
|
|
|
user: {
|
|
|
|
|
id: 1,
|
2021-04-12 13:32:25 +00:00
|
|
|
email: "hi@thegnar.co",
|
2016-09-19 23:43:35 +00:00
|
|
|
},
|
|
|
|
|
},
|
2016-12-29 20:27:43 +00:00
|
|
|
notifications: {},
|
2016-09-19 23:43:35 +00:00
|
|
|
};
|
2016-10-04 20:51:30 +00:00
|
|
|
const mockStoreWithUser = reduxMockStore(storeWithUser);
|
2021-04-12 13:32:25 +00:00
|
|
|
const application = connectedComponent(ConnectedApp, {
|
|
|
|
|
mockStore: mockStoreWithUser,
|
|
|
|
|
});
|
2016-09-19 23:43:35 +00:00
|
|
|
|
|
|
|
|
mount(application);
|
2020-12-01 18:15:12 +00:00
|
|
|
expect(spy).not.toHaveBeenCalled();
|
2016-09-19 23:43:35 +00:00
|
|
|
});
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
it("does not load the current user if there is no auth token", () => {
|
2020-12-01 18:15:12 +00:00
|
|
|
local.clear();
|
2016-09-19 23:43:35 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
const spy = jest
|
|
|
|
|
.spyOn(authActions, "fetchCurrentUser")
|
|
|
|
|
.mockImplementation(() => {
|
|
|
|
|
throw new Error("should not have been called");
|
|
|
|
|
});
|
2016-10-04 20:51:30 +00:00
|
|
|
const application = connectedComponent(ConnectedApp, { mockStore });
|
2016-09-19 23:43:35 +00:00
|
|
|
|
|
|
|
|
mount(application);
|
2020-12-01 18:15:12 +00:00
|
|
|
expect(spy).not.toHaveBeenCalled();
|
2016-09-19 23:43:35 +00:00
|
|
|
});
|
2016-09-06 18:41:16 +00:00
|
|
|
});
|