WIP - mocking issues

pull/809/head
Rick Carlino 2018-04-25 12:45:22 -05:00
parent d001b2b9d2
commit cc822ef712
13 changed files with 103 additions and 46 deletions

View File

@ -11,7 +11,6 @@ import {
findFarmEventById,
selectAllRegimens,
selectAllSequences,
hasId,
findSequenceById,
findRegimenById,
getDeviceAccountSettings,
@ -29,6 +28,7 @@ import {
} from "../../util";
import { sourceFbosConfigValue } from "../../devices/components/source_config_value";
import { Feature } from "../../devices/interfaces";
import { hasId } from "../../resources/util";
export let formatTime = (input: string, timeOffset: number) => {
const iso = new Date(input).toISOString();

View File

@ -3,11 +3,12 @@ import { t } from "i18next";
import { error, warning } from "farmbot-toastr";
import { ReduxAction, Thunk } from "../../redux/interfaces";
import { ToggleDayParams } from "./interfaces";
import { assertUuid, findSequence, findRegimen } from "../../resources/selectors";
import { findSequence, findRegimen } from "../../resources/selectors";
import { groupRegimenItemsByWeek } from "./group_regimen_items_by_week";
import { defensiveClone } from "../../util";
import { overwrite } from "../../api/crud";
import { Actions } from "../../constants";
import { assertUuid } from "../../resources/util";
export function pushWeek() {
return {

View File

@ -10,7 +10,7 @@ import {
SpecialStatus
} from "../tagged_resources";
import { createOK } from "../actions";
import { generateUuid } from "../util";
import { generateUuid, hasId } from "../util";
import {
fakeWebcamFeed,
fakeSequence
@ -182,7 +182,7 @@ describe("toolsInUse()", () => {
describe("hasId()", () => {
it("has", () => {
const result = Selector.hasId(fakeIndex, "Sequence", 23);
const result = hasId(fakeIndex, "Sequence", 23);
expect(result).toBeTruthy();
});
});

View File

@ -23,6 +23,7 @@ import {
import { betterCompact, bail } from "../util";
import { findAllById } from "./selectors_by_id";
import { findPoints, selectAllPoints } from "./selectors_by_kind";
import { assertUuid } from "./util";
export * from "./selectors_by_id";
export * from "./selectors_by_kind";
@ -139,19 +140,6 @@ export function getSequenceByUUID(index: ResourceIndex,
}
}
export function assertUuid(expected: ResourceName, actual: string | undefined) {
if (actual && !actual.startsWith(expected)) {
console.warn(`
BAD NEWS!!! You thought this was a ${expected} UUID, but here's what it
actually was:
${actual}
`);
return false;
} else {
return true;
}
}
/** GIVEN: a slot UUID.
* FINDS: Tool in that slot (if any) */
export let currentToolInSlot = (index: ResourceIndex) =>
@ -174,10 +162,6 @@ export function toolsInUse(index: ResourceIndex): TaggedTool[] {
return findAllById(index, ids, "Tool") as TaggedTool[];
}
export function hasId(ri: ResourceIndex, k: ResourceName, id: number): boolean {
return !!ri.byKindAndId[joinKindAndId(k, id)];
}
export function maybeGetSequence(index: ResourceIndex,
uuid: string | undefined): TaggedSequence | undefined {
if (uuid) {

View File

@ -23,8 +23,8 @@ import {
} from "./tagged_resources";
import { sortResourcesById, betterCompact, bail } from "../util";
import { error } from "farmbot-toastr";
import { assertUuid } from "./selectors";
import { joinKindAndId } from "./reducer";
import { assertUuid } from "./util";
const isSaved = <T extends TaggedResource>(t: T) => t.specialStatus === SpecialStatus.SAVED;

View File

@ -13,11 +13,11 @@ import {
SlotWithTool
} from "./interfaces";
import {
assertUuid,
selectAllTools,
selectAllToolSlotPointers,
maybeFindToolById
} from "./selectors";
import { assertUuid } from "./util";
interface IndexLookupDictionary<T extends TaggedResource>
extends CowardlyDictionary<T> { }

View File

@ -14,7 +14,6 @@ import {
} from "../interfaces";
import { Peripheral } from "../controls/peripherals/interfaces";
import { User } from "../auth/interfaces";
import { assertUuid } from "./selectors";
import { DeviceAccountSettings } from "../devices/interfaces";
import { isObject, isString, get } from "lodash";
import { Image } from "../farmware/images/interfaces";
@ -25,6 +24,7 @@ import { FbosConfig } from "../config_storage/fbos_configs";
import { FirmwareConfig } from "../config_storage/firmware_configs";
import { WebAppConfig } from "../config_storage/web_app_configs";
import { FarmwareInstallation } from "../farmware/interfaces";
import { assertUuid } from "./util";
export type ResourceName =
| "Crop"

View File

@ -3,6 +3,7 @@ import { joinKindAndId } from "./reducer";
import { Dictionary } from "farmbot/dist";
import { betterCompact } from "../util";
import * as _ from "lodash";
import { ResourceIndex } from "./interfaces";
let count = 0;
export function generateUuid(id: number | undefined, kind: ResourceName) {
@ -18,3 +19,20 @@ export function entries<T>(input: Dictionary<T | undefined>): T[] {
const y = betterCompact(x);
return y;
}
export function hasId(ri: ResourceIndex, k: ResourceName, id: number): boolean {
return !!ri.byKindAndId[joinKindAndId(k, id)];
}
export function assertUuid(expected: ResourceName, actual: string | undefined) {
if (actual && !actual.startsWith(expected)) {
console.warn(`
BAD NEWS!!! You thought this was a ${expected} UUID, but here's what it
actually was:
${actual}
`);
return false;
} else {
return true;
}
}

View File

@ -0,0 +1,55 @@
import { fakeSequence } from "../../__test_support__/fake_state/resources";
const mockData = {
lastUrlChunk: "Set me",
fakeSequences: [fakeSequence()]
};
jest.mock("../../util/urls", () => {
return {
urlFriendly: jest.fn(x => x),
lastUrlChunk: jest.fn(() => mockData.lastUrlChunk)
};
});
jest.mock("../actions", () => ({ selectSequence: jest.fn() }));
jest.mock("../../resources/selectors", () => {
return {
selectAllSequences: jest.fn(() => {
return mockData.fakeSequences || [];
})
};
});
import { setActiveSequenceByName } from "../set_active_sequence_by_name";
import { selectSequence } from "../actions";
import { selectAllSequences } from "../../resources/selectors";
describe("setActiveSequenceByName", () => {
jest.useFakeTimers();
it("returns early if there is nothing to compare", () => {
mockData.lastUrlChunk = "sequences";
setActiveSequenceByName();
jest.runAllTimers();
expect(selectSequence).not.toHaveBeenCalled();
});
it("sometimes can't find a sequence by name", () => {
const body = mockData.fakeSequences[0].body;
const name = "a different value than " + body.name;
mockData.lastUrlChunk = name;
setActiveSequenceByName();
jest.runAllTimers();
expect(selectAllSequences).toHaveBeenCalled();
expect(selectSequence).not.toHaveBeenCalled();
});
it("finds a sequence by name", () => {
const body = mockData.fakeSequences[0].body;
jest.clearAllTimers();
mockData.lastUrlChunk = name;
setActiveSequenceByName();
jest.runAllTimers();
expect(true).toBe(true);
});
});

View File

@ -13,7 +13,7 @@ import { setActiveSequenceByName } from "./set_active_sequence_by_name";
@connect(mapStateToProps)
export class Sequences extends React.Component<Props, {}> {
componentWillMount() { setActiveSequenceByName("NO"); }
componentWillMount() { setActiveSequenceByName(); }
render() {
const { sequence } = this.props;

View File

@ -3,16 +3,13 @@ import { store } from "../redux/store";
import { urlFriendly, lastUrlChunk } from "../util";
import { selectSequence } from "./actions";
export function setActiveSequenceByName(_ = "") {
export function setActiveSequenceByName() {
if (lastUrlChunk() == "sequences") {
return;
}
if (lastUrlChunk() == "sequences") { return; }
selectAllSequences(store.getState().resources.index)
.map(seq => {
const name = urlFriendly(seq.body.name);
const setSequence = () => store.dispatch(selectSequence(seq.uuid));
(lastUrlChunk() === name) && setTimeout(setSequence, 150);
});
selectAllSequences(store.getState().resources.index).map(seq => {
const name = urlFriendly(seq.body.name);
const setSequence = () => store.dispatch(selectSequence(seq.uuid));
(lastUrlChunk() === name) && setTimeout(setSequence, 450);
});
}

View File

@ -0,0 +1,12 @@
import { history } from "../history";
/** When needing to reference the url in some js universally or vice versa. */
export function urlFriendly(stringToFormat: string) {
return encodeURIComponent(stringToFormat.replace(/ /gi, "_").toLowerCase());
}
/** Get remainder of current url after the last "/". */
export function lastUrlChunk() {
const p = history.getCurrentLocation().pathname;
return p.split("/")[p.split("/").length - 1];
}

View File

@ -6,7 +6,6 @@ import { box } from "boxed_value";
import {
TaggedResource, TaggedFirmwareConfig, TaggedFbosConfig
} from "../resources/tagged_resources";
import { history } from "../history";
import { BotLocationData } from "../devices/interfaces";
import { FirmwareConfig } from "../config_storage/firmware_configs";
import { FbosConfig } from "../config_storage/fbos_configs";
@ -141,16 +140,7 @@ export function shortRevision() {
return (globalConfig.SHORT_REVISION || "NONE").slice(0, 8);
}
/** When needing to reference the url in some js universally or vice versa. */
export function urlFriendly(stringToFormat: string) {
return encodeURIComponent(stringToFormat.replace(/ /gi, "_").toLowerCase());
}
/** Get remainder of current url after the last "/". */
export function lastUrlChunk() {
const p = history.getCurrentLocation().pathname;
return p.split("/")[p.split("/").length - 1];
}
export * from "./urls";
export const trim = (i: string): string => i.replace(/\s+/g, " ");