add step tests

pull/1652/head
gabrielburnworth 2020-01-03 12:55:29 -08:00
parent a10267507b
commit 8b8b198d93
2 changed files with 55 additions and 31 deletions

View File

@ -1,6 +1,13 @@
let mockStep = {} as SendMessage;
jest.mock("../../../api/crud", () => ({
editStep: jest.fn(x => x.executor(mockStep)),
}));
import * as React from "react";
import { TileSendMessage, RefactoredSendMessage } from "../tile_send_message";
import { mount } from "enzyme";
import {
TileSendMessage, RefactoredSendMessage, SendMessageParams
} from "../tile_send_message";
import { mount, shallow } from "enzyme";
import { fakeSequence } from "../../../__test_support__/fake_state/resources";
import { SendMessage, Channel } from "farmbot/dist";
import { channel } from "../tile_send_message_support";
@ -8,7 +15,7 @@ import { emptyState } from "../../../resources/reducer";
import { MessageType } from "../../interfaces";
describe("<TileSendMessage/>", () => {
function props() {
const fakeProps = (): SendMessageParams => {
const currentStep: SendMessage = {
kind: "send_message",
args: {
@ -31,16 +38,17 @@ describe("<TileSendMessage/>", () => {
resources: emptyState().index,
confirmStepDeletion: false,
};
}
};
function bootstrapTest() {
return {
component: mount(<TileSendMessage {...props()} />)
};
}
it("throws error upon wrong step type", () => {
const p = fakeProps();
p.currentStep.kind = "nope" as SendMessage["kind"];
expect(() => shallow(<TileSendMessage {...p} />))
.toThrowError("TileSendMessage expects send_message");
});
it("renders inputs", () => {
const block = bootstrapTest().component;
const block = mount(<TileSendMessage {...fakeProps()} />);
const inputs = block.find("input");
const labels = block.find("label");
const buttons = block.find("button");
@ -72,14 +80,37 @@ describe("<TileSendMessage/>", () => {
});
it("adds and removes channels", () => {
const i = new RefactoredSendMessage(props());
const i = new RefactoredSendMessage(fakeProps());
const addEmail = i.add("email");
const removeEmail = i.remove("email");
const { currentStep } = i.props;
currentStep.body = [];
addEmail(currentStep);
expect(currentStep.body).toContainEqual(channel("email"));
removeEmail(currentStep);
expect(currentStep.body).not.toContainEqual(channel("email"));
});
it("adds and removes channels via toggle", () => {
const i = new RefactoredSendMessage(fakeProps());
delete i.props.currentStep.body;
mockStep = i.props.currentStep;
i.toggle("email")();
expect(mockStep.body).toContainEqual(channel("email"));
i.toggle("email")();
expect(mockStep.body).not.toContainEqual(channel("email"));
});
it("sets message type", () => {
const i = new RefactoredSendMessage(fakeProps());
mockStep = i.props.currentStep;
i.setMsgType({ label: "", value: "fun" });
expect(mockStep.args.message_type).toEqual("fun");
});
it("doesn't set incorrect message type", () => {
const i = new RefactoredSendMessage(fakeProps());
mockStep = i.props.currentStep;
expect(() => i.setMsgType({ label: "", value: "nope" }))
.toThrowError("message_type must be one of ALLOWED_MESSAGE_TYPES.");
});
});

View File

@ -29,7 +29,7 @@ export function TileSendMessage(props: StepParams) {
}
}
interface SendMessageParams {
export interface SendMessageParams {
currentStep: SendMessage;
currentSequence: TaggedSequence;
dispatch: Function;
@ -40,19 +40,12 @@ interface SendMessageParams {
export class RefactoredSendMessage
extends React.Component<SendMessageParams, {}> {
get args() { return this.props.currentStep.args; }
get message() { return this.args.message; }
get message_type() { return this.args.message_type; }
get step() { return this.props.currentStep; }
get dispatch() { return this.props.dispatch; }
get sequence() { return this.props.currentSequence; }
get index() { return this.props.index; }
get currentSelection() {
return MESSAGE_STATUSES_DDI[this.message_type];
return MESSAGE_STATUSES_DDI[this.props.currentStep.args.message_type];
}
get channels() {
return (this.step.body || []).map(x => x.args.channel_name);
return (this.props.currentStep.body || []).map(x => x.args.channel_name);
}
hasChannel = (name: ChannelName) => {
@ -69,19 +62,19 @@ export class RefactoredSendMessage
}
toggle = (n: ChannelName) => () => {
this.dispatch(editStep({
sequence: this.sequence,
step: this.step,
index: this.index,
this.props.dispatch(editStep({
sequence: this.props.currentSequence,
step: this.props.currentStep,
index: this.props.index,
executor: this.hasChannel(n) ? this.remove(n) : this.add(n)
}));
}
setMsgType = (x: DropDownItem) => {
this.dispatch(editStep({
sequence: this.sequence,
step: this.step,
index: this.index,
this.props.dispatch(editStep({
sequence: this.props.currentSequence,
step: this.props.currentStep,
index: this.props.index,
executor: (step: SendMessage) => {
if (isMessageType(x.value)) {
step.args.message_type = x.value;
@ -110,7 +103,7 @@ export class RefactoredSendMessage
<Col xs={12}>
<label>{t("Message")}</label>
<span className="char-limit">
{this.message.length}/300
{this.props.currentStep.args.message.length}/300
</span>
<StepInputBox dispatch={dispatch}
step={currentStep}