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";
import { IPC_TAGHISTORYUPDATE } from "../../app/src/actions/actions_taghistory";

describe("TagHistory", () => {
	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 = { tagName: "val_IntakePressure", value: 100 };
			expect(_.map(TagHistoryReducer({}, action), (x) => x)).toHaveLength(1);
		});

		it("should add another value to an existing history state", () => {
			action.payload = { tagName: "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 = { tagName: "test", value: 100 };
			expect(_.map(TagHistoryReducer({}, action), (x) => x)).toHaveLength(0);
		});
	});

	describe("IPC_TAGHISTORYUPDATE", () => {
		const action = { 
			type: IPC_TAGHISTORYUPDATE,
			payload: ""
		};

		it("should add to the tagHistory state", () => {
			action.payload = {tagName: "test", historyRows: [{value: 100, timestamp: new Date(0)}]};
			const stateAfterAdd = TagHistoryReducer({}, action);
			expect(stateAfterAdd).toEqual( {test : [{value: 100, timestamp: new Date(0)}]});
		});
	});
	
});