diff --git a/frontend/config/__tests__/actions_test.ts b/frontend/config/__tests__/actions_test.ts index aee2412a9..baf50dae9 100644 --- a/frontend/config/__tests__/actions_test.ts +++ b/frontend/config/__tests__/actions_test.ts @@ -1,58 +1,72 @@ -const mockState = { - auth: { - token: { - unencoded: { iss: "http://geocities.com" } - } - } -}; - -jest.mock("axios", () => ({ - interceptors: { - response: { use: jest.fn() }, - request: { use: jest.fn() } - }, - get() { return Promise.resolve({ data: mockState }); } -})); - jest.mock("../../session", () => ({ Session: { fetchStoredToken: jest.fn(), getAll: () => undefined, - clear: jest.fn() + clear: jest.fn(), } })); jest.mock("../../auth/actions", () => ({ didLogin: jest.fn(), - setToken: jest.fn() + setToken: jest.fn(), })); +jest.mock("../../refresh_token", () => ({ maybeRefreshToken: jest.fn() })); + +let mockTimeout = Promise.resolve({ token: "fake token data" }); +jest.mock("promise-timeout", () => ({ timeout: () => mockTimeout })); + import { ready, storeToken } from "../actions"; import { setToken, didLogin } from "../../auth/actions"; import { Session } from "../../session"; import { auth } from "../../__test_support__/fake_state/token"; import { fakeState } from "../../__test_support__/fake_state"; -describe("Actions", () => { - it("calls didLogin()", () => { - jest.resetAllMocks(); +describe("ready()", () => { + it("uses new token", async () => { + const fakeAuth = { token: "fake token data" }; + mockTimeout = Promise.resolve(fakeAuth); const dispatch = jest.fn(); const thunk = ready(); - thunk(dispatch, fakeState); - expect(setToken).toHaveBeenCalled(); + const state = fakeState(); + console.warn = jest.fn(); + await thunk(dispatch, () => state); + expect(setToken).toHaveBeenCalledWith(fakeAuth); + expect(didLogin).toHaveBeenCalledWith(fakeAuth, dispatch); + expect(console.warn).not.toHaveBeenCalled(); + expect(Session.clear).not.toHaveBeenCalled(); }); - it("Calls Session.clear() when missing auth", () => { - jest.resetAllMocks(); + it("uses old token", async () => { + mockTimeout = Promise.reject({ token: "not used" }); + const dispatch = jest.fn(); + const thunk = ready(); + const state = fakeState(); + console.warn = jest.fn(); + await thunk(dispatch, () => state); + expect(setToken).toHaveBeenLastCalledWith(state.auth); + expect(didLogin).toHaveBeenCalledWith(state.auth, dispatch); + expect(console.warn) + .toHaveBeenCalledWith(expect.stringContaining("Can't refresh token.")); + expect(Session.clear).not.toHaveBeenCalled(); + }); + + it("calls Session.clear() when missing auth", () => { const dispatch = jest.fn(); const state = fakeState(); delete state.auth; const getState = () => state; const thunk = ready(); + console.warn = jest.fn(); thunk(dispatch, getState); + expect(setToken).not.toHaveBeenCalled(); + expect(didLogin).not.toHaveBeenCalled(); + expect(console.warn).not.toHaveBeenCalled(); expect(Session.clear).toHaveBeenCalled(); }); +}); +describe("storeToken()", () => { it("stores token", () => { const old = auth; old.token.unencoded.jti = "old"; diff --git a/frontend/devices/connectivity/__tests__/retry_btn_test.tsx b/frontend/devices/connectivity/__tests__/retry_btn_test.tsx deleted file mode 100644 index 5aec1fa77..000000000 --- a/frontend/devices/connectivity/__tests__/retry_btn_test.tsx +++ /dev/null @@ -1,42 +0,0 @@ -import * as React from "react"; -import { shallow } from "enzyme"; -import { RetryBtn } from "../retry_btn"; -import { SpecialStatus } from "farmbot"; - -describe("", () => { - it("is green before saving", () => { - const props = { - flags: [true], - onClick: jest.fn(), - status: SpecialStatus.SAVED - }; - const el = shallow(); - expect(el.find(".green").length).toBe(1); - expect(el.find(".yellow").length).toBe(0); - expect(el.find(".red").length).toBe(0); - }); - - it("is yellow during save", () => { - const props = { - flags: [false, true], - onClick: jest.fn(), - status: SpecialStatus.SAVING - }; - const el = shallow(); - expect(el.find(".green").length).toBe(0); - expect(el.find(".yellow").length).toBe(1); - expect(el.find(".red").length).toBe(0); - }); - - it("is red when problems arise", () => { - const props = { - flags: [true, false], - onClick: jest.fn(), - status: SpecialStatus.SAVED - }; - const el = shallow(); - expect(el.find(".green").length).toBe(0); - expect(el.find(".yellow").length).toBe(0); - expect(el.find(".red").length).toBe(1); - }); -}); diff --git a/frontend/devices/connectivity/retry_btn.tsx b/frontend/devices/connectivity/retry_btn.tsx deleted file mode 100644 index ac3e0bd8a..000000000 --- a/frontend/devices/connectivity/retry_btn.tsx +++ /dev/null @@ -1,18 +0,0 @@ -import * as React from "react"; -import { SpecialStatus } from "farmbot"; -import { t } from "../../i18next_wrapper"; - -interface RetryBtnProps { - flags: boolean[]; - onClick(): void; - status: SpecialStatus; -} - -export function RetryBtn(props: RetryBtnProps) { - const failures = props.flags.includes(false); - const color = failures ? "red" : "green"; - const css = props.status === "SAVING" ? "yellow" : color; - return ; -} diff --git a/frontend/farm_designer/map/legend/__tests__/garden_map_legend_test.tsx b/frontend/farm_designer/map/legend/__tests__/garden_map_legend_test.tsx index a4c3cb69e..ff8cdfbdb 100644 --- a/frontend/farm_designer/map/legend/__tests__/garden_map_legend_test.tsx +++ b/frontend/farm_designer/map/legend/__tests__/garden_map_legend_test.tsx @@ -23,8 +23,6 @@ import { GardenMapLegend, ZoomControls, PointsSubMenu } from "../garden_map_legend"; import { GardenMapLegendProps } from "../../interfaces"; -import { clickButton } from "../../../../__test_support__/helpers"; -import { history } from "../../../../history"; import { BooleanSetting } from "../../../../session_keys"; import { fakeTimeSettings @@ -96,15 +94,6 @@ describe("", () => { }); describe("", () => { - it("navigates to point creator", () => { - const wrapper = mount(); - clickButton(wrapper, 0, "point creator"); - expect(history.push).toHaveBeenCalledWith( - "/app/designer/points/add"); - }); - it("shows historic points", () => { const toggle = jest.fn(); const wrapper = shallow(
- {!DevSettings.futureFeaturesEnabled() && - } -
; diff --git a/frontend/farm_designer/points/__tests__/weeds_add_test.tsx b/frontend/farm_designer/points/__tests__/weeds_add_test.tsx deleted file mode 100644 index b4e78ff3d..000000000 --- a/frontend/farm_designer/points/__tests__/weeds_add_test.tsx +++ /dev/null @@ -1,25 +0,0 @@ -import * as React from "react"; -import { mount } from "enzyme"; -import { - RawAddWeed as AddWeed, AddWeedProps, mapStateToProps -} from "../weeds_add"; -import { fakeState } from "../../../__test_support__/fake_state"; - -describe("", () => { - const fakeProps = (): AddWeedProps => ({ - dispatch: jest.fn(), - }); - - it("renders", () => { - const wrapper = mount(); - expect(wrapper.text()).toContain("Add"); - }); -}); - -describe("mapStateToProps()", () => { - it("returns props", () => { - const state = fakeState(); - const props = mapStateToProps(state); - expect(props.dispatch).toEqual(expect.any(Function)); - }); -}); diff --git a/frontend/farm_designer/points/weeds_add.tsx b/frontend/farm_designer/points/weeds_add.tsx deleted file mode 100644 index 9c83d0e15..000000000 --- a/frontend/farm_designer/points/weeds_add.tsx +++ /dev/null @@ -1,36 +0,0 @@ -import React from "react"; -import { connect } from "react-redux"; -import { - DesignerPanel, DesignerPanelContent, DesignerPanelHeader -} from "../designer_panel"; -import { Everything } from "../../interfaces"; -import { t } from "../../i18next_wrapper"; -import { Panel } from "../panel_header"; - -export interface AddWeedProps { - dispatch: Function; -} - -export interface AddWeedState { -} - -export const mapStateToProps = (props: Everything): AddWeedProps => ({ - dispatch: props.dispatch, -}); - -export class RawAddWeed extends React.Component { - state: AddWeedState = {}; - render() { - return - - - - ; - } -} - -export const AddWeed = connect(mapStateToProps)(RawAddWeed); diff --git a/frontend/farm_designer/saved_gardens/__tests__/saved_gardens_test.tsx b/frontend/farm_designer/saved_gardens/__tests__/saved_gardens_test.tsx index d35487d6d..4fbbb5476 100644 --- a/frontend/farm_designer/saved_gardens/__tests__/saved_gardens_test.tsx +++ b/frontend/farm_designer/saved_gardens/__tests__/saved_gardens_test.tsx @@ -13,17 +13,10 @@ jest.mock("../../../history", () => ({ jest.mock("../../../api/crud", () => ({ edit: jest.fn() })); -let mockDev = false; -jest.mock("../../../account/dev/dev_support", () => ({ - DevSettings: { - futureFeaturesEnabled: () => mockDev, - } -})); - import * as React from "react"; import { mount, shallow } from "enzyme"; import { - RawSavedGardens as SavedGardens, mapStateToProps, SavedGardensLink, + RawSavedGardens as SavedGardens, mapStateToProps, SavedGardenHUD, savedGardenOpen, } from "../saved_gardens"; import { clickButton } from "../../../__test_support__/helpers"; @@ -106,25 +99,6 @@ describe("mapStateToProps()", () => { }); }); -describe("", () => { - it("opens saved garden panel", () => { - mockDev = true; - const wrapper = shallow(); - clickButton(wrapper, 0, "saved gardens"); - expect(history.push).toHaveBeenCalledWith( - "/app/designer/gardens"); - mockDev = false; - }); - - it("saved garden button hidden", () => { - mockDev = false; - const wrapper = shallow(); - const btn = wrapper.find("button").at(0); - expect(btn.props().hidden).toEqual(true); - mockDev = false; - }); -}); - describe("savedGardenOpen", () => { it("is open", () => { const result = savedGardenOpen(["", "", "", "gardens", "4", ""]); diff --git a/frontend/farm_designer/saved_gardens/saved_gardens.tsx b/frontend/farm_designer/saved_gardens/saved_gardens.tsx index 2e0ff5571..ef4c9fde6 100644 --- a/frontend/farm_designer/saved_gardens/saved_gardens.tsx +++ b/frontend/farm_designer/saved_gardens/saved_gardens.tsx @@ -62,15 +62,6 @@ export class RawSavedGardens } } -/** Link to SavedGardens panel for garden map legend. */ -export const SavedGardensLink = () => - ; - /** Check if a SavedGarden is currently open (URL approach). */ export const savedGardenOpen = (pathArray: string[]) => pathArray[3] === "gardens" && parseInt(pathArray[4]) > 0 diff --git a/frontend/tos_update/__tests__/component_test.tsx b/frontend/tos_update/__tests__/component_test.tsx index 4d77e7df7..f35a57f85 100644 --- a/frontend/tos_update/__tests__/component_test.tsx +++ b/frontend/tos_update/__tests__/component_test.tsx @@ -16,9 +16,9 @@ import { API } from "../../api/index"; import { Session } from "../../session"; import { error } from "../../toast/toast"; import { formEvent, inputEvent } from "../../__test_support__/fake_html_events"; +import { TermsCheckbox } from "../../front_page/terms_checkbox"; describe("", () => { - const instance = () => shallow().instance(); it("renders correctly when envs are set", () => { const oldTos = globalConfig.TOS_URL; const oldPriv = globalConfig.PRIV_URL; @@ -31,7 +31,7 @@ describe("", () => { }); it("has a setter", () => { - const tosUpdate = instance(); + const tosUpdate = shallow().instance(); tosUpdate.setState = jest.fn(); tosUpdate.set("email")(inputEvent("foo@bar.com")); expect(tosUpdate.setState).toHaveBeenCalledWith({ email: "foo@bar.com" }); @@ -47,7 +47,7 @@ describe("", () => { it("submits a form", async () => { location.assign = jest.fn(); - const i = instance(); + const i = shallow().instance(); i.setState(fake); await i.submit(fakeFormEvent); expect(fakeFormEvent.preventDefault).toHaveBeenCalled(); @@ -59,7 +59,7 @@ describe("", () => { it("errors while submitting", async () => { mockPostResponse = Promise.reject({ response: { data: ["error"] } }); - const i = instance(); + const i = shallow().instance(); i.setState(fake); await i.submit(fakeFormEvent); expect(fakeFormEvent.preventDefault).toHaveBeenCalled(); @@ -75,4 +75,31 @@ describe("", () => { ["https://farm.bot/privacy/", "https://farm.bot/tos/"] .map(string => expect(el.html()).toContain(string)); }); + + it("accepts terms", () => { + const wrapper = mount(); + const tosForm = shallow(wrapper.instance().tosForm()); + expect(wrapper.state().agree_to_terms).toBeFalsy(); + tosForm.find(TermsCheckbox).simulate("change", { + currentTarget: { checked: true } + }); + expect(wrapper.state().agree_to_terms).toBeTruthy(); + }); + + it("errors on click", () => { + const wrapper = mount(); + expect(wrapper.state().agree_to_terms).toBeFalsy(); + const tosForm = shallow(wrapper.instance().tosForm()); + tosForm.find("button").simulate("click"); + expect(error).toHaveBeenCalledWith("Please agree to the terms."); + }); + + it("doesn't error on click", () => { + const wrapper = mount(); + wrapper.setState({ agree_to_terms: true }); + expect(wrapper.state().agree_to_terms).toBeTruthy(); + const tosForm = shallow(wrapper.instance().tosForm()); + tosForm.find("button").simulate("click"); + expect(error).not.toHaveBeenCalled(); + }); });