|
| 1 | +import MockAdapter from 'axios-mock-adapter'; |
| 2 | +import { Factory } from 'rosie'; |
| 3 | + |
| 4 | +import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; |
| 5 | +import { initializeMockApp } from '@edx/frontend-platform/testing'; |
| 6 | + |
| 7 | +import { initializeStore } from '../../store'; |
| 8 | +import executeThunk from '../../test-utils'; |
| 9 | +import { |
| 10 | + getNotificationsApiUrl, getNotificationsCountApiUrl, markAllNotificationsAsReadpiUrl, markNotificationsSeenApiUrl, |
| 11 | + getNotificationCounts, getNotifications, markNotificationSeen, markAllNotificationRead, markNotificationRead, |
| 12 | +} from './api'; |
| 13 | +import { |
| 14 | + fetchAppsNotificationCount, |
| 15 | + fetchNotificationList, |
| 16 | + markAllNotificationsAsRead, |
| 17 | + markNotificationsAsRead, |
| 18 | + markNotificationsAsSeen, |
| 19 | +} from './thunks'; |
| 20 | + |
| 21 | +import './__factories__'; |
| 22 | + |
| 23 | +const notificationCountsApiUrl = getNotificationsCountApiUrl(); |
| 24 | +const notificationsApiUrl = getNotificationsApiUrl(); |
| 25 | +const markedAllNotificationsAsSeenApiUrl = markNotificationsSeenApiUrl('discussions'); |
| 26 | +const markedAllNotificationsAsReadApiUrl = markAllNotificationsAsReadpiUrl('discussions'); |
| 27 | +const markedNotificationAsReadApiUrl = markAllNotificationsAsReadpiUrl('discussions', 1); |
| 28 | + |
| 29 | +let axiosMock = null; |
| 30 | +let store; |
| 31 | + |
| 32 | +describe('Notifications API', () => { |
| 33 | + beforeEach(async () => { |
| 34 | + initializeMockApp({ |
| 35 | + authenticatedUser: { |
| 36 | + userId: '123abc', |
| 37 | + username: 'testuser', |
| 38 | + administrator: false, |
| 39 | + roles: [], |
| 40 | + }, |
| 41 | + }); |
| 42 | + axiosMock = new MockAdapter(getAuthenticatedHttpClient()); |
| 43 | + Factory.resetAll(); |
| 44 | + store = initializeStore(); |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(() => { |
| 48 | + axiosMock.reset(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('successfully get notification counts for different tabs.', async () => { |
| 52 | + axiosMock.onGet(notificationCountsApiUrl).reply(200, (Factory.build('notificationsCount'))); |
| 53 | + |
| 54 | + const { count, countByAppName } = await getNotificationCounts(); |
| 55 | + |
| 56 | + expect(count).toEqual(45); |
| 57 | + expect(countByAppName.reminders).toEqual(10); |
| 58 | + expect(countByAppName.discussions).toEqual(20); |
| 59 | + expect(countByAppName.grades).toEqual(10); |
| 60 | + expect(countByAppName.authoring).toEqual(5); |
| 61 | + }); |
| 62 | + |
| 63 | + it('failed to get notification counts.', async () => { |
| 64 | + axiosMock.onGet(notificationCountsApiUrl).reply(404); |
| 65 | + await executeThunk(fetchAppsNotificationCount(), store.dispatch, store.getState); |
| 66 | + |
| 67 | + expect(store.getState().notifications.notificationStatus).toEqual('failed'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('denied to get notification counts.', async () => { |
| 71 | + axiosMock.onGet(notificationCountsApiUrl).reply(403, {}); |
| 72 | + await executeThunk(fetchAppsNotificationCount(), store.dispatch); |
| 73 | + |
| 74 | + expect(store.getState().notifications.notificationStatus).toEqual('denied'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('successfully get notifications.', async () => { |
| 78 | + axiosMock.onGet(notificationsApiUrl).reply( |
| 79 | + 200, |
| 80 | + (Factory.buildList('notification', 2, null, { createdDate: new Date().toISOString() })), |
| 81 | + ); |
| 82 | + |
| 83 | + const { notifications } = await getNotifications('discussions', 1, 10); |
| 84 | + |
| 85 | + expect(notifications).toHaveLength(2); |
| 86 | + }); |
| 87 | + |
| 88 | + it('failed to get notifications.', async () => { |
| 89 | + axiosMock.onGet(notificationsApiUrl).reply(404); |
| 90 | + await executeThunk(fetchNotificationList({ page: 1, pageSize: 10 }), store.dispatch, store.getState); |
| 91 | + |
| 92 | + expect(store.getState().notifications.notificationStatus).toEqual('failed'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('denied to get notifications.', async () => { |
| 96 | + axiosMock.onGet(notificationsApiUrl).reply(403, {}); |
| 97 | + await executeThunk(fetchNotificationList({ page: 1, pageSize: 10 }), store.dispatch); |
| 98 | + |
| 99 | + expect(store.getState().notifications.notificationStatus).toEqual('denied'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('successfully marked all notifications as seen for selected app.', async () => { |
| 103 | + axiosMock.onPut(markedAllNotificationsAsSeenApiUrl).reply(200, { message: 'Notifications marked seen.' }); |
| 104 | + |
| 105 | + const { message } = await markNotificationSeen('discussions'); |
| 106 | + |
| 107 | + expect(message).toEqual('Notifications marked seen.'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('failed to mark all notifications as seen for selected app.', async () => { |
| 111 | + axiosMock.onPut(markedAllNotificationsAsSeenApiUrl).reply(404); |
| 112 | + await executeThunk(markNotificationsAsSeen('discussions'), store.dispatch, store.getState); |
| 113 | + |
| 114 | + expect(store.getState().notifications.notificationStatus).toEqual('failed'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('denied to mark all notifications as seen for selected app.', async () => { |
| 118 | + axiosMock.onPut(markedAllNotificationsAsSeenApiUrl).reply(403, {}); |
| 119 | + await executeThunk(markNotificationsAsSeen('discussions'), store.dispatch); |
| 120 | + |
| 121 | + expect(store.getState().notifications.notificationStatus).toEqual('denied'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('successfully marked all notifications as read for selected app.', async () => { |
| 125 | + axiosMock.onPut(markedAllNotificationsAsReadApiUrl).reply(200, { message: 'Notifications marked read.' }); |
| 126 | + |
| 127 | + const { message } = await markAllNotificationRead('discussions'); |
| 128 | + |
| 129 | + expect(message).toEqual('Notifications marked read.'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('failed to mark all notifications as read for selected app.', async () => { |
| 133 | + axiosMock.onPut(markedAllNotificationsAsReadApiUrl).reply(404); |
| 134 | + await executeThunk(markAllNotificationsAsRead('discussions'), store.dispatch, store.getState); |
| 135 | + |
| 136 | + expect(store.getState().notifications.notificationStatus).toEqual('failed'); |
| 137 | + }); |
| 138 | + |
| 139 | + it('denied to mark all notifications as read for selected app.', async () => { |
| 140 | + axiosMock.onPut(markedAllNotificationsAsReadApiUrl).reply(403, {}); |
| 141 | + await executeThunk(markAllNotificationsAsRead('discussions'), store.dispatch); |
| 142 | + |
| 143 | + expect(store.getState().notifications.notificationStatus).toEqual('denied'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('successfully marked notification as read.', async () => { |
| 147 | + axiosMock.onPut(markedNotificationAsReadApiUrl).reply(200, { message: 'Notification marked read.' }); |
| 148 | + |
| 149 | + const { data } = await markNotificationRead('discussions', 1); |
| 150 | + |
| 151 | + expect(data.message).toEqual('Notification marked read.'); |
| 152 | + }); |
| 153 | + |
| 154 | + it('failed to mark notification as read .', async () => { |
| 155 | + axiosMock.onPut(markedNotificationAsReadApiUrl).reply(404); |
| 156 | + await executeThunk(markNotificationsAsRead('discussions', 1), store.dispatch, store.getState); |
| 157 | + |
| 158 | + expect(store.getState().notifications.notificationStatus).toEqual('failed'); |
| 159 | + }); |
| 160 | + |
| 161 | + it('denied to mark notification as read.', async () => { |
| 162 | + axiosMock.onPut(markedNotificationAsReadApiUrl).reply(403, {}); |
| 163 | + await executeThunk(markNotificationsAsRead('discussions', 1), store.dispatch); |
| 164 | + |
| 165 | + expect(store.getState().notifications.notificationStatus).toEqual('denied'); |
| 166 | + }); |
| 167 | +}); |
0 commit comments