Farmbot-Web-App/frontend/front_page/__tests__/front_page_test.tsx

261 lines
9.8 KiB
TypeScript
Raw Normal View History

2018-05-15 01:12:59 -06:00
let mockAxiosResponse = Promise.resolve({ data: "" });
2019-02-04 18:54:59 -07:00
2017-12-19 18:27:25 -07:00
jest.mock("axios", () => ({
2019-02-04 18:54:59 -07:00
post: jest.fn(() => mockAxiosResponse)
2017-12-19 18:27:25 -07:00
}));
2019-03-11 20:34:49 -06:00
let mockAuth: AuthState | undefined = undefined;
2018-05-15 01:12:59 -06:00
jest.mock("../../session", () => ({
Session: {
replaceToken: jest.fn(),
2019-03-11 20:34:49 -06:00
fetchStoredToken: () => mockAuth,
2018-05-15 01:12:59 -06:00
}
}));
2017-12-19 18:27:25 -07:00
jest.mock("../../api", () => ({
API: {
setBaseUrl: jest.fn(),
fetchBrowserLocation: jest.fn(),
fetchHostName: () => "localhost",
inferPort: () => 3000,
2017-12-20 20:47:15 -07:00
current: {
tokensPath: "://localhost:3000/api/tokens/",
passwordResetPath: "resetPath",
usersPath: "usersPath"
}
2017-12-19 18:27:25 -07:00
}
}));
2020-05-06 16:03:15 -06:00
jest.mock("../laptop_splash", () => ({ LaptopSplash: () => <div /> }));
import * as React from "react";
import { mount, shallow } from "enzyme";
2018-11-30 19:41:50 -07:00
import { FrontPage, setField, PartialFormEvent } from "../front_page";
2019-02-06 18:36:11 -07:00
import axios from "axios";
2017-12-19 18:27:25 -07:00
import { API } from "../../api";
2018-05-15 01:12:59 -06:00
import { Session } from "../../session";
2019-06-24 15:39:49 -06:00
import { success, error } from "../../toast/toast";
import { Content } from "../../constants";
2019-03-11 20:34:49 -06:00
import { AuthState } from "../../auth/interfaces";
import { auth } from "../../__test_support__/fake_state/token";
2020-02-07 16:05:16 -07:00
import { formEvent } from "../../__test_support__/fake_html_events";
describe("<FrontPage />", () => {
2019-03-11 20:34:49 -06:00
beforeEach(() => { mockAuth = undefined; });
2020-02-07 16:05:16 -07:00
const fakeFormEvent = formEvent();
2018-11-09 13:43:51 -07:00
it("shows forgot password box", () => {
2017-08-28 05:49:13 -06:00
const el = mount(<FrontPage />);
expect(el.text()).not.toContain("Reset Password");
el.find("a.forgot-password").first().simulate("click");
expect(el.text()).toContain("Reset Password");
});
2017-12-19 14:19:59 -07:00
it("shows TOS and Privacy links", () => {
const el = mount(<FrontPage />);
["Privacy Policy", "Terms of Use"].map(string =>
expect(el.text()).toContain(string));
2019-05-20 14:14:16 -06:00
["https://farm.bot/privacy/", "https://farm.bot/tos/"]
2017-12-19 14:19:59 -07:00
.map(string => expect(el.html()).toContain(string));
});
2017-12-19 18:27:25 -07:00
2019-03-11 20:34:49 -06:00
it("redirects when already logged in", () => {
mockAuth = auth;
location.assign = jest.fn();
const el = mount(<FrontPage />);
el.mount();
expect(location.assign).toHaveBeenCalledWith("/app/controls");
});
2018-05-15 01:12:59 -06:00
it("submits login: success", async () => {
mockAxiosResponse = Promise.resolve({ data: "new data" });
2019-03-11 20:34:49 -06:00
location.assign = jest.fn();
2018-11-09 13:43:51 -07:00
const el = mount<FrontPage>(<FrontPage />);
2018-05-15 20:38:52 -06:00
el.setState({ email: "foo@bar.io", loginPassword: "password" });
2020-02-07 16:05:16 -07:00
await el.instance().submitLogin(fakeFormEvent);
2018-05-15 01:12:59 -06:00
expect(API.setBaseUrl).toHaveBeenCalled();
2019-02-06 18:36:11 -07:00
expect(axios.post).toHaveBeenCalledWith(
2018-05-15 01:12:59 -06:00
"://localhost:3000/api/tokens/",
{ user: { email: "foo@bar.io", password: "password" } });
expect(Session.replaceToken).toHaveBeenCalledWith("new data");
2019-03-11 20:34:49 -06:00
expect(location.assign).toHaveBeenCalledWith("/app/controls");
2018-05-15 01:12:59 -06:00
});
it("submits login: not verified", async () => {
mockAxiosResponse = Promise.reject({ response: { status: 403 } });
2018-11-09 13:43:51 -07:00
const el = mount<FrontPage>(<FrontPage />);
2018-05-15 20:38:52 -06:00
el.setState({ email: "foo@bar.io", loginPassword: "password" });
2020-02-07 16:05:16 -07:00
await el.instance().submitLogin(fakeFormEvent);
expect(API.setBaseUrl).toHaveBeenCalled();
2019-02-06 18:36:11 -07:00
expect(axios.post).toHaveBeenCalledWith(
2017-12-19 18:27:25 -07:00
"://localhost:3000/api/tokens/",
{ user: { email: "foo@bar.io", password: "password" } });
2018-05-15 01:12:59 -06:00
expect(Session.replaceToken).not.toHaveBeenCalled();
// expect(error).toHaveBeenCalledWith("Account Not Verified");
// expect(instance.state.activePanel).toEqual("resendVerificationEmail");
2017-12-19 18:27:25 -07:00
});
2017-12-20 20:47:15 -07:00
2018-05-15 20:38:52 -06:00
it("submits login: TOS update", async () => {
mockAxiosResponse = Promise.reject({ response: { status: 451 } });
window.location.assign = jest.fn();
2018-11-09 13:43:51 -07:00
const el = mount<FrontPage>(<FrontPage />);
2018-05-15 20:38:52 -06:00
el.setState({ email: "foo@bar.io", loginPassword: "password" });
2020-02-07 16:05:16 -07:00
await el.instance().submitLogin(fakeFormEvent);
2018-05-15 20:38:52 -06:00
expect(API.setBaseUrl).toHaveBeenCalled();
2019-02-06 18:36:11 -07:00
expect(axios.post).toHaveBeenCalledWith(
2018-05-15 20:38:52 -06:00
"://localhost:3000/api/tokens/",
{ user: { email: "foo@bar.io", password: "password" } });
await expect(Session.replaceToken).not.toHaveBeenCalled();
expect(window.location.assign).toHaveBeenCalledWith("/tos_update");
});
2018-11-30 19:41:50 -07:00
it("submits registration: success", async () => {
mockAxiosResponse = Promise.resolve({ data: "new data" });
2018-11-09 13:43:51 -07:00
const el = mount<FrontPage>(<FrontPage />);
2017-12-20 20:47:15 -07:00
el.setState({
regEmail: "foo@bar.io",
regName: "Foo Bar",
regPassword: "password",
2018-11-09 13:43:51 -07:00
regConfirmation: "password",
2017-12-20 20:47:15 -07:00
agreeToTerms: true
});
2020-02-07 16:05:16 -07:00
await el.instance().submitRegistration(fakeFormEvent);
2019-02-06 18:36:11 -07:00
expect(axios.post).toHaveBeenCalledWith("usersPath", {
2017-12-20 20:47:15 -07:00
user: {
agree_to_terms: true, email: "foo@bar.io", name: "Foo Bar",
2018-11-09 13:43:51 -07:00
password: "password", password_confirmation: "password"
2017-12-20 20:47:15 -07:00
}
});
2018-11-30 19:41:50 -07:00
expect(success).toHaveBeenCalledWith(
2019-07-02 11:03:16 -06:00
expect.stringContaining("Almost done!"));
2018-11-30 19:41:50 -07:00
expect(el.instance().state.registrationSent).toEqual(true);
});
it("submits registration: failure", async () => {
mockAxiosResponse = Promise.reject({ response: { data: ["failure"] } });
const el = mount<FrontPage>(<FrontPage />);
el.setState({
regEmail: "foo@bar.io",
regName: "Foo Bar",
regPassword: "password",
regConfirmation: "password",
agreeToTerms: true
});
2020-02-07 16:05:16 -07:00
await el.instance().submitRegistration(fakeFormEvent);
2019-02-06 18:36:11 -07:00
await expect(axios.post).toHaveBeenCalledWith("usersPath", {
2018-11-30 19:41:50 -07:00
user: {
agree_to_terms: true, email: "foo@bar.io", name: "Foo Bar",
password: "password", password_confirmation: "password"
}
});
await expect(error).toHaveBeenCalledWith(
expect.stringContaining("failure"));
expect(el.instance().state.registrationSent).toEqual(false);
});
it("submits forgot password: success", async () => {
mockAxiosResponse = Promise.resolve({ data: "" });
const el = mount<FrontPage>(<FrontPage />);
el.setState({ email: "foo@bar.io", activePanel: "forgotPassword" });
2020-02-07 16:05:16 -07:00
await el.instance().submitForgotPassword(fakeFormEvent);
2019-02-06 18:36:11 -07:00
await expect(axios.post).toHaveBeenCalledWith("resetPath",
2018-11-30 19:41:50 -07:00
{ email: "foo@bar.io" });
await expect(success).toHaveBeenCalledWith(
"Email has been sent.", "Forgot Password");
expect(el.instance().state.activePanel).toEqual("login");
});
it("submits forgot password: error", async () => {
mockAxiosResponse = Promise.reject({ response: { data: ["failure"] } });
const el = mount<FrontPage>(<FrontPage />);
el.setState({ email: "foo@bar.io", activePanel: "forgotPassword" });
2020-02-07 16:05:16 -07:00
await el.instance().submitForgotPassword(fakeFormEvent);
2019-02-06 18:36:11 -07:00
await expect(axios.post).toHaveBeenCalledWith("resetPath",
2018-11-30 19:41:50 -07:00
{ email: "foo@bar.io" });
await expect(error).toHaveBeenCalledWith(
expect.stringContaining("failure"));
expect(el.instance().state.activePanel).toEqual("forgotPassword");
2017-12-20 20:47:15 -07:00
});
2018-11-30 19:41:50 -07:00
it("submits forgot password: no email error", async () => {
mockAxiosResponse = Promise.reject({ response: { data: ["not found"] } });
2018-11-09 13:43:51 -07:00
const el = mount<FrontPage>(<FrontPage />);
2018-11-30 19:41:50 -07:00
el.setState({ email: "foo@bar.io", activePanel: "forgotPassword" });
2020-02-07 16:05:16 -07:00
await el.instance().submitForgotPassword(fakeFormEvent);
2019-02-06 18:36:11 -07:00
await expect(axios.post).toHaveBeenCalledWith("resetPath",
2017-12-20 20:47:15 -07:00
{ email: "foo@bar.io" });
2018-11-30 19:41:50 -07:00
await expect(error).toHaveBeenCalledWith(expect.stringContaining(
"not associated with an account"));
expect(el.instance().state.activePanel).toEqual("forgotPassword");
2017-12-20 20:47:15 -07:00
});
it("renders proper panels", () => {
const el = mount(<FrontPage />);
el.setState({ activePanel: "resendVerificationEmail" });
expect(el.text()).toContain("Account Not Verified");
el.setState({ activePanel: "forgotPassword" });
expect(el.text()).toContain("Reset Password");
el.setState({ activePanel: "login" });
expect(el.text()).toContain("Login");
});
it("has a generalized form field setter fn", () => {
const spy = jest.fn();
2018-11-30 19:41:50 -07:00
type Input = Partial<PartialFormEvent["currentTarget"]>;
const fakeEv = (input: Input): PartialFormEvent => {
return {
currentTarget: {
checked: true,
defaultValue: "defaultValue",
value: "value",
...input
}
2018-07-20 04:24:02 -06:00
};
};
2018-07-20 04:24:02 -06:00
const agreeToTerms = setField("agreeToTerms", spy);
const event2 = fakeEv({ checked: false });
const expected2 = { agreeToTerms: event2.currentTarget.checked };
agreeToTerms(event2);
expect(spy).toHaveBeenCalledWith(expected2);
jest.resetAllMocks();
2018-07-20 04:24:02 -06:00
const regName = setField("regName", spy);
const event3 = fakeEv({ value: "hello!" });
2018-06-15 07:59:59 -06:00
const expected3 = { regName: event3.currentTarget.value };
regName(event3);
expect(spy).toHaveBeenCalledWith(expected3);
jest.resetAllMocks();
});
it("resendVerificationPanel(): ok()", () => {
2018-11-09 13:43:51 -07:00
const wrapper = mount<FrontPage>(<FrontPage />);
const component = shallow(<div>
{wrapper.instance().resendVerificationPanel()}
</div>);
wrapper.instance().setState({ activePanel: "resendVerificationEmail" });
expect(wrapper.instance().state.activePanel)
.toEqual("resendVerificationEmail");
// tslint:disable-next-line:no-any
(component.find("ResendVerification").props() as any).ok();
expect(success).toHaveBeenCalledWith(Content.VERIFICATION_EMAIL_RESENT);
2018-11-09 13:43:51 -07:00
expect(wrapper.instance().state.activePanel).toEqual("login");
});
it("resendVerificationPanel(): no()", () => {
2018-11-09 13:43:51 -07:00
const wrapper = mount<FrontPage>(<FrontPage />);
const component = shallow(<div>
{wrapper.instance().resendVerificationPanel()}
</div>);
wrapper.instance().setState({ activePanel: "resendVerificationEmail" });
expect(wrapper.instance().state.activePanel)
.toEqual("resendVerificationEmail");
// tslint:disable-next-line:no-any
(component.find("ResendVerification").props() as any).no();
expect(error).toHaveBeenCalledWith(Content.VERIFICATION_EMAIL_RESEND_ERROR);
2018-11-09 13:43:51 -07:00
expect(wrapper.instance().state.activePanel).toEqual("login");
});
});