Skip to content

Commit 6efa310

Browse files
test: added redux, selector and api cases
1 parent c3541a3 commit 6efa310

6 files changed

Lines changed: 97 additions & 52 deletions

File tree

package-lock.json

Lines changed: 46 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@
6262
"@fortawesome/free-solid-svg-icons": "6.3.0",
6363
"@fortawesome/react-fontawesome": "^0.2.0",
6464
"@reduxjs/toolkit": "1.9.5",
65+
"axios-mock-adapter": "1.21.4",
6566
"babel-polyfill": "6.26.0",
6667
"classnames": "2.3.2",
6768
"lodash": "4.17.21",
6869
"react-redux": "7.2.9",
6970
"react-responsive": "8.2.0",
71+
"react-router-dom": "5.3.4",
7072
"react-transition-group": "4.4.5",
71-
"timeago.js": "4.0.2",
72-
"react-router-dom": "5.3.4"
73+
"rosie": "2.1.0",
74+
"timeago.js": "4.0.2"
7375
},
7476
"peerDependencies": {
7577
"@edx/frontend-platform": "^4.0.0",

src/Notifications/data/api.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
import { camelCaseObject } from '@edx/frontend-platform';
2-
import notificationsList from './notifications.json';
1+
import { camelCaseObject, getConfig, snakeCaseObject } from '@edx/frontend-platform';
2+
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
3+
4+
export const getNotificationsCountApiUrl = () => `${getConfig().LMS_BASE_URL}/api/notifications/count/`;
5+
export const getNotificationsApiUrl = () => `${getConfig().LMS_BASE_URL}/api/notifications/`;
6+
export const markNotificationsSeenApiUrl = (appName) => `${getConfig().LMS_BASE_URL}/api/notifications/mark-notifications-unseen/${appName}/`;
7+
export const markAllNotificationsAsReadpiUrl = (appName, id) => `${getConfig().LMS_BASE_URL}/api/notifications/mark-notifications-read/${appName}/${id}`;
38

49
export async function getNotifications(appName, page, pageSize) {
5-
const { data } = notificationsList;
10+
const params = snakeCaseObject({ page, pageSize });
11+
const { data } = await getAuthenticatedHttpClient().get(getNotificationsApiUrl(), { params });
12+
613
const startIndex = (page - 1) * pageSize;
714
const endIndex = startIndex + pageSize;
815

@@ -11,30 +18,25 @@ export async function getNotifications(appName, page, pageSize) {
1118
}
1219

1320
export async function getNotificationCounts() {
14-
const data = {
15-
count: 45,
16-
count_by_app_name: {
17-
reminders: 10,
18-
discussions: 20,
19-
grades: 10,
20-
authoring: 5,
21-
},
22-
show_notification_tray: false,
23-
};
21+
const { data } = await getAuthenticatedHttpClient().get(getNotificationsCountApiUrl());
22+
2423
return camelCaseObject(data);
2524
}
2625

27-
export async function markNotificationSeen() {
28-
const data = [];
26+
export async function markNotificationSeen(appName) {
27+
const { data } = await getAuthenticatedHttpClient().put(`${markNotificationsSeenApiUrl(appName)}`);
28+
2929
return camelCaseObject(data);
3030
}
3131

32-
export async function markAllNotificationRead() {
33-
const { data } = camelCaseObject(notificationsList);
34-
return data;
32+
export async function markAllNotificationRead(appName) {
33+
const { data } = await getAuthenticatedHttpClient().put(`${markAllNotificationsAsReadpiUrl(appName)}`);
34+
35+
return camelCaseObject(data);
3536
}
3637

37-
export async function markNotificationRead(notificationId) {
38-
const { data } = camelCaseObject(notificationsList);
39-
return { data, id: notificationId };
38+
export async function markNotificationRead(appName, notificationId) {
39+
const { data } = await getAuthenticatedHttpClient().put(`${markAllNotificationsAsReadpiUrl(appName, notificationId)}`);
40+
41+
return camelCaseObject({ data, id: notificationId });
4042
}

src/Notifications/data/redux.test.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ describe('Notification Redux', () => {
4040
store = initializeStore();
4141

4242
axiosMock.onGet(notificationCountsApiUrl).reply(200, (Factory.build('notificationsCount')));
43-
axiosMock.onGet(notificationsApiUrl).reply(200, (Factory.buildList('notification', 2, null)));
43+
axiosMock.onGet(notificationsApiUrl).reply(
44+
200,
45+
(Factory.buildList('notification', 2, null, { createdDate: new Date().toISOString() })),
46+
);
4447
await executeThunk(fetchAppsNotificationCount(), store.dispatch, store.getState);
4548
await executeThunk(fetchNotificationList({ page: 1, pageSize: 10 }), store.dispatch, store.getState);
4649
});
@@ -68,9 +71,9 @@ describe('Notification Redux', () => {
6871
});
6972

7073
it('successfully loaded notifications list in the redux.', async () => {
71-
const state = store.getState();
74+
const { notifications: { notifications } } = store.getState();
7275

73-
expect(Object.keys(state.notifications.notifications)).toHaveLength(2);
76+
expect(Object.keys(notifications)).toHaveLength(2);
7477
});
7578

7679
it('successfully loaded notification counts in the redux.', async () => {
@@ -84,9 +87,9 @@ describe('Notification Redux', () => {
8487
});
8588

8689
it('successfully loaded showNotificationTray status in the redux based on api.', async () => {
87-
const state = store.getState();
90+
const { notifications: { showNotificationTray } } = store.getState();
8891

89-
expect(state.notifications.showNotificationTray).toEqual(true);
92+
expect(showNotificationTray).toEqual(true);
9093
});
9194

9295
it('successfully store the count, numPages, currentPage, and nextPage data in redux.', async () => {
@@ -98,15 +101,15 @@ describe('Notification Redux', () => {
98101
});
99102

100103
it('successfully updated the selected app name in redux.', async () => {
101-
const state = store.getState();
104+
const { notifications: { appName } } = store.getState();
102105

103-
expect(state.notifications.appName).toEqual('discussions');
106+
expect(appName).toEqual('discussions');
104107
});
105108

106109
it('successfully store notification ids in the selected app in apps.', async () => {
107-
const state = store.getState();
110+
const { notifications: { apps } } = store.getState();
108111

109-
expect(state.notifications.apps.discussions).toHaveLength(2);
112+
expect(apps.discussions).toHaveLength(2);
110113
});
111114

112115
it('successfully marked all notifications as seen for selected app.', async () => {
@@ -120,21 +123,21 @@ describe('Notification Redux', () => {
120123
axiosMock.onPut(markedAllNotificationsAsReadApiUrl).reply(200);
121124
await executeThunk(markAllNotificationsAsRead('discussions'), store.dispatch, store.getState);
122125

123-
const { notifications } = store.getState();
124-
const firstNotification = Object.values(notifications.notifications)[0];
126+
const { notifications: { notificationStatus, notifications } } = store.getState();
127+
const firstNotification = Object.values(notifications)[0];
125128

126-
expect(notifications.notificationStatus).toEqual('successful');
129+
expect(notificationStatus).toEqual('successful');
127130
expect(firstNotification.lastRead).not.toBeNull();
128131
});
129132

130133
it('successfully marked notification as read in the redux.', async () => {
131134
axiosMock.onPut(markedNotificationAsReadApiUrl).reply(200);
132135
await executeThunk(markNotificationsAsRead('discussions', 1), store.dispatch, store.getState);
133136

134-
const { notifications } = store.getState();
135-
const firstNotification = Object.values(notifications.notifications)[0];
137+
const { notifications: { notificationStatus, notifications } } = store.getState();
138+
const firstNotification = Object.values(notifications)[0];
136139

137-
expect(notifications.notificationStatus).toEqual('successful');
140+
expect(notificationStatus).toEqual('successful');
138141
expect(firstNotification.lastRead).not.toBeNull();
139142
});
140143
});

src/Notifications/data/selector.test.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ describe('Notification Selectors', () => {
4343
store = initializeStore();
4444

4545
axiosMock.onGet(notificationCountsApiUrl).reply(200, (Factory.build('notificationsCount')));
46-
axiosMock.onGet(notificationsApiUrl).reply(200, (Factory.buildList('notification', 2, null)));
46+
axiosMock.onGet(notificationsApiUrl).reply(
47+
200,
48+
(Factory.buildList('notification', 2, null, { createdDate: new Date().toISOString() })),
49+
);
4750
await executeThunk(fetchAppsNotificationCount(), store.dispatch, store.getState);
4851
await executeThunk(fetchNotificationList({ page: 1, pageSize: 10 }), store.dispatch, store.getState);
4952
});

src/Notifications/data/thunks.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ export const markAllNotificationsAsRead = (appName) => (
9797
}
9898
);
9999

100-
export const markNotificationsAsRead = (notificationId) => (
100+
export const markNotificationsAsRead = (appName, notificationId) => (
101101
async (dispatch) => {
102102
try {
103103
dispatch(markNotificationsAsReadRequest({ notificationId }));
104-
const data = await markNotificationRead(notificationId);
104+
const data = await markNotificationRead(appName, notificationId);
105105
dispatch(markNotificationsAsReadSuccess(data));
106106
} catch (error) {
107107
if (getHttpErrorStatus(error) === 403) {

0 commit comments

Comments
 (0)