From b65c11972b5c8548c48edd6c51cc4159f85e6892 Mon Sep 17 00:00:00 2001 From: KikyoNanakusa Date: Fri, 5 Apr 2024 04:21:09 +0900 Subject: [PATCH 1/9] =?UTF-8?q?test:UserCard=E3=81=AE=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__test__/components/UserCard.test.tsx | 36 +++++++++++++++++++ .../src/components/molecules/UserCard.tsx | 29 +++++++-------- 2 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 typing-app/__test__/components/UserCard.test.tsx diff --git a/typing-app/__test__/components/UserCard.test.tsx b/typing-app/__test__/components/UserCard.test.tsx new file mode 100644 index 0000000..3ef6697 --- /dev/null +++ b/typing-app/__test__/components/UserCard.test.tsx @@ -0,0 +1,36 @@ +import { UserCardPresenter } from "@/components/molecules/UserCard"; +import { describe, expect, it } from "@jest/globals"; +import { render, screen } from "@testing-library/react"; + +describe("UserCard", () => { + const mockUser = { + handleName: "しずっぴー", + studentNumber: "B1234567", + id: "1", + }; + + it("renders UserCard", () => { + const userCard = render(); + + const avatar = screen.getByRole("img"); + expect(avatar).toBeInTheDocument(); + }); + + it("renders UserCard with user data", () => { + const userCard = render(); + + const name = screen.getByText(/名前:/); + const studentNumber = screen.getByText(/学籍番号:/); + expect(name).toHaveTextContent(mockUser.handleName); + expect(studentNumber).toHaveTextContent(mockUser.studentNumber); + }); + + it("renders UserCard with default data", () => { + const userCard = render(); + + const name = screen.getByText(/名前:/); + const studentNumber = screen.getByText(/学籍番号:/); + expect(name).toHaveTextContent("ログインしていません"); + expect(studentNumber).toHaveTextContent("未ログイン"); + }); +}); \ No newline at end of file diff --git a/typing-app/src/components/molecules/UserCard.tsx b/typing-app/src/components/molecules/UserCard.tsx index 0e09dc5..fceed09 100644 --- a/typing-app/src/components/molecules/UserCard.tsx +++ b/typing-app/src/components/molecules/UserCard.tsx @@ -1,29 +1,30 @@ import React from "react"; import { Avatar, Box, Text, HStack, VStack } from "@chakra-ui/react"; +import { getCurrentUser } from "@/app/actions"; -const UserCard: React.FC = () => { - // モックのユーザー情報 - const user = { - name: "テストユーザー", - studentId: "24AB1234", - avatarUrl: "https://avatars.githubusercontent.com/u/12345678?v=4", - }; +type UserCardProps = { + user: Awaited>; +}; +export const UserCardPresenter: React.FC = ({ user }) => { return ( - user && ( - + - + - - 名前: {user.name} + + 名前: {user ? user.handleName : "ログインしていません"} - 学籍番号: {user.studentId} + 学籍番号: {user ? user.studentNumber : "未ログイン"} - ) ); }; +const UserCard = async () => { + const user = await getCurrentUser(); + return ; +}; + export default UserCard; From e48eee83fbc6d7bd1e16cc1b14fc841b0162a688 Mon Sep 17 00:00:00 2001 From: KikyoNanakusa Date: Fri, 5 Apr 2024 04:28:47 +0900 Subject: [PATCH 2/9] =?UTF-8?q?style:=20prettier=E3=82=92=E9=81=A9?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__test__/components/UserCard.test.tsx | 72 +++++++++---------- .../src/components/molecules/UserCard.tsx | 22 +++--- 2 files changed, 47 insertions(+), 47 deletions(-) diff --git a/typing-app/__test__/components/UserCard.test.tsx b/typing-app/__test__/components/UserCard.test.tsx index 3ef6697..4d8b6b0 100644 --- a/typing-app/__test__/components/UserCard.test.tsx +++ b/typing-app/__test__/components/UserCard.test.tsx @@ -1,36 +1,36 @@ -import { UserCardPresenter } from "@/components/molecules/UserCard"; -import { describe, expect, it } from "@jest/globals"; -import { render, screen } from "@testing-library/react"; - -describe("UserCard", () => { - const mockUser = { - handleName: "しずっぴー", - studentNumber: "B1234567", - id: "1", - }; - - it("renders UserCard", () => { - const userCard = render(); - - const avatar = screen.getByRole("img"); - expect(avatar).toBeInTheDocument(); - }); - - it("renders UserCard with user data", () => { - const userCard = render(); - - const name = screen.getByText(/名前:/); - const studentNumber = screen.getByText(/学籍番号:/); - expect(name).toHaveTextContent(mockUser.handleName); - expect(studentNumber).toHaveTextContent(mockUser.studentNumber); - }); - - it("renders UserCard with default data", () => { - const userCard = render(); - - const name = screen.getByText(/名前:/); - const studentNumber = screen.getByText(/学籍番号:/); - expect(name).toHaveTextContent("ログインしていません"); - expect(studentNumber).toHaveTextContent("未ログイン"); - }); -}); \ No newline at end of file +import { UserCardPresenter } from "@/components/molecules/UserCard"; +import { describe, expect, it } from "@jest/globals"; +import { render, screen } from "@testing-library/react"; + +describe("UserCard", () => { + const mockUser = { + handleName: "しずっぴー", + studentNumber: "B1234567", + id: "1", + }; + + it("renders UserCard", () => { + const userCard = render(); + + const avatar = screen.getByRole("img"); + expect(avatar).toBeInTheDocument(); + }); + + it("renders UserCard with user data", () => { + const userCard = render(); + + const name = screen.getByText(/名前:/); + const studentNumber = screen.getByText(/学籍番号:/); + expect(name).toHaveTextContent(mockUser.handleName); + expect(studentNumber).toHaveTextContent(mockUser.studentNumber); + }); + + it("renders UserCard with default data", () => { + const userCard = render(); + + const name = screen.getByText(/名前:/); + const studentNumber = screen.getByText(/学籍番号:/); + expect(name).toHaveTextContent("ログインしていません"); + expect(studentNumber).toHaveTextContent("未ログイン"); + }); +}); diff --git a/typing-app/src/components/molecules/UserCard.tsx b/typing-app/src/components/molecules/UserCard.tsx index fceed09..83da80c 100644 --- a/typing-app/src/components/molecules/UserCard.tsx +++ b/typing-app/src/components/molecules/UserCard.tsx @@ -8,17 +8,17 @@ type UserCardProps = { export const UserCardPresenter: React.FC = ({ user }) => { return ( - - - - - - 名前: {user ? user.handleName : "ログインしていません"} - - 学籍番号: {user ? user.studentNumber : "未ログイン"} - - - + + + + + + 名前: {user ? user.handleName : "ログインしていません"} + + 学籍番号: {user ? user.studentNumber : "未ログイン"} + + + ); }; From dfe96d08033a38a99bb54f2eabb249f5808c0553 Mon Sep 17 00:00:00 2001 From: KikyoNanakusa Date: Fri, 5 Apr 2024 04:47:22 +0900 Subject: [PATCH 3/9] =?UTF-8?q?refactor:=20User=E5=9E=8B=E3=82=92=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E5=A4=89?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-app/__test__/components/UserCard.test.tsx | 9 +++++---- typing-app/src/components/molecules/UserCard.tsx | 11 ++++------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/typing-app/__test__/components/UserCard.test.tsx b/typing-app/__test__/components/UserCard.test.tsx index 4d8b6b0..9653ae8 100644 --- a/typing-app/__test__/components/UserCard.test.tsx +++ b/typing-app/__test__/components/UserCard.test.tsx @@ -1,23 +1,24 @@ import { UserCardPresenter } from "@/components/molecules/UserCard"; import { describe, expect, it } from "@jest/globals"; import { render, screen } from "@testing-library/react"; +import { User } from "@/types/user"; describe("UserCard", () => { - const mockUser = { + const mockUser: User = { handleName: "しずっぴー", studentNumber: "B1234567", id: "1", }; it("renders UserCard", () => { - const userCard = render(); + const userCard = render(UserCardPresenter(mockUser)); const avatar = screen.getByRole("img"); expect(avatar).toBeInTheDocument(); }); it("renders UserCard with user data", () => { - const userCard = render(); + const userCard = render(UserCardPresenter(mockUser)); const name = screen.getByText(/名前:/); const studentNumber = screen.getByText(/学籍番号:/); @@ -26,7 +27,7 @@ describe("UserCard", () => { }); it("renders UserCard with default data", () => { - const userCard = render(); + const userCard = render(UserCardPresenter(null)); const name = screen.getByText(/名前:/); const studentNumber = screen.getByText(/学籍番号:/); diff --git a/typing-app/src/components/molecules/UserCard.tsx b/typing-app/src/components/molecules/UserCard.tsx index 83da80c..1694d4c 100644 --- a/typing-app/src/components/molecules/UserCard.tsx +++ b/typing-app/src/components/molecules/UserCard.tsx @@ -1,12 +1,9 @@ import React from "react"; import { Avatar, Box, Text, HStack, VStack } from "@chakra-ui/react"; import { getCurrentUser } from "@/app/actions"; +import { User } from "@/types/user"; -type UserCardProps = { - user: Awaited>; -}; - -export const UserCardPresenter: React.FC = ({ user }) => { +export async function UserCardPresenter(user: User | null) { return ( @@ -20,11 +17,11 @@ export const UserCardPresenter: React.FC = ({ user }) => { ); -}; +} const UserCard = async () => { const user = await getCurrentUser(); - return ; + return UserCardPresenter(user); }; export default UserCard; From 6e13f986d0ea1fb5484e94c2233206ea7396e623 Mon Sep 17 00:00:00 2001 From: KikyoNanakusa Date: Fri, 5 Apr 2024 04:50:00 +0900 Subject: [PATCH 4/9] =?UTF-8?q?refactor:User=E3=82=92import=20type?= =?UTF-8?q?=E3=81=A7import=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-app/__test__/components/UserCard.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typing-app/__test__/components/UserCard.test.tsx b/typing-app/__test__/components/UserCard.test.tsx index 9653ae8..dd375a9 100644 --- a/typing-app/__test__/components/UserCard.test.tsx +++ b/typing-app/__test__/components/UserCard.test.tsx @@ -1,7 +1,7 @@ import { UserCardPresenter } from "@/components/molecules/UserCard"; import { describe, expect, it } from "@jest/globals"; import { render, screen } from "@testing-library/react"; -import { User } from "@/types/user"; +import type { User } from "@/types/user"; describe("UserCard", () => { const mockUser: User = { From 7fc4f58339377413fbd17a5d4edc8bffe50fadc3 Mon Sep 17 00:00:00 2001 From: KikyoNanakusa Date: Fri, 5 Apr 2024 05:08:11 +0900 Subject: [PATCH 5/9] =?UTF-8?q?refactor:=20getCurrentUser=E3=81=AE?= =?UTF-8?q?=E6=88=BB=E3=82=8A=E5=80=A4=E3=82=92null=E3=81=8B=E3=82=89undef?= =?UTF-8?q?ined=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-app/__test__/components/UserCard.test.tsx | 6 +++--- typing-app/src/app/actions.ts | 4 ++-- typing-app/src/components/molecules/UserCard.tsx | 10 +++++++--- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/typing-app/__test__/components/UserCard.test.tsx b/typing-app/__test__/components/UserCard.test.tsx index dd375a9..fb188f6 100644 --- a/typing-app/__test__/components/UserCard.test.tsx +++ b/typing-app/__test__/components/UserCard.test.tsx @@ -11,14 +11,14 @@ describe("UserCard", () => { }; it("renders UserCard", () => { - const userCard = render(UserCardPresenter(mockUser)); + const userCard = render(); const avatar = screen.getByRole("img"); expect(avatar).toBeInTheDocument(); }); it("renders UserCard with user data", () => { - const userCard = render(UserCardPresenter(mockUser)); + const userCard = render(); const name = screen.getByText(/名前:/); const studentNumber = screen.getByText(/学籍番号:/); @@ -27,7 +27,7 @@ describe("UserCard", () => { }); it("renders UserCard with default data", () => { - const userCard = render(UserCardPresenter(null)); + const userCard = render(); const name = screen.getByText(/名前:/); const studentNumber = screen.getByText(/学籍番号:/); diff --git a/typing-app/src/app/actions.ts b/typing-app/src/app/actions.ts index 6f4924c..db2f9b1 100644 --- a/typing-app/src/app/actions.ts +++ b/typing-app/src/app/actions.ts @@ -45,12 +45,12 @@ export async function logout() { export async function getCurrentUser() { const userStr = cookies().get("user")?.value; - if (!userStr) return null; + if (!userStr) return undefined; function isValidUser(o: any): o is User { return o && typeof o.id === "string" && typeof o.studentNumber === "string" && typeof o.handleName == "string"; } const user = JSON.parse(userStr) as User; - return isValidUser(user) ? user : null; + return isValidUser(user) ? user : undefined; } diff --git a/typing-app/src/components/molecules/UserCard.tsx b/typing-app/src/components/molecules/UserCard.tsx index 1694d4c..49ad2c3 100644 --- a/typing-app/src/components/molecules/UserCard.tsx +++ b/typing-app/src/components/molecules/UserCard.tsx @@ -1,9 +1,13 @@ import React from "react"; import { Avatar, Box, Text, HStack, VStack } from "@chakra-ui/react"; import { getCurrentUser } from "@/app/actions"; -import { User } from "@/types/user"; +import type { User } from "@/types/user"; -export async function UserCardPresenter(user: User | null) { +interface UserCardPresenterProps { + user?: User; +} + +export const UserCardPresenter = ({ user }:UserCardPresenterProps) => { return ( @@ -21,7 +25,7 @@ export async function UserCardPresenter(user: User | null) { const UserCard = async () => { const user = await getCurrentUser(); - return UserCardPresenter(user); + return ; }; export default UserCard; From 6aec5789b678ef5958538cb14e5743e2b85d68da Mon Sep 17 00:00:00 2001 From: KikyoNanakusa Date: Fri, 5 Apr 2024 05:10:44 +0900 Subject: [PATCH 6/9] =?UTF-8?q?style:=20prettier=E3=82=92=E9=81=A9?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-app/__test__/components/UserCard.test.tsx | 2 +- typing-app/src/components/molecules/UserCard.tsx | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/typing-app/__test__/components/UserCard.test.tsx b/typing-app/__test__/components/UserCard.test.tsx index fb188f6..899841f 100644 --- a/typing-app/__test__/components/UserCard.test.tsx +++ b/typing-app/__test__/components/UserCard.test.tsx @@ -27,7 +27,7 @@ describe("UserCard", () => { }); it("renders UserCard with default data", () => { - const userCard = render(); + const userCard = render(); const name = screen.getByText(/名前:/); const studentNumber = screen.getByText(/学籍番号:/); diff --git a/typing-app/src/components/molecules/UserCard.tsx b/typing-app/src/components/molecules/UserCard.tsx index 49ad2c3..5ff6dd0 100644 --- a/typing-app/src/components/molecules/UserCard.tsx +++ b/typing-app/src/components/molecules/UserCard.tsx @@ -7,7 +7,7 @@ interface UserCardPresenterProps { user?: User; } -export const UserCardPresenter = ({ user }:UserCardPresenterProps) => { +export const UserCardPresenter = ({ user }: UserCardPresenterProps) => { return ( @@ -21,11 +21,11 @@ export const UserCardPresenter = ({ user }:UserCardPresenterProps) => { ); -} +}; const UserCard = async () => { const user = await getCurrentUser(); - return ; + return ; }; export default UserCard; From 0d7ff84db287565a2a57f1f8dc8922444b6bba85 Mon Sep 17 00:00:00 2001 From: KikyoNanakusa Date: Fri, 5 Apr 2024 09:23:08 +0900 Subject: [PATCH 7/9] =?UTF-8?q?feat:=E7=94=BB=E5=83=8F=E3=82=92=E3=81=97?= =?UTF-8?q?=E3=81=9A=E3=81=A3=E3=81=B4=E3=83=BC=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-app/src/components/molecules/UserCard.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typing-app/src/components/molecules/UserCard.tsx b/typing-app/src/components/molecules/UserCard.tsx index 5ff6dd0..05bf0ee 100644 --- a/typing-app/src/components/molecules/UserCard.tsx +++ b/typing-app/src/components/molecules/UserCard.tsx @@ -11,7 +11,7 @@ export const UserCardPresenter = ({ user }: UserCardPresenterProps) => { return ( - + 名前: {user ? user.handleName : "ログインしていません"} From 22e4b0e2912b83bb7aa82da511a755a20eb89d4c Mon Sep 17 00:00:00 2001 From: KikyoNanakusa Date: Fri, 5 Apr 2024 09:33:15 +0900 Subject: [PATCH 8/9] =?UTF-8?q?feat:UserCard=E3=81=AE=E3=82=B5=E3=82=A4?= =?UTF-8?q?=E3=82=BA=E3=82=92=E6=8B=A1=E5=A4=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/molecules/UserCard.tsx | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/typing-app/src/components/molecules/UserCard.tsx b/typing-app/src/components/molecules/UserCard.tsx index 05bf0ee..c39e0ab 100644 --- a/typing-app/src/components/molecules/UserCard.tsx +++ b/typing-app/src/components/molecules/UserCard.tsx @@ -9,17 +9,15 @@ interface UserCardPresenterProps { export const UserCardPresenter = ({ user }: UserCardPresenterProps) => { return ( - - - - - - 名前: {user ? user.handleName : "ログインしていません"} - - 学籍番号: {user ? user.studentNumber : "未ログイン"} - - - + + + + + 名前: {user ? user.handleName : "ログインしていません"} + + 学籍番号: {user ? user.studentNumber : "未ログイン"} + + ); }; From 9386efa2be58230625e8a9472fe827ec0afed6d2 Mon Sep 17 00:00:00 2001 From: KikyoNanakusa Date: Fri, 5 Apr 2024 09:34:48 +0900 Subject: [PATCH 9/9] =?UTF-8?q?style:prettier=E3=82=92=E9=81=A9=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-app/src/components/molecules/UserCard.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/typing-app/src/components/molecules/UserCard.tsx b/typing-app/src/components/molecules/UserCard.tsx index c39e0ab..7d9ecab 100644 --- a/typing-app/src/components/molecules/UserCard.tsx +++ b/typing-app/src/components/molecules/UserCard.tsx @@ -10,7 +10,11 @@ interface UserCardPresenterProps { export const UserCardPresenter = ({ user }: UserCardPresenterProps) => { return ( - + 名前: {user ? user.handleName : "ログインしていません"}