Edge case and tests for extractParent()

This commit is contained in:
Rick Carlino 2017-12-08 08:38:47 -06:00
parent bf259815ef
commit 58a4572666
6 changed files with 67 additions and 15 deletions

View file

@ -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("<PasswordReset/>", () => {
beforeEach(function () {

View file

@ -1,8 +0,0 @@
export interface State {
password?: string;
passwordConfirmation?: string;
serverURL?: string;
serverPort?: string;
}
export interface Props { }

View file

@ -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<Props, State> {
constructor() {
super();

View file

@ -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", () => {

View file

@ -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");
}
});
});

View file

@ -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;
};