1
- import { render , screen } from "@testing-library/react" ;
1
+ import { render , screen } from "@testing-library/react" ;
2
2
import { ChakraProvider } from "@chakra-ui/react" ;
3
3
import { Contributors } from "../components/Contributors" ;
4
4
import { act } from "react" ;
5
+ import userEvent from "@testing-library/user-event" ;
5
6
6
-
7
+ // firebase関数のモック化
7
8
jest . mock ( "firebase/app" , ( ) => ( {
8
9
initializeApp : jest . fn ( ) ,
9
10
} ) ) ;
@@ -13,6 +14,22 @@ jest.mock("firebase/auth", () => ({
13
14
} ) ,
14
15
} ) ) ;
15
16
17
+ const mockGetSubjects = jest
18
+ . fn ( )
19
+ . mockResolvedValue ( [ { id : 1 , year : 1 , name : "離散数学" } ] ) ;
20
+
21
+ // supabase関数のモック化
22
+ jest . mock ( "../lib/supabasefunctions" , ( ) => {
23
+ return {
24
+ getSubjects : ( ) => mockGetSubjects ( ) ,
25
+ getSubjectUserIds : jest . fn ( ) ,
26
+ getUserInfo : jest . fn ( ) ,
27
+ // addUser: jest.fn(),
28
+ // updateUserInfo: jest.fn(),
29
+ // addUserToSubject: jest.fn()
30
+ } ;
31
+ } ) ;
32
+
16
33
describe ( "過去問の募集投稿ページのテスト" , ( ) => {
17
34
beforeEach ( ( ) => {
18
35
render (
@@ -28,17 +45,17 @@ describe("過去問の募集投稿ページのテスト", () => {
28
45
29
46
test ( "バリデーションエラーが表示される" , async ( ) => {
30
47
const postButton = screen . getByTestId ( "kakomon-register-modal" ) ;
31
-
48
+
32
49
await act ( async ( ) => {
33
50
postButton . click ( ) ;
34
51
} ) ;
35
-
52
+
36
53
const submitButton = await screen . findByTestId ( "kakomon-register-button" ) ;
37
-
54
+
38
55
await act ( async ( ) => {
39
56
submitButton . click ( ) ;
40
57
} ) ;
41
-
58
+
42
59
expect (
43
60
await screen . findByText ( "⚠️お名前は必須入力項目です。" )
44
61
) . toBeInTheDocument ( ) ;
@@ -50,4 +67,31 @@ describe("過去問の募集投稿ページのテスト", () => {
50
67
) . toBeInTheDocument ( ) ;
51
68
} ) ;
52
69
70
+ test ( "過去問を登録できる" , async ( ) => {
71
+ const postButton = screen . getByTestId ( "kakomon-register-modal" ) ;
72
+ await act ( async ( ) => {
73
+ postButton . click ( ) ;
74
+ } ) ;
75
+ const submitButton = await screen . findByTestId ( "kakomon-register-button" ) ;
76
+ expect ( submitButton ) . toBeInTheDocument ( ) ;
77
+
78
+ const nameInput = await screen . findByTestId ( "name-input" ) ;
79
+ await act ( async ( ) => {
80
+ await userEvent . type ( nameInput , "テスト太郎" ) ;
81
+ } ) ;
82
+ const kakaoIdInput = screen . getByTestId ( "kakaoId-input" ) ;
83
+ await act ( async ( ) => {
84
+ await userEvent . type ( kakaoIdInput , "test-id" ) ;
85
+ } ) ;
86
+
87
+ const subjectNameInput = screen . getByTestId ( "subjectName-input" ) ;
88
+ await act ( async ( ) => {
89
+ await userEvent . type ( subjectNameInput , "離散数学" ) ;
90
+ } ) ;
91
+ await act ( async ( ) => {
92
+ submitButton . click ( ) ;
93
+ } ) ;
94
+
95
+ } ) ;
96
+
53
97
} ) ;
0 commit comments