Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit71deafa

Browse files
committed
setup: useText hook refactor
1 parentb557f1f commit71deafa

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+
exportconstuseText=initialText=>{
10+
// TODO: extract useState logic out of MessageForm
11+
12+
return{
13+
text,
14+
handleChange
15+
};
16+
};
17+
918
exportconstMessageForm=({ 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
importReactfrom"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+
constevent={
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)

[8]ページ先頭

©2009-2025 Movatter.jp