@@ -22,6 +22,8 @@ beforeAll(() => {
2222} ) ;
2323
2424describe ( 'list_movies_data' , ( ) => {
25+ let mockElements ;
26+
2527 beforeEach ( ( ) => {
2628 jest . clearAllMocks ( ) ;
2729 // Assign global variables from globalData
@@ -36,6 +38,18 @@ describe('list_movies_data', () => {
3638 global . UNDELETE_BUTTON = globalData . UNDELETE_BUTTON ;
3739 global . TABLE_HEAD = globalData . TABLE_HEAD ;
3840 global . user_primary_course_id = parseInt ( globalData . user_primary_course_id , 10 ) ;
41+
42+ // Mock document.querySelector to return mock elements with innerHTML setter
43+ mockElements = { } ;
44+ document . querySelector = jest . fn ( ( selector ) => {
45+ if ( ! mockElements [ selector ] ) {
46+ mockElements [ selector ] = { innerHTML : '' , style : { } } ;
47+ }
48+ return mockElements [ selector ] ;
49+ } ) ;
50+
51+ // Mock document.querySelectorAll for hiding movie players
52+ document . querySelectorAll = jest . fn ( ( ) => [ ] ) ;
3953 } ) ;
4054
4155 test ( 'should create the correct HTML for published movies' , ( ) => {
@@ -57,25 +71,25 @@ describe('list_movies_data', () => {
5771 }
5872 ] ;
5973
60- $ . fn . html = jest . fn ( ) ;
6174 list_movies_data ( movies ) ;
62- expect ( $ . fn . html ) . toHaveBeenCalledTimes ( 4 ) ;
63-
64- const expectedPublishedHtml = expect . stringContaining ( "<table><tbody><tr><td><i>" ) ;
65- expect ( $ . fn . html ) . toHaveBeenCalledWith ( expectedPublishedHtml ) ;
75+ expect ( document . querySelector ) . toHaveBeenCalledTimes ( 4 ) ;
76+
77+ // Check that the published movies div was populated
78+ const publishedHtml = mockElements [ '#your-published-movies' ] . innerHTML ;
79+ expect ( publishedHtml ) . toContain ( "<table>" ) ;
80+ expect ( publishedHtml ) . toContain ( "Movie Title" ) ;
6681 } ) ;
6782
6883 test ( 'should handle an empty list of movies gracefully' , ( ) => {
6984 const movies = [ ] ;
7085
71- $ . fn . html = jest . fn ( ) ;
7286 list_movies_data ( movies ) ;
73- expect ( $ . fn . html ) . toHaveBeenCalledTimes ( 4 ) ;
87+ expect ( document . querySelector ) . toHaveBeenCalledTimes ( 4 ) ;
7488
75- // Simson broke this with the dynamodb update. It needs to be fixed.
76- // const expectedEmptyHtml = expect.stringContaining('<table><tbody><tr><td><i>No movies</i></td></tr></tbody></table>') ;
77- const expectedEmptyHtml = expect . stringContaining ( "<table><tbody><tr><td><i> No movies</i></td></tr><tr><td colspan=\"6\"><a href=\"/upload\">Click here to upload a movie</a></td></tr></tbody></table> ") ;
78- expect ( $ . fn . html ) . toHaveBeenCalledWith ( expectedEmptyHtml ) ;
89+ // Check that empty movies show the right message
90+ const publishedHtml = mockElements [ '#your-published- movies' ] . innerHTML ;
91+ expect ( publishedHtml ) . toContain ( " No movies") ;
92+ expect ( publishedHtml ) . toContain ( '<a href="/upload">Click here to upload a movie</a>' ) ;
7993 } ) ;
8094
8195 test ( 'should correctly classify and display course movies' , ( ) => {
@@ -98,36 +112,35 @@ describe('list_movies_data', () => {
98112 }
99113 ] ;
100114
101- $ . fn . html = jest . fn ( ) ;
102115 list_movies_data ( movies ) ;
103- expect ( $ . fn . html ) . toHaveBeenCalledTimes ( 4 ) ;
116+ expect ( document . querySelector ) . toHaveBeenCalledTimes ( 4 ) ;
104117
105- const expectedCourseHtml = expect . stringContaining ( '<table>' ) ;
106- expect ( $ . fn . html ) . toHaveBeenCalledWith ( expectedCourseHtml ) ;
118+ // Check that course movies are shown in the right section
119+ const courseHtml = mockElements [ '#course-movies' ] . innerHTML ;
120+ expect ( courseHtml ) . toContain ( '<table>' ) ;
107121 } ) ;
108122
109123 test ( 'should display a link to upload movies for non-demo users' , ( ) => {
110124 const movies = [ ] ;
111125 global . demo_mode = false ;
112- $ . fn . html = jest . fn ( ) ;
113126 list_movies_data ( movies ) ;
114- expect ( $ . fn . html ) . toHaveBeenCalledTimes ( 4 ) ;
127+ expect ( document . querySelector ) . toHaveBeenCalledTimes ( 4 ) ;
115128
116- const expectedUploadLinkHtml = expect . stringContaining ( '<table><tbody><tr><td><i>No movies</i></td></tr><tr><td colspan="6"><a href="/upload">Click here to upload a movie</a></td></tr></tbody></table>' ) ;
117- expect ( $ . fn . html ) . toHaveBeenCalledWith ( expectedUploadLinkHtml ) ;
129+ const publishedHtml = mockElements [ '#your-published-movies' ] . innerHTML ;
130+ expect ( publishedHtml ) . toContain ( 'No movies' ) ;
131+ expect ( publishedHtml ) . toContain ( '<a href="/upload">Click here to upload a movie</a>' ) ;
118132 } ) ;
119133
120134 test ( 'should not display upload link for demo users' , ( ) => {
121135 const movies = [ ] ;
122136
123137 global . demo_mode = true ;
124138
125- $ . fn . html = jest . fn ( ) ;
126139 list_movies_data ( movies ) ;
127- expect ( $ . fn . html ) . toHaveBeenCalledTimes ( 4 ) ;
140+ expect ( document . querySelector ) . toHaveBeenCalledTimes ( 4 ) ;
128141
129- const notExpectedUploadLinkHtml = expect . not . stringContaining ( 'Click here to upload a movie' ) ;
130- expect ( $ . fn . html ) . toHaveBeenCalledWith ( notExpectedUploadLinkHtml ) ;
142+ const publishedHtml = mockElements [ '#your-published-movies' ] . innerHTML ;
143+ expect ( publishedHtml ) . not . toContain ( 'Click here to upload a movie' ) ;
131144 } ) ;
132145
133146 // Action Button Tests
@@ -142,14 +155,20 @@ describe('list_movies_data', () => {
142155 published : 0 ,
143156 deleted : 0 ,
144157 title : 'Unpublished Movie' ,
158+ date_uploaded : 1627689600 ,
159+ width : 1280 ,
160+ height : 720 ,
161+ total_bytes : 2000000 ,
162+ fps : '30' ,
163+ total_frames : 3600 ,
164+ orig_movie : false ,
145165 }
146166 ] ;
147167
148- $ . fn . html = jest . fn ( ) ;
149168 list_movies_data ( movies ) ;
150169
151- const expectedButtonHtml = expect . stringContaining ( `<td> Status: Not published<br/>` ) ;
152- expect ( $ . fn . html ) . toHaveBeenCalledWith ( expectedButtonHtml ) ;
170+ const unpublishedHtml = mockElements [ '#your-unpublished-movies' ] . innerHTML ;
171+ expect ( unpublishedHtml ) . toContain ( 'Status: Not published' ) ;
153172 } ) ;
154173
155174 test ( 'should display an unpublish button for published movies' , ( ) => {
@@ -160,13 +179,19 @@ describe('list_movies_data', () => {
160179 published : 1 ,
161180 deleted : 0 ,
162181 title : 'Published Movie' ,
182+ date_uploaded : 1627689600 ,
183+ width : 1280 ,
184+ height : 720 ,
185+ total_bytes : 2000000 ,
186+ fps : '30' ,
187+ total_frames : 3600 ,
188+ orig_movie : false ,
163189 }
164190 ] ;
165191
166- $ . fn . html = jest . fn ( ) ;
167192 list_movies_data ( movies ) ;
168193
169- const expectedButtonHtml = expect . stringContaining ( `<td> Status: <b>Published</b>` ) ;
170- expect ( $ . fn . html ) . toHaveBeenCalledWith ( expectedButtonHtml ) ;
194+ const publishedHtml = mockElements [ '#your-published-movies' ] . innerHTML ;
195+ expect ( publishedHtml ) . toContain ( 'Status: <b>Published</b>' ) ;
171196 } ) ;
172197} ) ;
0 commit comments