Adds tests for action creators and reducers
This commit is contained in:
89
__tests__/actions/actions_plc.test.js
Normal file
89
__tests__/actions/actions_plc.test.js
Normal file
@@ -0,0 +1,89 @@
|
||||
import { configure } from "enzyme";
|
||||
import Adapter from "enzyme-adapter-react-16";
|
||||
import { setPlcIpAddress, SET_PLC_IPADDRESS, ipcPlcInitializeSend, INITIALIZE_PLC, ipcPlcDetailsReceived, PLC_DATA_RECEIVED, ipcPlcErrorReceived, PLC_ERROR_RECEIVED } from "../../app/src/actions/actions_plc";
|
||||
import { ipcRenderer } from "electron";
|
||||
configure({ adapter: new Adapter() });
|
||||
|
||||
describe("actions_plc", () => {
|
||||
describe("setPlcIpAddress", () => {
|
||||
let action;
|
||||
|
||||
beforeEach(() => {
|
||||
action = setPlcIpAddress("192.168.1.10");
|
||||
});
|
||||
|
||||
|
||||
it("has the correct type", () => {
|
||||
expect(action.type).toEqual(SET_PLC_IPADDRESS);
|
||||
});
|
||||
|
||||
it("has the correct payload", () => {
|
||||
expect(action.payload).toEqual("192.168.1.10");
|
||||
});
|
||||
});
|
||||
|
||||
describe("ipcPlcInitializeSend", () => {
|
||||
let action;
|
||||
const tagList = {
|
||||
test: { name: "test", value: 100.0 }
|
||||
};
|
||||
beforeEach(() => {
|
||||
action = ipcPlcInitializeSend("192.168.1.10", tagList);
|
||||
});
|
||||
|
||||
it("has the correct type", () => {
|
||||
expect(action.type).toEqual(INITIALIZE_PLC);
|
||||
});
|
||||
|
||||
it("has the correct payload", () => {
|
||||
expect(action.payload).toBeTruthy();
|
||||
});
|
||||
|
||||
it("triggers the ipcRenderer.send function", () => {
|
||||
expect(ipcRenderer.send).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe("ipcPlcDetailsReceived", () => {
|
||||
let action;
|
||||
const plcData = {
|
||||
ipAddress: "192.168.1.10",
|
||||
details: {
|
||||
name: "CLX"
|
||||
}
|
||||
};
|
||||
beforeEach(() => {
|
||||
action = ipcPlcDetailsReceived(undefined, plcData);
|
||||
});
|
||||
|
||||
|
||||
it("has the correct type", () => {
|
||||
expect(action.type).toEqual(PLC_DATA_RECEIVED);
|
||||
});
|
||||
|
||||
it("has the correct payload", () => {
|
||||
expect(action.payload).toEqual({
|
||||
ipAddress: "192.168.1.10",
|
||||
details: {
|
||||
name: "CLX"
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("ipcPlcErrorReceived", () => {
|
||||
let action;
|
||||
beforeEach(() => {
|
||||
action = ipcPlcErrorReceived(undefined, "ERROR MESSAGE!");
|
||||
});
|
||||
|
||||
it("has the correct type", () => {
|
||||
expect(action.type).toEqual(PLC_ERROR_RECEIVED);
|
||||
});
|
||||
|
||||
it("has the correct payload", () => {
|
||||
expect(action.payload).toEqual("ERROR MESSAGE!");
|
||||
});
|
||||
});
|
||||
});
|
||||
112
__tests__/actions/actions_tags.test.js
Normal file
112
__tests__/actions/actions_tags.test.js
Normal file
@@ -0,0 +1,112 @@
|
||||
import { configure } from "enzyme";
|
||||
import Adapter from "enzyme-adapter-react-16";
|
||||
import { ipcRenderer } from "electron";
|
||||
import { ipcTagUpdate, IPC_TAGSYNC, IPC_TAGUPDATE, ipcTagSync, storeNewTag, STORE_NEW_TAG, deleteTag, DELETE_TAG, writeTag, WRITE_TAG } from "../../app/src/actions/actions_tags";
|
||||
configure({ adapter: new Adapter() });
|
||||
|
||||
describe("actions_tags", () => {
|
||||
describe("ipcTagUpdate", () => {
|
||||
let action;
|
||||
|
||||
const sampleTag = { state: {
|
||||
tag: {
|
||||
name: "test",
|
||||
value: 100.0
|
||||
}
|
||||
}};
|
||||
|
||||
beforeEach(() => {
|
||||
action = ipcTagUpdate(undefined, sampleTag);
|
||||
});
|
||||
|
||||
it("has the correct type", () => {
|
||||
expect(action.type).toEqual(IPC_TAGUPDATE);
|
||||
});
|
||||
|
||||
it("has the correct payload", () => {
|
||||
expect(action.payload).toEqual({ name: "test", value: 100.0 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("ipcTagSync", () => {
|
||||
let action;
|
||||
|
||||
const sampleTag = { test: {
|
||||
name: "test",
|
||||
value: 100.0
|
||||
}};
|
||||
|
||||
beforeEach(() => {
|
||||
action = ipcTagSync("192.168.1.10", sampleTag);
|
||||
});
|
||||
|
||||
it("has the correct type", () => {
|
||||
expect(action.type).toEqual(IPC_TAGSYNC);
|
||||
});
|
||||
|
||||
it("has the correct payload", () => {
|
||||
expect(action.payload).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should execute the ipcRenderer.send function", () => {
|
||||
expect(ipcRenderer.send).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("storeNewTag", () => {
|
||||
let action;
|
||||
|
||||
const sampleTag = {
|
||||
name: "test",
|
||||
value: 100.0
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
action = storeNewTag(sampleTag);
|
||||
});
|
||||
|
||||
it("has the correct type", () => {
|
||||
expect(action.type).toEqual(STORE_NEW_TAG);
|
||||
});
|
||||
|
||||
it("has the correct payload", () => {
|
||||
expect(action.payload).toEqual({ name: "test", value: 100.0 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteTag", () => {
|
||||
let action;
|
||||
|
||||
beforeEach(() => {
|
||||
action = deleteTag("test");
|
||||
});
|
||||
|
||||
it("has the correct type", () => {
|
||||
expect(action.type).toEqual(DELETE_TAG);
|
||||
});
|
||||
|
||||
it("has the correct payload", () => {
|
||||
expect(action.payload).toEqual("test");
|
||||
});
|
||||
});
|
||||
|
||||
describe("writeTag", () => {
|
||||
let action;
|
||||
|
||||
beforeEach(() => {
|
||||
action = writeTag("test", 100.0);
|
||||
});
|
||||
|
||||
it("has the correct type", () => {
|
||||
expect(action.type).toEqual(WRITE_TAG);
|
||||
});
|
||||
|
||||
it("has the correct payload", () => {
|
||||
expect(action.payload).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should execute the ipcRenderer.send function", () => {
|
||||
expect(ipcRenderer.send).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
44
__tests__/reducers/reducer_alarm.test.js
Normal file
44
__tests__/reducers/reducer_alarm.test.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import { configure } from "enzyme";
|
||||
import Adapter from "enzyme-adapter-react-16";
|
||||
configure({ adapter: new Adapter() });
|
||||
|
||||
import AlarmReducer from "../../app/src/reducers/reducer_alarm";
|
||||
import { IPC_TAGUPDATE } from "../../app/src/actions/actions_tags";
|
||||
|
||||
describe("reducer_alarm", () => {
|
||||
it("should not change state on unused type", () => {
|
||||
expect(AlarmReducer(["test", "test"], "test")).toEqual(["test", "test"]);
|
||||
});
|
||||
|
||||
it("should return a default empty object", () => {
|
||||
expect(AlarmReducer(undefined, "test")).toEqual([]);
|
||||
});
|
||||
|
||||
describe("IPC_TAGUPDATE", () => {
|
||||
let action = {
|
||||
type: IPC_TAGUPDATE,
|
||||
payload: ""
|
||||
};
|
||||
|
||||
it("should not change active alarms if tag not in alarm list", () => {
|
||||
action.payload = { name: "test", value: false };
|
||||
expect(AlarmReducer([], action)).toEqual([]);
|
||||
});
|
||||
|
||||
it("should add the tag to active alarms if alarm tag value is true", () => {
|
||||
action.payload = { name: "alarm_ESTOP", value: true };
|
||||
expect(AlarmReducer([], action)).toEqual(["alarm_ESTOP"]);
|
||||
});
|
||||
|
||||
it("should not change active alarms if alarm tag value is false", () => {
|
||||
action.payload = { name: "alarm_ESTOP", value: false };
|
||||
expect(AlarmReducer(["test"], action)).toEqual(["test"]);
|
||||
});
|
||||
|
||||
it("should remove the event from the list if alarm tag value is false", () => {
|
||||
action.payload = { name: "alarm_ESTOP", value: false };
|
||||
expect(AlarmReducer(["alarm_ESTOP"], action)).toEqual([]);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
39
__tests__/reducers/reducer_events.test.js
Normal file
39
__tests__/reducers/reducer_events.test.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import { configure } from "enzyme";
|
||||
import Adapter from "enzyme-adapter-react-16";
|
||||
configure({ adapter: new Adapter() });
|
||||
|
||||
import EventsReducer from "../../app/src/reducers/reducer_events";
|
||||
import { IPC_TAGUPDATE } from "../../app/src/actions/actions_tags";
|
||||
|
||||
describe("reducer_events", () => {
|
||||
it("should not change state on unused type", () => {
|
||||
expect(EventsReducer(["test", "test"], "test")).toEqual(["test", "test"]);
|
||||
});
|
||||
|
||||
it("should return a default empty object", () => {
|
||||
expect(EventsReducer(undefined, "test")).toEqual([]);
|
||||
});
|
||||
|
||||
describe("IPC_TAGUPDATE", () => {
|
||||
let action = {
|
||||
type: IPC_TAGUPDATE,
|
||||
payload: ""
|
||||
};
|
||||
|
||||
it("should add an event to the event log for an event tag true", () => {
|
||||
action.payload = { name: "cmd_Start", value: true };
|
||||
const newState = EventsReducer([], action);
|
||||
expect(newState[0].tag).toEqual("cmd_Start");
|
||||
});
|
||||
|
||||
it("should return unaltered event log if not in event log", () => {
|
||||
action.payload = { name: "test", value: true };
|
||||
expect(EventsReducer([], action)).toEqual([]);
|
||||
});
|
||||
|
||||
it("should return unaltered event log if event tag value false", () => {
|
||||
action.payload = { name: "cmd_Start", value: false };
|
||||
expect(EventsReducer([], action)).toEqual([]);
|
||||
});
|
||||
});
|
||||
});
|
||||
52
__tests__/reducers/reducer_plc.test.js
Normal file
52
__tests__/reducers/reducer_plc.test.js
Normal file
@@ -0,0 +1,52 @@
|
||||
import { configure } from "enzyme";
|
||||
import Adapter from "enzyme-adapter-react-16";
|
||||
configure({ adapter: new Adapter() });
|
||||
|
||||
import PlcReducer from "../../app/src/reducers/reducer_plc";
|
||||
import { SET_PLC_IPADDRESS, PLC_DATA_RECEIVED, PLC_ERROR_RECEIVED } from "../../app/src/actions/actions_plc";
|
||||
|
||||
describe("PlcReducer", () => {
|
||||
it("should not change state on unused type", () => {
|
||||
expect(PlcReducer({test: "test"}, {})).toEqual({test: "test"});
|
||||
});
|
||||
|
||||
it("should return a default empty object", () => {
|
||||
expect(PlcReducer(undefined, "test")).toEqual({});
|
||||
});
|
||||
|
||||
describe("SET_PLC_IPADDRESS", ()=>{
|
||||
const action = {
|
||||
type: SET_PLC_IPADDRESS,
|
||||
payload: "192.168.1.10"
|
||||
};
|
||||
|
||||
it("should add ip address to state", ()=> {
|
||||
const state = PlcReducer({}, action);
|
||||
expect(state.ipAddress).toEqual(action.payload);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PLC_DATA_RECEIVED", ()=>{
|
||||
const action = {
|
||||
type: PLC_DATA_RECEIVED,
|
||||
payload: {name: "test"}
|
||||
};
|
||||
|
||||
it("should add details to state", ()=> {
|
||||
const state = PlcReducer({}, action);
|
||||
expect(state).toEqual(action.payload);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PLC_ERROR_RECEIVED", ()=>{
|
||||
const action = {
|
||||
type: PLC_ERROR_RECEIVED,
|
||||
payload: "PLC ERROR!"
|
||||
};
|
||||
|
||||
it("should add details to state", ()=> {
|
||||
const state = PlcReducer({}, action);
|
||||
expect(state.error).toEqual("PLC ERROR!");
|
||||
});
|
||||
});
|
||||
});
|
||||
41
__tests__/reducers/reducer_taghistory.test.js
Normal file
41
__tests__/reducers/reducer_taghistory.test.js
Normal file
@@ -0,0 +1,41 @@
|
||||
import { configure } from "enzyme";
|
||||
import Adapter from "enzyme-adapter-react-16";
|
||||
import _ from "lodash";
|
||||
configure({ adapter: new Adapter() });
|
||||
|
||||
import TagHistoryReducer from "../../app/src/reducers/reducer_taghistory";
|
||||
import { IPC_TAGUPDATE } from "../../app/src/actions/actions_tags";
|
||||
|
||||
describe("PlcReducer", () => {
|
||||
it("should not change state on unused type", () => {
|
||||
expect(TagHistoryReducer({test: "test"}, "test")).toEqual({test: "test"});
|
||||
});
|
||||
|
||||
it("should return a default empty object", () => {
|
||||
expect(TagHistoryReducer(undefined, "test")).toEqual({});
|
||||
});
|
||||
|
||||
describe("IPC_TAGUPDATE", () => {
|
||||
const action = {
|
||||
type: IPC_TAGUPDATE,
|
||||
payload: ""
|
||||
};
|
||||
|
||||
it("should add a history tag to an empty state", () => {
|
||||
action.payload = { name: "val_IntakePressure", value: 100 };
|
||||
expect(_.map(TagHistoryReducer({}, action), (x) => x)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("should add another value to an existing history state", () => {
|
||||
action.payload = { name: "val_IntakePressure", value: 100 };
|
||||
const stateAfterAdd = TagHistoryReducer({}, action);
|
||||
expect(_.map(TagHistoryReducer(stateAfterAdd, action), (x) => x)[0]).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("should not add to the history if the tag is not a historical tag", () => {
|
||||
action.payload = { name: "test", value: 100 };
|
||||
expect(_.map(TagHistoryReducer({}, action), (x) => x)).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
63
__tests__/reducers/reducer_tags.test.js
Normal file
63
__tests__/reducers/reducer_tags.test.js
Normal file
@@ -0,0 +1,63 @@
|
||||
import { configure } from "enzyme";
|
||||
import Adapter from "enzyme-adapter-react-16";
|
||||
configure({ adapter: new Adapter() });
|
||||
|
||||
import TagsReducer from "../../app/src/reducers/reducer_tags";
|
||||
import { IPC_TAGUPDATE, STORE_NEW_TAG, DELETE_TAG } from "../../app/src/actions/actions_tags";
|
||||
|
||||
describe("PlcReducer", () => {
|
||||
it("should not change state on unused type", () => {
|
||||
expect(TagsReducer({test: "test"}, "test")).toEqual({test: "test"});
|
||||
});
|
||||
|
||||
it("should return a default empty object", () => {
|
||||
expect(TagsReducer(undefined, "test")).toEqual({});
|
||||
});
|
||||
|
||||
describe("IPC_TAGUPDATE", () => {
|
||||
const action = {
|
||||
type: IPC_TAGUPDATE,
|
||||
payload: { name: "test", value: 111.111 }
|
||||
};
|
||||
|
||||
it("should store a new value for a new tag", () => {
|
||||
const newState = TagsReducer({}, action);
|
||||
expect(newState).toEqual({ test: { name: "test", value: 111.111 }});
|
||||
});
|
||||
|
||||
it("should store a new value for an existing tag", () => {
|
||||
const existingState = { test: { name: "test", value: 0.00 }};
|
||||
const newState = TagsReducer(existingState, action);
|
||||
expect(newState).toEqual({test: { name: "test", value: 111.111 }});
|
||||
});
|
||||
});
|
||||
|
||||
describe("STORE_NEW_TAG", () => {
|
||||
const action = {
|
||||
type: STORE_NEW_TAG,
|
||||
payload: "test"
|
||||
};
|
||||
|
||||
it("should create a new object for a new tag", () => {
|
||||
const newState = TagsReducer({}, action);
|
||||
expect(newState).toEqual({ test: { name: "test" }});
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE_TAG", () => {
|
||||
const action = {
|
||||
type: DELETE_TAG,
|
||||
payload: "test"
|
||||
};
|
||||
|
||||
it("should remove the object in state with the key in the payload", () => {
|
||||
const initialState = {
|
||||
test: { name: "test", value: 10.0 },
|
||||
remain: { name: "remain", value: 1000 }
|
||||
};
|
||||
const newState = TagsReducer(initialState, action);
|
||||
expect(newState).toEqual({ remain: { name: "remain", value: 1000 } });
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user