Skip to content

Commit

Permalink
Add input message
Browse files Browse the repository at this point in the history
  • Loading branch information
ziiw committed May 26, 2020
1 parent 5991af8 commit 076b9c2
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/components/MessageInput/MessageInput.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.MessageInput {
display: flex;
flex-direction: row;
padding: 1em;
background-color: grey;
}
42 changes: 42 additions & 0 deletions src/components/MessageInput/MessageInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React, { useState } from "react";
import "./MessageInput.css";

const MessageInput = (props) => {
const [content, setContent] = useState("");
const [isPublic, setIsPublic] = useState(true);

const sendMessage = () => {
if (content.length > 0) {
props.handleSubmit({ author: "User", content, isPublic });
setContent("");
}
};

return (
<div className="MessageInput">
<input
type="text"
data-testid="input-text"
value={content}
placeholder={"Type your message"}
onChange={(e) => setContent(e.target.value)}
/>
<input
type="checkbox"
data-testid="input-checkbox"
name="isPublic"
defaultChecked={isPublic}
onChange={() => setIsPublic(!isPublic)}
/>
<button
onClick={sendMessage}
disabled={props.isLoading}
data-testid="submit-btn"
>
Send {isPublic ? "public" : "private"}
</button>
</div>
);
};

export default MessageInput;
47 changes: 47 additions & 0 deletions src/components/MessageInput/MessageInput.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from "react";
import { render, fireEvent, cleanup } from "@testing-library/react";
import MessageInput from "./MessageInput";

afterEach(cleanup);

describe("<MessageInput />", () => {
it("should send messages", () => {
const fn = jest.fn();
const { getByTestId } = render(<MessageInput handleSubmit={fn} />);
const inputText = getByTestId("input-text");
const btn = getByTestId("submit-btn");
fireEvent.change(inputText, { target: { value: "last message" } });
fireEvent.click(btn);
expect(fn).toHaveBeenCalledTimes(1);
expect(fn).toHaveBeenCalledWith({
author: "User",
isPublic: true,
content: "last message",
});
});

it("should send private messages", () => {
const fn = jest.fn();
const { getByTestId } = render(<MessageInput handleSubmit={fn} />);
const inputText = getByTestId("input-text");
const inputCheckbox = getByTestId("input-checkbox");
const btn = getByTestId("submit-btn");
fireEvent.change(inputText, { target: { value: "last private message" } });
fireEvent.click(inputCheckbox);
fireEvent.click(btn);
expect(fn).toHaveBeenCalledTimes(1);
expect(fn).toHaveBeenCalledWith({
author: "User",
isPublic: false,
content: "last private message",
});
});

it("should not send blank messages", () => {
const fn = jest.fn();
const { getByTestId } = render(<MessageInput handleSubmit={fn} />);
const btn = getByTestId("submit-btn");
fireEvent.click(btn);
expect(fn).toHaveBeenCalledTimes(0);
});
});

0 comments on commit 076b9c2

Please sign in to comment.