👏 Fix findSlotByToolId() + test cases

pull/343/head
Rick Carlino 2017-07-17 14:26:27 -05:00
parent 5fa1edfbce
commit 7d96ca2b5f
2 changed files with 42 additions and 25 deletions

View File

@ -2,25 +2,50 @@ import { buildResourceIndex } from "../../__test_support__/resource_index_builde
import { findSlotByToolId } from "../selectors";
import { resourceReducer } from "../reducer";
import { Actions } from "../../constants";
import { init } from "../../api/crud";
import { TaggedTool } from "../tagged_resources";
import { TaggedTool, TaggedToolSlotPointer } from "../tagged_resources";
import { createOK } from "../actions";
import { generateUuid } from "../util";
const TOOL_ID = 99;
const SLOT_ID = 100;
const fakeTool: TaggedTool = {
kind: "tools",
uuid: generateUuid(TOOL_ID, "tools"),
body: {
name: "yadda yadda",
id: TOOL_ID
}
};
const fakeSlot: TaggedToolSlotPointer = {
kind: "points",
uuid: generateUuid(SLOT_ID, "points"),
body: {
tool_id: TOOL_ID,
pointer_type: "ToolSlot",
radius: 0,
x: 0,
y: 0,
z: 0,
name: "wow",
pointer_id: SLOT_ID,
meta: {}
}
};
describe("findSlotByToolId", () => {
it("returns undefined when not found", () => {
const ID = 99;
let fakeTool: TaggedTool = {
kind: "tools",
uuid: generateUuid(ID, "tools"),
body: {
name: "yadda yadda",
id: ID
}
};
let state = resourceReducer(buildResourceIndex(), createOK(fakeTool));
expect(state.index.byKindAndId["tools." + fakeTool.body.id]);
let result = findSlotByToolId(state.index, ID);
let result = findSlotByToolId(state.index, TOOL_ID);
expect(result).toBeFalsy();
});
it("returns something when there is a match", () => {
let initialState = buildResourceIndex();
let state = [createOK(fakeTool), createOK(fakeSlot)]
.reduce(resourceReducer, initialState);
let result = findSlotByToolId(state.index, TOOL_ID);
expect(result).toBeTruthy();
if (result) { expect(result.kind).toBe("points"); }
});
});

View File

@ -300,12 +300,6 @@ export function toArray(index: ResourceIndex) {
});
}
/** Search for matching key/value pairs in the body of a resource. */
export function where(index: ResourceIndex,
body: object): (TaggedResource | undefined)[] {
return _.filter(index.references, body);
}
/** GIVEN: a slot UUID.
* FINDS: Tool in that slot (if any) */
export let currentToolInSlot = (index: ResourceIndex) =>
@ -385,7 +379,6 @@ export let findSequenceById = (ri: ResourceIndex, sequence_id: number) => {
}
};
export let findRegimenById = (ri: ResourceIndex, regimen_id: number) => {
let regimen = byId("regimens")(ri, regimen_id);
if (regimen && isTaggedRegimen(regimen) && sanityCheck(regimen)) {
@ -399,12 +392,11 @@ export let findSlotById = byId<TaggedToolSlotPointer>("points");
/** Find a Tool's corresponding Slot. */
export let findSlotByToolId = (index: ResourceIndex, tool_id: number) => {
let tool = findToolById(index, tool_id);
let filter = (x: TaggedResource) => {
if (x && isTaggedToolSlotPointer(x)) {
return x.body.tool_id === tool_id;
}
};
let tts = where(index, { tool_id: tool.body.id }).filter(filter)[0];
let query: any = { body: { tool_id: tool.body.id } };
let every = Object
.keys(index.references)
.map(x => index.references[x]);
let tts = _.find(every, query);
if (tts && isTaggedToolSlotPointer(tts) && sanityCheck(tts)) {
return tts;
} else {