Add tests for FarmwareReducer

pull/356/head
Rick Carlino 2017-07-27 09:34:52 -05:00
parent 2deab5ea67
commit e1756d3d84
3 changed files with 67 additions and 36 deletions

View File

@ -1,51 +1,52 @@
import { Everything } from "../../interfaces";
import { buildResourceIndex } from "../resource_index_builder";
import { TaggedFarmEvent, TaggedSequence, TaggedRegimen } from "../../resources/tagged_resources";
import { TaggedFarmEvent, TaggedSequence, TaggedRegimen, TaggedImage } from "../../resources/tagged_resources";
import { ExecutableType } from "../../farm_designer/interfaces";
import { fakeResource } from "../fake_resource";
export let resources: Everything["resources"] = buildResourceIndex();
export function fakeSequence(): TaggedSequence {
return {
"kind": "sequences",
uuid: "---",
"body": {
args: { version: 4 },
id: 12,
color: "red",
name: "fake",
kind: "sequence",
body: []
}
};
return fakeResource("sequences", {
args: { version: 4 },
id: 12,
color: "red",
name: "fake",
kind: "sequence",
body: []
});
}
export function fakeRegimen(): TaggedRegimen {
return {
uuid: "Whatever",
kind: "regimens",
body: {
name: "Foo",
color: "red",
regimen_items: []
}
};
return fakeResource("regimens", {
name: "Foo",
color: "red",
regimen_items: []
});
}
export function fakeFarmEvent(exe_type: ExecutableType,
exe_id: number): TaggedFarmEvent {
return {
"kind": "farm_events",
uuid: "---",
"body": {
"id": 21,
"start_time": "2017-05-22T05:00:00.000Z",
"end_time": "2017-05-30T05:00:00.000Z",
"repeat": 1,
"time_unit": "never",
"executable_id": exe_id,
"executable_type": exe_type,
"calendar": []
}
};
return fakeResource("farm_events", {
"id": 21,
"start_time": "2017-05-22T05:00:00.000Z",
"end_time": "2017-05-30T05:00:00.000Z",
"repeat": 1,
"time_unit": "never",
"executable_id": exe_id,
"executable_type": exe_type,
"calendar": []
});
}
export function fakeImage(): TaggedImage {
return fakeResource("images", {
id: 23,
device_id: 46,
attachment_processed_at: undefined,
updated_at: new Date().toISOString(),
created_at: new Date().toISOString(),
attachment_url: "https://i.redd.it/xz0e2kinm4cz.jpg",
meta: { x: 0, y: 0, z: 0 }
});
}

View File

@ -0,0 +1,30 @@
import { famrwareReducer } from "../reducer";
import { FarmwareState } from "../interfaces";
import { Actions } from "../../constants";
import { fakeImage } from "../../__test_support__/fake_state/resources";
describe("famrwareReducer", () => {
it("Removes UUIDs from state on deletion", () => {
let image = fakeImage();
let oldState: FarmwareState = { currentImage: image.uuid };
let newState = famrwareReducer(oldState, {
type: Actions.DESTROY_RESOURCE_OK,
payload: image
});
expect(oldState.currentImage).not.toEqual(newState.currentImage);
expect(newState.currentImage).toBeUndefined();
});
it("adds UUID to state on SELECT_IMAGE", () => {
let image = fakeImage();
let oldState: FarmwareState = { currentImage: undefined };
let newState = famrwareReducer(oldState, {
type: Actions.SELECT_IMAGE,
payload: image.uuid
});
expect(oldState.currentImage).not.toEqual(newState.currentImage);
expect(newState.currentImage).not.toBeUndefined();
expect(newState.currentImage).toBe(image.uuid);
});
});