Skip to content

Commit 71deafa

Browse files
committed
setup: useText hook refactor
1 parent b557f1f commit 71deafa

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/components/MessageForm/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import { Card } from "../Card";
66
import { MAX_MESSAGE_TEXT_LENGTH } from "../../config";
77
import "./styles.css";
88

9+
export const useText = initialText => {
10+
// TODO: extract useState logic out of MessageForm
11+
12+
return {
13+
text,
14+
handleChange
15+
};
16+
};
17+
918
export const MessageForm = ({ user }) => {
1019
const [text, setText] = React.useState("");
1120

src/tests/components/MessageForm.test.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from "react";
22
import { render, fireEvent } from "@testing-library/react";
3-
import { MessageForm } from "../../components/MessageForm";
3+
import { renderHook, act } from "@testing-library/react-hooks";
4+
import { MessageForm, useText } from "../../components/MessageForm";
45
import { USER, MAX_MESSAGE_TEXT_LENGTH } from "../../config";
56

67
describe("MessageForm", () => {
@@ -50,3 +51,30 @@ describe("MessageForm", () => {
5051
expect(button.disabled).toBe(false);
5152
});
5253
});
54+
55+
describe("useText", () => {
56+
test("useText hook should initiate text to empty string", () => {
57+
const { result } = renderHook(useText);
58+
expect(result.current.text).toBe("");
59+
});
60+
61+
test("useText hook should return a handleChange function", () => {
62+
const { result } = renderHook(useText);
63+
expect(result.current.handleChange).toBeDefined();
64+
});
65+
66+
test("useText hook allow updates of text", () => {
67+
const { result } = renderHook(useText);
68+
69+
act(() => {
70+
const event = {
71+
preventDefault() {},
72+
target: {
73+
value: "10 letters"
74+
}
75+
};
76+
result.current.handleChange(event);
77+
});
78+
expect(result.current.text).toBe("10 letters");
79+
});
80+
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy