From 58a4572666e48c572960cf8d83c12212c1df576c Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Fri, 8 Dec 2017 08:38:47 -0600 Subject: [PATCH] Edge case and tests for extractParent() --- .../__tests__/password_reset_test.tsx | 3 +- webpack/password_reset/interfaces.ts | 8 --- webpack/password_reset/password_reset.tsx | 10 +++- webpack/regimens/__tests__/actions_test.ts | 3 +- .../sequences/__tests__/locals_list_test.tsx | 50 +++++++++++++++++++ webpack/sequences/locals_list.tsx | 8 ++- 6 files changed, 67 insertions(+), 15 deletions(-) delete mode 100644 webpack/password_reset/interfaces.ts create mode 100644 webpack/sequences/__tests__/locals_list_test.tsx diff --git a/webpack/password_reset/__tests__/password_reset_test.tsx b/webpack/password_reset/__tests__/password_reset_test.tsx index d2af45666..56c365ba5 100644 --- a/webpack/password_reset/__tests__/password_reset_test.tsx +++ b/webpack/password_reset/__tests__/password_reset_test.tsx @@ -1,8 +1,7 @@ import * as React from "react"; import { mount } from "enzyme"; -import { PasswordReset } from "../password_reset"; +import { PasswordReset, State } from "../password_reset"; import * as moxios from "moxios"; -import { State } from "../interfaces"; describe("", () => { beforeEach(function () { diff --git a/webpack/password_reset/interfaces.ts b/webpack/password_reset/interfaces.ts deleted file mode 100644 index b145aa8e3..000000000 --- a/webpack/password_reset/interfaces.ts +++ /dev/null @@ -1,8 +0,0 @@ -export interface State { - password?: string; - passwordConfirmation?: string; - serverURL?: string; - serverPort?: string; -} - -export interface Props { } diff --git a/webpack/password_reset/password_reset.tsx b/webpack/password_reset/password_reset.tsx index 9df970ba8..f67692289 100644 --- a/webpack/password_reset/password_reset.tsx +++ b/webpack/password_reset/password_reset.tsx @@ -4,10 +4,18 @@ import { t } from "i18next"; import { error as log, init as logInit } from "farmbot-toastr"; import { prettyPrintApiErrors } from "../util"; import { API } from "../api"; -import { State, Props } from "./interfaces"; import { Widget, WidgetHeader, WidgetBody, Row, Col } from "../ui/index"; import { Session } from "../session"; +export interface State { + password?: string; + passwordConfirmation?: string; + serverURL?: string; + serverPort?: string; +} + +export interface Props { } + export class PasswordReset extends React.Component { constructor() { super(); diff --git a/webpack/regimens/__tests__/actions_test.ts b/webpack/regimens/__tests__/actions_test.ts index 7fe259895..7f61008a2 100644 --- a/webpack/regimens/__tests__/actions_test.ts +++ b/webpack/regimens/__tests__/actions_test.ts @@ -3,8 +3,7 @@ import { fakeRegimen } from "../../__test_support__/fake_state/resources"; import { Actions } from "../../constants"; import { fakeState } from "../../__test_support__/fake_state"; import { buildResourceIndex } from "../../__test_support__/resource_index_builder"; -import { SpecialStatus, TaggedRegimen } from "../../resources/tagged_resources"; -import { ReduxAction } from "../../redux/interfaces"; +import { SpecialStatus } from "../../resources/tagged_resources"; describe("editRegimen()", () => { it("doesn't call edit", () => { diff --git a/webpack/sequences/__tests__/locals_list_test.tsx b/webpack/sequences/__tests__/locals_list_test.tsx new file mode 100644 index 000000000..0d59fd026 --- /dev/null +++ b/webpack/sequences/__tests__/locals_list_test.tsx @@ -0,0 +1,50 @@ +import { extractParent } from "../locals_list"; +import { VariableDeclaration, ParameterDeclaration } from "farmbot"; + +describe("extractParent()", () => { + const irrelevant: VariableDeclaration = { + kind: "variable_declaration", + args: { + label: "nope", + data_value: { kind: "coordinate", args: { x: 0, y: 0, z: 0 } } + } + }; + + const bad: ParameterDeclaration = { + kind: "parameter_declaration", + args: { label: "parent", data_type: "coordinate" } + }; + + const good: VariableDeclaration = { + kind: "variable_declaration", + args: { + label: "parent", + data_value: { kind: "coordinate", args: { x: 0, y: 0, z: 0 } } + } + }; + + it("returns undefined on empty arrays", () => { + expect(extractParent([])).toBeUndefined(); + expect(extractParent()).toBeUndefined(); + }); + + it("returns undefined on arrays that don't have a parent", () => { + expect(extractParent([irrelevant])).toBeUndefined(); + }); + + it("only returns parent if it is a VARIABLE declaration.", () => { + expect(extractParent([bad])).toBeUndefined(); + }); + + it("returns parent when there is one", () => { + const list = [bad, good, irrelevant]; + const q = extractParent; + const result = extractParent(list); + debugger; + expect(result).toBeTruthy(); + if (result) { + expect(result.kind).toBe("variable_declaration"); + expect(result.args.label).toBe("parent"); + } + }); +}); diff --git a/webpack/sequences/locals_list.tsx b/webpack/sequences/locals_list.tsx index 77f6ada4d..9dd1e50f6 100644 --- a/webpack/sequences/locals_list.tsx +++ b/webpack/sequences/locals_list.tsx @@ -37,9 +37,13 @@ interface ParentVariableFormProps { /** Given an array of variable declarations (or undefined), finds the "parent" * special identifier */ -const extractParent = +export const extractParent = (list?: LocalVariable[]): VariableDeclaration | undefined => { - const p = list && list.filter(x => x.args.label === "parent")[0]; + const p = (list || []).filter(x => { + const isParent = x.args.label === "parent"; + const isVar = x.kind === "variable_declaration"; + return isVar && isParent; + })[0]; return (p && p.kind === "variable_declaration") ? p : undefined; };