1
1
import React from 'react' ;
2
2
3
+ import { fireEvent } from '@testing-library/react' ;
3
4
import MockAdapter from 'axios-mock-adapter' ;
4
5
5
6
import { getConfig } from '@edx/frontend-platform' ;
6
- import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth' ;
7
7
import { sendTrackEvent } from '@edx/frontend-platform/analytics' ;
8
+ import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth' ;
8
9
10
+ import { getSessionStorage , setSessionStorage } from '../../../../../../data/sessionStorage' ;
9
11
import {
10
12
initializeMockApp , initializeTestStore , render , screen ,
11
13
} from '../../../../../../setupTest' ;
@@ -14,11 +16,19 @@ import { buildTopicsFromUnits } from '../../../../../data/__factories__/discussi
14
16
import { getCourseDiscussionTopics } from '../../../../../data/thunks' ;
15
17
import SidebarContext from '../../../SidebarContext' ;
16
18
import DiscussionsNotificationsSidebar from '../DiscussionsNotificationsSidebar' ;
19
+ import DiscussionsNotificationsTrigger from '../DiscussionsNotificationsTrigger' ;
17
20
import DiscussionsWidget from './DiscussionsWidget' ;
18
21
19
22
initializeMockApp ( ) ;
20
23
jest . mock ( '@edx/frontend-platform/analytics' ) ;
21
24
25
+ jest . mock ( '../../../../../../data/sessionStorage' , ( ) => ( {
26
+ getSessionStorage : jest . fn ( ) ,
27
+ setSessionStorage : jest . fn ( ) ,
28
+ } ) ) ;
29
+
30
+ const onClickMock = jest . fn ( ) ;
31
+
22
32
describe ( 'DiscussionsWidget' , ( ) => {
23
33
let axiosMock ;
24
34
let mockData ;
@@ -81,4 +91,34 @@ describe('DiscussionsWidget', () => {
81
91
expect ( screen . queryByText ( 'Back to course' ) ) . toBeInTheDocument ( ) ;
82
92
expect ( sendTrackEvent ) . toHaveBeenCalledTimes ( 1 ) ;
83
93
} ) ;
94
+
95
+ it ( 'should open notification tray if closed' , ( ) => {
96
+ ( getSessionStorage as jest . Mock ) . mockReturnValue ( 'closed' ) ;
97
+
98
+ renderWithProvider ( ( ) => < DiscussionsNotificationsTrigger onClick = { onClickMock } /> ) ;
99
+
100
+ const button = screen . getByRole ( 'button' ) ;
101
+ fireEvent . click ( button ) ;
102
+
103
+ expect ( setSessionStorage ) . toHaveBeenCalledWith (
104
+ `notificationTrayStatus.${ courseId } ` ,
105
+ 'open' ,
106
+ ) ;
107
+ expect ( onClickMock ) . toHaveBeenCalled ( ) ;
108
+ } ) ;
109
+
110
+ it ( 'should close notification tray if open' , ( ) => {
111
+ ( getSessionStorage as jest . Mock ) . mockReturnValue ( 'open' ) ;
112
+
113
+ renderWithProvider ( ( ) => < DiscussionsNotificationsTrigger onClick = { onClickMock } /> ) ;
114
+
115
+ const button = screen . getByRole ( 'button' ) ;
116
+ fireEvent . click ( button ) ;
117
+
118
+ expect ( setSessionStorage ) . toHaveBeenCalledWith (
119
+ `notificationTrayStatus.${ courseId } ` ,
120
+ 'open' ,
121
+ ) ;
122
+ expect ( onClickMock ) . toHaveBeenCalled ( ) ;
123
+ } ) ;
84
124
} ) ;
0 commit comments