Skip to content

Commit bcc2a48

Browse files
Merge pull request #3057 from OneCommunityGlobal/sheetal-unit-tests-weekly-summaries-action
Sheetal M - Test cases for action weeklySummaries.js
2 parents a27b9f9 + 64beaf2 commit bcc2a48

1 file changed

Lines changed: 207 additions & 0 deletions

File tree

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
import axios from 'axios';
2+
import * as actions from '../../constants/weeklySummaries';
3+
import { ENDPOINTS } from '../../utils/URL';
4+
import { fetchWeeklySummariesBegin, fetchWeeklySummariesSuccess, fetchWeeklySummariesError, getWeeklySummaries, updateWeeklySummaries } from '../weeklySummaries'
5+
import { getUserProfileActionCreator } from '../../actions/userProfile';
6+
7+
8+
jest.mock('axios');
9+
10+
11+
describe('Weekly Summaries Action',() => {
12+
13+
it('Should return action FETCH_WEEKLY_SUMMARIES_BEGIN', () => {
14+
15+
const data = fetchWeeklySummariesBegin();
16+
17+
expect(data).toEqual( {type: actions.FETCH_WEEKLY_SUMMARIES_BEGIN });
18+
19+
});
20+
21+
it('Should fetch weekly summaries success', () => {
22+
23+
const weeklySummariesData = {
24+
id: 1,
25+
dueDate: "2024-12-29",
26+
summary: "Weekly Summary"
27+
};
28+
29+
const result = fetchWeeklySummariesSuccess(weeklySummariesData);
30+
31+
const expecteResult = {
32+
type: actions.FETCH_WEEKLY_SUMMARIES_SUCCESS,
33+
payload : {weeklySummariesData}
34+
}
35+
36+
expect(result).toEqual(expecteResult);
37+
38+
});
39+
40+
it('Should return action FETCH_WEEKLY_SUMMARIES_ERROR ', () => {
41+
42+
const error = {};
43+
const result = fetchWeeklySummariesError(error);
44+
45+
expect(result).toEqual({
46+
type: actions.FETCH_WEEKLY_SUMMARIES_ERROR,
47+
payload: {error}
48+
});
49+
})
50+
51+
});
52+
53+
describe('Weekly Summaries', () => {
54+
55+
jest.mock('axios');
56+
const dispatch = jest.fn();
57+
58+
beforeEach(() => {
59+
jest.clearAllMocks();
60+
});
61+
62+
describe('Get Weekly Summaries', () => {
63+
64+
it('Should dispatch actions and return status 200 on successful API call', async() => {
65+
66+
const mockData = {
67+
weeklySummariesCount : 1,
68+
weeklySummaries: [
69+
{
70+
id: "1",
71+
dueDate: "2024-12-29",
72+
summary: "Weekly Summary"
73+
}
74+
],
75+
mediaUrl: 'http://media.com',
76+
adminLinks: [{ Name: 'Media Folder', Link: 'http://newmedia.com' }]
77+
}
78+
79+
axios.get.mockResolvedValue({ data: mockData, status:200 });
80+
81+
const result = await getWeeklySummaries(1)(dispatch);
82+
83+
expect(dispatch).toHaveBeenCalledWith(fetchWeeklySummariesBegin());
84+
expect(dispatch).toHaveBeenCalledWith(fetchWeeklySummariesSuccess({
85+
weeklySummariesCount:1,
86+
weeklySummaries:[
87+
{
88+
id: "1",
89+
dueDate: "2024-12-29",
90+
summary: "Weekly Summary"
91+
}
92+
],
93+
mediaUrl: 'http://newmedia.com'
94+
}));
95+
96+
expect(dispatch).toHaveBeenCalledWith(getUserProfileActionCreator(mockData));
97+
expect(result).toBe(200);
98+
99+
});
100+
101+
it('Should dispatch an error action when GET request fails', async() => {
102+
103+
const mockError = { response : { status: 500 } };
104+
axios.get.mockRejectedValueOnce(mockError);
105+
106+
const result = await getWeeklySummaries(1)(dispatch);
107+
108+
expect(dispatch).toHaveBeenCalledWith(fetchWeeklySummariesBegin());
109+
expect(dispatch).toHaveBeenCalledWith(fetchWeeklySummariesError(mockError));
110+
expect(result).toBe(500);
111+
})
112+
113+
});
114+
115+
describe('Update Weekly Summaries', () => {
116+
117+
it('Should successfully update weekly summaries and return status 200', async() => {
118+
119+
const mockUserProfile = {
120+
firstName: 'User First Name',
121+
lastName: 'User Last Name',
122+
weeklySummariesCount : 0,
123+
weeklySummaries:[]
124+
}
125+
126+
const weeklySummariesData = {
127+
weeklySummariesCount : 1,
128+
weeklySummaries: [
129+
{
130+
id: "1",
131+
dueDate: "2025-01-05",
132+
summary: "Weekly Summary Week1"
133+
},
134+
],
135+
mediaUrl: 'http://media.com'
136+
}
137+
138+
axios.get.mockResolvedValue({ data: mockUserProfile });
139+
axios.put.mockResolvedValue({ status: 200 });
140+
141+
const result = await updateWeeklySummaries(1, weeklySummariesData )(dispatch);
142+
expect(result).toBe(200);
143+
expect(axios.get).toHaveBeenCalledWith(ENDPOINTS.USER_PROFILE(1));
144+
expect(axios.put).toHaveBeenCalled();
145+
expect(dispatch).toHaveBeenCalledWith(getUserProfileActionCreator({
146+
...mockUserProfile,
147+
'adminLinks' : [{ Name: 'Media Folder', Link: 'http://media.com' }] ,
148+
...weeklySummariesData
149+
}));
150+
151+
});
152+
153+
it('Ensure that if the "Media Folder" link already exists, it should be updated correctly.', async() => {
154+
155+
const mockUserProfile = {
156+
firstName: 'User First Name',
157+
lastName: 'User Last Name',
158+
weeklySummariesCount : 0,
159+
weeklySummaries:[],
160+
'adminLinks' : [{ Name: 'Media Folder', Link: 'http://media.com' }]
161+
}
162+
163+
const weeklySummariesData = {
164+
weeklySummariesCount : 1,
165+
weeklySummaries: [
166+
{
167+
id: "1",
168+
dueDate: "2025-01-05",
169+
summary: "Weekly Summary Week1"
170+
},
171+
],
172+
mediaUrl: 'http://newmedia.com'
173+
}
174+
175+
axios.get.mockResolvedValue({ data: mockUserProfile });
176+
axios.put.mockResolvedValue({ status: 200 });
177+
178+
const result = await updateWeeklySummaries(1, weeklySummariesData )(dispatch);
179+
expect(result).toBe(200);
180+
expect(axios.get).toHaveBeenCalledWith(ENDPOINTS.USER_PROFILE(1));
181+
expect(axios.put).toHaveBeenCalled();
182+
expect(dispatch).toHaveBeenCalledWith(getUserProfileActionCreator({
183+
...mockUserProfile,
184+
'adminLinks' : [{ Name: 'Media Folder', Link: 'http://newmedia.com' }] ,
185+
...weeklySummariesData
186+
}));
187+
188+
});
189+
190+
it('Should throw error when API request fails', async() => {
191+
192+
const mockError = { response : { status: 500 } };
193+
axios.get.mockRejectedValueOnce(mockError);
194+
195+
const result = await updateWeeklySummaries(1, {
196+
mediaUrl: 'http://newmedia.com',
197+
weeklySummaries: [],
198+
weeklySummariesCount: 2 })(dispatch);
199+
200+
expect(dispatch).not.toHaveBeenCalled();
201+
expect(result).toBe(500);
202+
203+
});
204+
205+
});
206+
207+
});

0 commit comments

Comments
 (0)