@@ -7,6 +7,8 @@ import { sendTrackEvent, sendTrackingLogEvent } from '@edx/frontend-platform/ana
7
7
8
8
import { initializeMockApp , initializeTestStore } from '@src/setupTest' ;
9
9
import SidebarUnit from './SidebarUnit' ;
10
+ import SidebarContext from '../../../SidebarContext' ;
11
+ import { ID } from '../constants' ;
10
12
11
13
jest . mock ( '@edx/frontend-platform/analytics' , ( ) => ( {
12
14
sendTrackEvent : jest . fn ( ) ,
@@ -20,6 +22,13 @@ describe('<SidebarUnit />', () => {
20
22
let unit ;
21
23
let sequenceId ;
22
24
25
+ // A default context for desktop mode.
26
+ const defaultSidebarContext = {
27
+ toggleSidebar : jest . fn ( ) ,
28
+ shouldDisplayFullScreen : false ,
29
+ currentSidebar : ID ,
30
+ } ;
31
+
23
32
const initTestStore = async ( options ) => {
24
33
store = await initializeTestStore ( options ) ;
25
34
const state = store . getState ( ) ;
@@ -28,21 +37,23 @@ describe('<SidebarUnit />', () => {
28
37
unit = state . courseware . courseOutline . units [ sequence . unitIds [ 0 ] ] ;
29
38
} ;
30
39
31
- function renderWithProvider ( props = { } ) {
40
+ function renderWithProvider ( props = { } , sidebarContext = defaultSidebarContext ) {
32
41
const { container } = render (
33
42
< AppProvider store = { store } wrapWithRouter = { false } >
34
43
< IntlProvider locale = "en" >
35
44
< MemoryRouter >
36
- < SidebarUnit
37
- isFirst
38
- id = { unit . id }
39
- courseId = "course123"
40
- sequenceId = { sequenceId }
41
- unit = { { ...unit , icon : 'video' , isLocked : false } }
42
- isActive = { false }
43
- activeUnitId = { unit . id }
44
- { ...props }
45
- />
45
+ < SidebarContext . Provider value = { sidebarContext } >
46
+ < SidebarUnit
47
+ isFirst
48
+ id = { unit . id }
49
+ courseId = "course123"
50
+ sequenceId = { sequenceId }
51
+ unit = { { ...unit , icon : 'video' , isLocked : false } }
52
+ isActive = { false }
53
+ activeUnitId = { unit . id }
54
+ { ...props }
55
+ />
56
+ </ SidebarContext . Provider >
46
57
</ MemoryRouter >
47
58
</ IntlProvider >
48
59
</ AppProvider > ,
@@ -87,21 +98,42 @@ describe('<SidebarUnit />', () => {
87
98
expect ( screen . getByText ( unit . title ) ) . toBeInTheDocument ( ) ;
88
99
} ) ;
89
100
90
- it ( 'sends log event correctly when unit is clicked' , async ( ) => {
91
- await initTestStore ( ) ;
92
- renderWithProvider ( { unit : { ...unit } } ) ;
93
- const logData = {
94
- id : unit . id ,
95
- current_tab : 1 ,
96
- tab_count : 1 ,
97
- target_id : unit . id ,
98
- target_tab : 1 ,
99
- widget_placement : 'left' ,
100
- } ;
101
-
102
- userEvent . click ( screen . getByText ( unit . title ) ) ;
103
-
104
- expect ( sendTrackEvent ) . toHaveBeenCalledWith ( 'edx.ui.lms.sequence.tab_selected' , logData ) ;
105
- expect ( sendTrackingLogEvent ) . toHaveBeenCalledWith ( 'edx.ui.lms.sequence.tab_selected' , logData ) ;
101
+ describe ( 'When a unit is clicked' , ( ) => {
102
+ it ( 'sends log event correctly' , async ( ) => {
103
+ await initTestStore ( ) ;
104
+ renderWithProvider ( { unit : { ...unit } } ) ;
105
+ const logData = {
106
+ id : unit . id ,
107
+ current_tab : 1 ,
108
+ tab_count : 1 ,
109
+ target_id : unit . id ,
110
+ target_tab : 1 ,
111
+ widget_placement : 'left' ,
112
+ } ;
113
+
114
+ userEvent . click ( screen . getByText ( unit . title ) ) ;
115
+
116
+ expect ( sendTrackEvent ) . toHaveBeenCalledWith ( 'edx.ui.lms.sequence.tab_selected' , logData ) ;
117
+ expect ( sendTrackingLogEvent ) . toHaveBeenCalledWith ( 'edx.ui.lms.sequence.tab_selected' , logData ) ;
118
+ } ) ;
119
+
120
+ it ( 'leaves sidebar open in desktop mode' , async ( ) => {
121
+ await initTestStore ( ) ;
122
+ renderWithProvider ( { unit : { ...unit } } ) ;
123
+ userEvent . click ( screen . getByText ( unit . title ) ) ;
124
+
125
+ expect ( defaultSidebarContext . toggleSidebar ) . not . toHaveBeenCalled ( ) ;
126
+ expect ( window . sessionStorage . getItem ( 'hideCourseOutlineSidebar' ) ) . toBeNull ( ) ;
127
+ } ) ;
128
+
129
+ it ( 'closes sidebar on mobile devices' , async ( ) => {
130
+ await initTestStore ( ) ;
131
+ renderWithProvider ( { unit : { ...unit } } , { ...defaultSidebarContext , shouldDisplayFullScreen : true } ) ;
132
+ userEvent . click ( screen . getByText ( unit . title ) ) ;
133
+
134
+ expect ( defaultSidebarContext . toggleSidebar ) . toHaveBeenCalledTimes ( 1 ) ;
135
+ expect ( defaultSidebarContext . toggleSidebar ) . toHaveBeenCalledWith ( null ) ;
136
+ expect ( window . sessionStorage . getItem ( 'hideCourseOutlineSidebar' ) ) . toEqual ( 'true' ) ;
137
+ } ) ;
106
138
} ) ;
107
139
} ) ;
0 commit comments