@@ -8,114 +8,136 @@ import GetMedication from "./page";
88const mockRouter = jest . fn ( ) ;
99
1010jest . mock ( "next/navigation" , ( ) => ( {
11- useRouter : ( ) => {
12- return {
13- push : mockRouter ,
14- } ;
15- } ,
11+ useRouter : ( ) => {
12+ return {
13+ push : mockRouter ,
14+ } ;
15+ } ,
1616} ) ) ;
1717
1818jest . mock ( "../../http/medicationAPI" , ( ) => {
19- return {
20- getMedication : jest . fn ( ) . mockResolvedValue ( ( ) => {
21- return {
22- success : "SUCCESS" ,
23- data : {
24- uid : "1" ,
25- medicationName : "Advil" ,
26- image : "imagepath" ,
27- dateStarted : "2014-01-01" ,
28- time : "08:36" ,
29- dosage : 60 ,
30- unit : "milligram (mg)" ,
31- frequency : "Six times a day" ,
32- route : "Rectal" ,
33- Notes : "Test medication" ,
34- } ,
35- } ;
36- } ) ,
37- } ;
19+ return {
20+ getMedication : jest . fn ( ) . mockResolvedValue ( ( ) => {
21+ return {
22+ success : "SUCCESS" ,
23+ data : {
24+ uid : "1" ,
25+ medicationName : "Advil" ,
26+ image : "imagepath" ,
27+ dateStarted : "2014-01-01" ,
28+ expirationDate : "2014-01-01" ,
29+ time : "08:36" ,
30+ dosage : 60 ,
31+ unit : "milligram (mg)" ,
32+ frequency : "Six times a day" ,
33+ route : "Rectal" ,
34+ Notes : "Test medication" ,
35+ } ,
36+ } ;
37+ } ) ,
38+ } ;
3839} ) ;
3940
4041jest . mock ( "../../contexts/UserContext" , ( ) => {
41- return {
42- useUser : ( ) => {
43- return {
44- userInfo : {
45- uid : "1" ,
46- } ,
47- } ;
48- } ,
49- } ;
42+ return {
43+ useUser : ( ) => {
44+ return {
45+ userInfo : {
46+ uid : "1" ,
47+ } ,
48+ } ;
49+ } ,
50+ } ;
5051} ) ;
5152
5253jest . mock ( "../../contexts/AuthContext" , ( ) => {
53- return {
54- useAuth : jest . fn ( ) ,
55- } ;
54+ return {
55+ useAuth : jest . fn ( ) ,
56+ } ;
5657} ) ;
5758
5859describe ( "User is logged in" , ( ) => {
59- beforeEach ( ( ) => {
60- useAuth . mockImplementation ( ( ) => {
61- return {
62- user : {
63- uid : "1" ,
64- } ,
65- } ;
66- } ) ;
67- } ) ;
60+ beforeEach ( ( ) => {
61+ useAuth . mockImplementation ( ( ) => {
62+ return {
63+ user : {
64+ uid : "1" ,
65+ } ,
66+ } ;
67+ } ) ;
68+ } ) ;
6869
69- test ( "User data is displayed correctly" , async ( ) => {
70- render ( < GetMedication params = { { medication : "1" } } /> ) ;
71- setTimeout ( ( ) => {
72- expect ( screen . getByText ( "Start Date:" ) ) . toBeInTheDocument ( ) ;
73- expect ( screen . getByText ( "Time:" ) ) . toBeInTheDocument ( ) ;
74- expect ( screen . getByText ( "Medication Name:" ) ) . toBeInTheDocument ( ) ;
75- expect ( screen . getByText ( "Dosage:" ) ) . toBeInTheDocument ( ) ;
76- expect ( screen . getByText ( "Frequency:" ) ) . toBeInTheDocument ( ) ;
77- expect ( screen . getByText ( "Unit:" ) ) . toBeInTheDocument ( ) ;
78- expect ( screen . getByText ( "Route:" ) ) . toBeInTheDocument ( ) ;
79- expect ( screen . getByText ( "Notes:" ) ) . toBeInTheDocument ( ) ;
80- expect ( screen . getByText ( "Jan 1,2014" ) ) . toBeInTheDocument ( ) ;
81- expect ( screen . getByText ( "8h36" ) ) . toBeInTheDocument ( ) ;
82- expect ( screen . getByText ( "advil" ) ) . toBeInTheDocument ( ) ;
83- expect ( screen . getByText ( "80" ) ) . toBeInTheDocument ( ) ;
84- expect ( screen . getByText ( "other" ) ) . toBeInTheDocument ( ) ;
85- expect ( screen . getByText ( "other" ) ) . toBeInTheDocument ( ) ;
86- expect ( screen . getByText ( "other" ) ) . toBeInTheDocument ( ) ;
87- expect ( screen . getByText ( "I got a lower dose" ) ) . toBeInTheDocument ( ) ;
88- const pillImage = screen . getByAltText ( "Medication Image" ) ;
89- expect ( pillImage ) . toBeVisible ( ) ;
90- } , 1000 ) ;
91- } ) ;
70+ test ( "User data is displayed correctly" , async ( ) => {
71+ render ( < GetMedication params = { { medication : "1" } } /> ) ;
72+ setTimeout ( ( ) => {
73+ expect ( screen . getByText ( "Start Date:" ) ) . toBeInTheDocument ( ) ;
74+ expect ( screen . getByText ( "Expiration Date" ) ) . toBeInTheDocument ( ) ;
75+ expect ( screen . getByText ( "Time:" ) ) . toBeInTheDocument ( ) ;
76+ expect ( screen . getByText ( "Medication Name:" ) ) . toBeInTheDocument ( ) ;
77+ expect ( screen . getByText ( "Dosage:" ) ) . toBeInTheDocument ( ) ;
78+ expect ( screen . getByText ( "Frequency:" ) ) . toBeInTheDocument ( ) ;
79+ expect ( screen . getByText ( "Unit:" ) ) . toBeInTheDocument ( ) ;
80+ expect ( screen . getByText ( "Route:" ) ) . toBeInTheDocument ( ) ;
81+ expect ( screen . getByText ( "Notes:" ) ) . toBeInTheDocument ( ) ;
82+ expect ( screen . getByText ( "Jan 1,2014" ) ) . toBeInTheDocument ( ) ;
83+ expect ( screen . getByText ( "8h36" ) ) . toBeInTheDocument ( ) ;
84+ expect ( screen . getByText ( "advil" ) ) . toBeInTheDocument ( ) ;
85+ expect ( screen . getByText ( "80" ) ) . toBeInTheDocument ( ) ;
86+ expect ( screen . getByText ( "other" ) ) . toBeInTheDocument ( ) ;
87+ expect ( screen . getByText ( "other" ) ) . toBeInTheDocument ( ) ;
88+ expect ( screen . getByText ( "other" ) ) . toBeInTheDocument ( ) ;
89+ expect ( screen . getByText ( "I got a lower dose" ) ) . toBeInTheDocument ( ) ;
90+ const pillImage = screen . getByAltText ( "Medication Image" ) ;
91+ expect ( pillImage ) . toBeVisible ( ) ;
92+ } , 1000 ) ;
93+ } ) ;
9294
93- test ( "Cancel button functions correctly" , async ( ) => {
94- render ( < GetMedication params = { { medication : "1" } } /> ) ;
95- setTimeout ( ( ) => {
96- const cancelButton = screen . getAllByRole ( "button" ) [ 2 ] ;
97- userEvent . click ( cancelButton ) ;
98- mockRouter ;
99- expect ( mockRouter ) . toHaveBeenCalledWith ( "/getMedications" ) ;
100- } , 1000 ) ;
101- } ) ;
95+ test ( "Cancel button functions correctly" , async ( ) => {
96+ render ( < GetMedication params = { { medication : "1" } } /> ) ;
97+ setTimeout ( ( ) => {
98+ const cancelButton = screen . getAllByRole ( "button" ) [ 2 ] ;
99+ userEvent . click ( cancelButton ) ;
100+ mockRouter ;
101+ expect ( mockRouter ) . toHaveBeenCalledWith ( "/getMedications" ) ;
102+ } , 1000 ) ;
103+ } ) ;
102104
103- test ( "Update button functions correctly" , async ( ) => {
104- render ( < GetMedication params = { { medication : "1" } } /> ) ;
105- setTimeout ( ( ) => {
106- const updateButton = screen . getAllByRole ( "button" ) [ 1 ] ;
107- userEvent . click ( updateButton ) ;
108- mockRouter ;
109- expect ( mockRouter ) . toHaveBeenCalledWith ( "/getMedications/1/1" ) ;
110- } , 1000 ) ;
111- } ) ;
105+ test ( "Update button functions correctly" , async ( ) => {
106+ render ( < GetMedication params = { { medication : "1" } } /> ) ;
107+ setTimeout ( ( ) => {
108+ const updateButton = screen . getAllByRole ( "button" ) [ 1 ] ;
109+ userEvent . click ( updateButton ) ;
110+ mockRouter ;
111+ expect ( mockRouter ) . toHaveBeenCalledWith ( "/getMedications/1/1" ) ;
112+ } , 1000 ) ;
113+ } ) ;
112114
113- test ( "getMedication function is called correctly" , async ( ) => {
114- await act ( async ( ) => {
115- jest . advanceTimersByTime ( 1000 ) ;
116- } ) ;
117- await waitFor ( ( ) => {
118- expect ( getMedication ) . toHaveBeenCalled ( ) ;
119- } ) ;
120- } ) ;
115+ test ( "getMedication function is called correctly" , async ( ) => {
116+ await act ( async ( ) => {
117+ jest . advanceTimersByTime ( 1000 ) ;
118+ } ) ;
119+ await waitFor ( ( ) => {
120+ expect ( getMedication ) . toHaveBeenCalled ( ) ;
121+ } ) ;
122+ } ) ;
123+ } ) ;
124+
125+ describe ( "User is not logged in" , ( ) => {
126+ beforeEach ( ( ) => {
127+ useAuth . mockImplementation ( ( ) => {
128+ return {
129+ user : null ,
130+ } ;
131+ } ) ;
132+ } ) ;
133+
134+ test ( "Error message is shown" , async ( ) => {
135+ render ( < GetMedication params = { { medication : "1" } } /> ) ;
136+ const errorText = await screen . findByText ( "Error 403 - Access Forbidden" ) ;
137+ const errorRedirectingText = await screen . findByText (
138+ "Redirecting to Login Page..."
139+ ) ;
140+ expect ( errorText ) . toBeInTheDocument ( ) ;
141+ expect ( errorRedirectingText ) . toBeInTheDocument ( ) ;
142+ } ) ;
121143} ) ;
0 commit comments