1+ import axios from "axios" ;
2+ import * as actions from '../../constants/weeklySummariesReport' ;
3+ import * as weeklySummaryReport from '../weeklySummariesReport' ;
4+ import { ENDPOINTS } from '../../utils/URL' ;
5+
6+ jest . mock ( 'axios' ) ;
7+
8+ describe ( 'Weekly Summaries Report Actions' , ( ) => {
9+
10+ it ( 'Should return action FETCH_SUMMARIES_REPORT_BEGIN' , ( ) => {
11+
12+ const data = weeklySummaryReport . fetchWeeklySummariesReportBegin ( ) ;
13+ expect ( data ) . toEqual ( { type : actions . FETCH_SUMMARIES_REPORT_BEGIN } ) ;
14+
15+ } ) ;
16+
17+ it ( 'Should return action FETCH_SUMMARIES_REPORT_SUCCESS' , ( ) => {
18+
19+ const weeklySummariesData = {
20+ id : 1 ,
21+ dueDate : "2024-12-29" ,
22+ summary : "Weekly Summary"
23+ } ;
24+
25+ const data = weeklySummaryReport . fetchWeeklySummariesReportSuccess ( weeklySummariesData ) ;
26+
27+ const expectedResult = {
28+ type : actions . FETCH_SUMMARIES_REPORT_SUCCESS ,
29+ payload : { weeklySummariesData }
30+ }
31+
32+ expect ( data ) . toEqual ( expectedResult ) ;
33+
34+ } ) ;
35+
36+ it ( 'Should return action FETCH_SUMMARIES_REPORT_ERROR' , ( ) => {
37+
38+ const error = { }
39+
40+ const data = weeklySummaryReport . fetchWeeklySummariesReportError ( error ) ;
41+
42+ expect ( data ) . toEqual ( {
43+ type :actions . FETCH_SUMMARIES_REPORT_ERROR ,
44+ payload : { error}
45+ } ) ;
46+
47+ } )
48+
49+ } ) ;
50+
51+ describe ( 'Weekly Summary Report' , ( ) => {
52+
53+ const dispatch = jest . fn ( ) ;
54+
55+ beforeEach ( ( ) => {
56+ jest . clearAllMocks ( ) ;
57+ } ) ;
58+
59+ describe ( 'Get Weekly Summaries Report' , ( ) => {
60+
61+ it ( 'Should dispatch actions and return 200 on successful API call ' , async ( ) => {
62+
63+ const mockData = {
64+ 'weeklySummaries' : [
65+ {
66+ id : "1" ,
67+ dueDate : "2024-12-29" ,
68+ summary : "Week1 Summary"
69+ }
70+ ]
71+ }
72+
73+ axios . get . mockResolvedValue ( { data : mockData , status :200 } ) ;
74+
75+ const result = await weeklySummaryReport . getWeeklySummariesReport ( ) ( dispatch ) ;
76+
77+ expect ( dispatch ) . toHaveBeenCalledWith ( weeklySummaryReport . fetchWeeklySummariesReportBegin ( ) ) ;
78+ expect ( dispatch ) . toHaveBeenCalledWith ( weeklySummaryReport . fetchWeeklySummariesReportSuccess ( result . data ) ) ;
79+ expect ( result . status ) . toBe ( 200 ) ;
80+
81+ } ) ;
82+
83+ it ( 'Should dispatch error action when GET request fails' , async ( ) => {
84+
85+ const mockError = { response : { status : 500 } } ;
86+ axios . get . mockRejectedValueOnce ( mockError ) ;
87+
88+ const result = await weeklySummaryReport . getWeeklySummariesReport ( ) ( dispatch ) ;
89+
90+ expect ( dispatch ) . toHaveBeenCalledWith ( weeklySummaryReport . fetchWeeklySummariesReportBegin ( ) ) ;
91+ expect ( dispatch ) . toHaveBeenCalledWith ( weeklySummaryReport . fetchWeeklySummariesReportError ( mockError ) )
92+ expect ( result ) . toBe ( 500 ) ;
93+
94+ } ) ;
95+
96+ } ) ;
97+
98+
99+ describe ( 'Update One Summary Report' , ( ) => {
100+
101+ let mockUserProfile , updatedField ;
102+
103+ beforeEach ( ( ) => {
104+
105+ mockUserProfile = {
106+ firstName : 'User First Name' ,
107+ lastName : 'User Last Name' ,
108+ weeklySummariesCount : 0 ,
109+ weeklySummaries :[
110+ {
111+ id : "1" ,
112+ dueDate : "2024-12-29" ,
113+ summary : "Weekly Summary Week1"
114+ }
115+ ]
116+ }
117+
118+ updatedField = {
119+
120+ weeklySummaries : [
121+ {
122+ id : "2" ,
123+ dueDate : "2025-01-05" ,
124+ summary : "Weekly Summary Week2"
125+ } ,
126+ ]
127+
128+ } ;
129+
130+ } ) ;
131+
132+ it ( 'Should successfully update user profile with weekly summary and dispatch UPDATE_SUMMARY_REPORT action ' , async ( ) => {
133+
134+ axios . get . mockResolvedValue ( { data : mockUserProfile } ) ;
135+ axios . put . mockResolvedValue ( {
136+ status : 200
137+ } ) ;
138+
139+ const result = await weeklySummaryReport . updateOneSummaryReport ( 1 , updatedField ) ( dispatch ) ;
140+ expect ( result . status ) . toBe ( 200 ) ;
141+ expect ( axios . get ) . toHaveBeenCalledWith ( ENDPOINTS . USER_PROFILE ( 1 ) ) ;
142+ expect ( axios . put ) . toHaveBeenCalled ( ) ;
143+ expect ( dispatch ) . toHaveBeenCalledWith ( weeklySummaryReport . updateSummaryReport ( { _id : 1 , updatedField } ) ) ;
144+
145+ } ) ;
146+
147+ it ( 'Should throw error when PUT request fails' , async ( ) => {
148+
149+ axios . get . mockResolvedValue ( { data : mockUserProfile } ) ;
150+ axios . put . mockResolvedValue ( {
151+ status : 500
152+ } ) ;
153+
154+ const result = weeklySummaryReport . updateOneSummaryReport ( 1 , updatedField ) ;
155+ await expect ( result ( dispatch ) ) . rejects . toThrow ( new Error ( 'An error occurred while attempting to save the changes to the profile.' ) ) ;
156+
157+ } ) ;
158+
159+ it ( 'Should throw error when GET request fails' , async ( ) => {
160+
161+ axios . get . mockRejectedValue ( new Error ( 'Failed to fetch user profile' ) ) ;
162+
163+ const result = weeklySummaryReport . updateOneSummaryReport ( 1 , updatedField ) ;
164+ await expect ( result ( dispatch ) ) . rejects . toThrow ( new Error ( 'Failed to fetch user profile' ) ) ;
165+
166+ } ) ;
167+
168+ } ) ;
169+ } ) ;
0 commit comments