1
+ import { render , screen } from "@testing-library/react" ;
2
+ import { BrowserRouter } from "react-router-dom" ;
3
+ import { AddUpdateMovieModal } from "../../components/movie-details/AddUpdateMovieModal" ;
4
+
5
+ describe ( "AddUpdateMovieModal component" , ( ) => {
6
+ const mockMovie = {
7
+ // mock movie object
8
+ } ;
9
+
10
+ const mockOperation = "Create" ;
11
+ const handleShowAddUpdateModal = vi . fn ( ) ;
12
+ const handleAddUpdateMovie = vi . fn ( ) ;
13
+
14
+ it ( "calls createMutation.mutate with the correct arguments and navigates to the home page on success" , ( ) => {
15
+ const mockCreateMutation = {
16
+ mutate : vi . fn ( ) ,
17
+ } ;
18
+
19
+ const mockNavigate = vi . fn ( ) ;
20
+
21
+
22
+ render (
23
+ < BrowserRouter >
24
+ < AddUpdateMovieModal
25
+ createMutation = { mockCreateMutation }
26
+ navigate = { mockNavigate }
27
+ />
28
+ </ BrowserRouter >
29
+ ) ;
30
+
31
+ // Call the handleAddUpdateMovie function with the mock movie and operation
32
+ handleAddUpdateMovie ( mockMovie , mockOperation ) ;
33
+
34
+ // Assert that createMutation.mutate was called with the correct arguments
35
+ expect ( mockCreateMutation . mutate ) . toHaveBeenCalledWith ( mockMovie , {
36
+ onSuccess : expect . any ( Function ) ,
37
+ onError : expect . any ( Function ) ,
38
+ } ) ;
39
+
40
+ // Simulate a successful mutation
41
+ mockCreateMutation . mutate . mock . calls [ 0 ] [ 1 ] . onSuccess ( ) ;
42
+
43
+ // Assert that the success alert was displayed
44
+ expect ( screen . getByText ( "Movie added successfully" ) ) . toBeInTheDocument ( ) ;
45
+
46
+ // Assert that handleShowAddUpdateModal was called
47
+ expect ( handleShowAddUpdateModal ) . toHaveBeenCalled ( ) ;
48
+
49
+ // Assert that navigate was called with the correct path
50
+ expect ( mockNavigate ) . toHaveBeenCalledWith ( "/" ) ;
51
+
52
+ // Reset mock functions
53
+ mockCreateMutation . mutate . mockReset ( ) ;
54
+ mockNavigate . mockReset ( ) ;
55
+ } ) ;
56
+
57
+ it ( "calls updateMutation.mutate with the correct arguments and navigates to the home page on success" , ( ) => {
58
+ const mockUpdateMutation = {
59
+ mutate : vi . fn ( ) ,
60
+ } ;
61
+
62
+ const mockNavigate = vi . fn ( ) ;
63
+
64
+ render (
65
+ < BrowserRouter >
66
+ < AddUpdateMovieModal
67
+ updateMutation = { mockUpdateMutation }
68
+ navigate = { mockNavigate }
69
+ />
70
+ </ BrowserRouter >
71
+ ) ;
72
+
73
+ // Call the handleAddUpdateMovie function with the mock movie and operation
74
+ handleAddUpdateMovie ( mockMovie , "Update" ) ;
75
+
76
+ // Assert that updateMutation.mutate was called with the correct arguments
77
+ expect ( mockUpdateMutation . mutate ) . toHaveBeenCalledWith ( mockMovie , {
78
+ onSuccess : expect . any ( Function ) ,
79
+ onError : expect . any ( Function ) ,
80
+ } ) ;
81
+
82
+ // Simulate a successful mutation
83
+ mockUpdateMutation . mutate . mock . calls [ 0 ] [ 1 ] . onSuccess ( ) ;
84
+
85
+ // Assert that the success alert was displayed
86
+ expect ( screen . getByText ( "Movie updated successfully" ) ) . toBeInTheDocument ( ) ;
87
+
88
+ // Assert that handleShowAddUpdateModal was called
89
+ expect ( handleShowAddUpdateModal ) . toHaveBeenCalled ( ) ;
90
+
91
+ // Assert that navigate was called with the correct path
92
+ expect ( mockNavigate ) . toHaveBeenCalledWith ( "/" ) ;
93
+
94
+ // Reset mock functions
95
+ mockUpdateMutation . mutate . mockReset ( ) ;
96
+ mockNavigate . mockReset ( ) ;
97
+ } ) ;
98
+
99
+ it ( "logs an error message for an invalid operation" , ( ) => {
100
+ console . log = vi . fn ( ) ;
101
+
102
+ // render(
103
+ // <BrowserRouter>
104
+ // <AddUpdateMovieModal />
105
+ // </BrowserRouter>
106
+ // );
107
+
108
+ // Call the handleAddUpdateMovie function with the mock movie and an invalid operation
109
+ handleAddUpdateMovie ( mockMovie , "InvalidOperation" ) ;
110
+
111
+ // Assert that console.log was called with the error message
112
+ expect ( console . log ) . toHaveBeenCalledWith ( "Invalid operation" ) ;
113
+
114
+ } ) ;
115
+ } ) ;
0 commit comments