Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38ee2a3

Browse files
committed
setup: useState message
1 parent e7f5a39 commit 38ee2a3

File tree

3 files changed

+84
-15
lines changed

3 files changed

+84
-15
lines changed

src/components/Message/index.js renamed to src/components/Message/useState.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export const Message = props => {
1616
text
1717
} = props;
1818

19+
/* TODO: toggle retweet & favorite status & count on click */
20+
1921
return (
2022
<Card
2123
className="Message"
@@ -37,7 +39,7 @@ export const Message = props => {
3739
<IconButton
3840
role="retweet"
3941
onClick={() => {
40-
/* toggle retweet */
42+
/* toggle retweet locally */
4143
}}
4244
>
4345
<Icon
@@ -51,7 +53,7 @@ export const Message = props => {
5153
<IconButton
5254
role="favorite"
5355
onClick={() => {
54-
/* toggle favorite */
56+
/* toggle favorite locally */
5557
}}
5658
>
5759
<Icon

src/stories/2-Message.stories.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { Message } from "../components/Message";
2+
import { Message } from "../components/Message/useState";
33
import { USER } from "../config";
44

55
export default {
@@ -8,17 +8,22 @@ export default {
88

99
const now = new Date();
1010

11-
export const message = () => {
12-
const post = {
13-
id: "1",
14-
created_at: now.setMinutes(now.getMinutes() - 20),
15-
text:
16-
"A bunch of text here filling out the body of the tweet, might be really really long. Maybe even spans a few lines because its so long.",
17-
user: USER,
18-
retweeted: false,
19-
retweet_count: 52,
20-
favorited: false,
21-
favorite_count: 101
22-
};
11+
const post = {
12+
id: "1",
13+
created_at: now.setMinutes(now.getMinutes() - 20),
14+
text:
15+
"A bunch of text here filling out the body of the tweet, might be really really long. Maybe even spans a few lines because its so long.",
16+
user: USER,
17+
retweeted: false,
18+
retweet_count: 52,
19+
favorited: false,
20+
favorite_count: 101
21+
};
22+
23+
export const useStateMessage = () => {
2324
return <Message {...post} />;
2425
};
26+
27+
useStateMessage.story = {
28+
name: "useState example"
29+
};

src/tests/components/Message.test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import React from "react";
2+
import { render, fireEvent } from "@testing-library/react";
3+
import { Message } from "../../components/Message/useState";
4+
import { USER } from "../../config";
5+
6+
const mockProps = {
7+
user: USER,
8+
text: "Some text",
9+
created_at: new Date().toISOString(),
10+
retweeted: false,
11+
retweet_count: 12,
12+
favorited: false,
13+
favorite_count: 22
14+
};
15+
16+
describe("Message", () => {
17+
test("should display the initial favorite count", () => {
18+
const utils = render(<Message {...mockProps} />);
19+
const count = utils.getByTitle("favorite_count");
20+
expect(count.textContent).toBe("22");
21+
});
22+
23+
test("should increment the favorite count on click", () => {
24+
const utils = render(<Message {...mockProps} />);
25+
const button = utils.getByRole("favorite");
26+
fireEvent.click(button);
27+
const count = utils.getByTitle("favorite_count");
28+
expect(count.textContent).toBe("23");
29+
});
30+
31+
test("favoriting should return to initial count on second click", () => {
32+
const utils = render(<Message {...mockProps} />);
33+
const button = utils.getByRole("favorite");
34+
fireEvent.click(button);
35+
fireEvent.click(button);
36+
const count = utils.getByTitle("favorite_count");
37+
expect(count.textContent).toBe("22");
38+
});
39+
40+
test("should display the initial retweet count", () => {
41+
const utils = render(<Message {...mockProps} />);
42+
const count = utils.getByTitle("retweet_count");
43+
expect(count.textContent).toBe("12");
44+
});
45+
46+
test("should increment the retweet count on click", () => {
47+
const utils = render(<Message {...mockProps} />);
48+
const button = utils.getByRole("retweet");
49+
fireEvent.click(button);
50+
const count = utils.getByTitle("retweet_count");
51+
expect(count.textContent).toBe("13");
52+
});
53+
54+
test("retweeting should return to initial count on second click", () => {
55+
const utils = render(<Message {...mockProps} />);
56+
const button = utils.getByRole("retweet");
57+
fireEvent.click(button);
58+
fireEvent.click(button);
59+
const count = utils.getByTitle("retweet_count");
60+
expect(count.textContent).toBe("12");
61+
});
62+
});

0 commit comments

Comments
 (0)