@@ -3,6 +3,7 @@ import FileSaver from 'file-saver';
3
3
4
4
import { selectors } from 'data/redux' ;
5
5
import { RequestKeys } from 'data/constants/requests' ;
6
+ import api from 'data/services/lms/api' ;
6
7
import * as download from './download' ;
7
8
8
9
const mockBlobWriter = jest . fn ( ) . mockName ( 'BlobWriter' ) ;
@@ -38,7 +39,7 @@ jest.mock('data/redux', () => ({
38
39
selectors : {
39
40
grading : {
40
41
selected : {
41
- response : jest . fn ( ) ,
42
+ submissionUUID : jest . fn ( ) ,
42
43
username : jest . fn ( ) ,
43
44
} ,
44
45
} ,
@@ -57,13 +58,13 @@ describe('download thunkActions', () => {
57
58
} ) ;
58
59
const files = [ mockFile ( 'test-file1.jpg' ) , mockFile ( 'test-file2.pdf' ) ] ;
59
60
const blobs = [ 'blob1' , 'blob2' ] ;
60
- const response = { files } ;
61
+ const submissionUUID = 'submission-uuid' ;
61
62
const username = 'student-name' ;
62
63
let dispatch ;
63
64
const getState = ( ) => testState ;
64
65
describe ( 'genManifest' , ( ) => {
65
66
test ( 'returns a list of strings with filename and description for each file' , ( ) => {
66
- expect ( download . genManifest ( response . files ) ) . toEqual (
67
+ expect ( download . genManifest ( files ) ) . toEqual (
67
68
[
68
69
`Filename: ${ files [ 0 ] . name } \nDescription: ${ files [ 0 ] . description } \nSize: ${ files [ 0 ] . size } ` ,
69
70
`Filename: ${ files [ 1 ] . name } \nDescription: ${ files [ 1 ] . description } \nSize: ${ files [ 0 ] . size } ` ,
@@ -86,43 +87,25 @@ describe('download thunkActions', () => {
86
87
} ) ;
87
88
} ) ;
88
89
} ) ;
89
-
90
- describe ( 'getTimeStampUrl' , ( ) => {
91
- it ( 'generate different url every milisecond for cache busting' , ( ) => {
92
- const testUrl = 'test/url?param1=true' ;
93
- const firstGen = download . getTimeStampUrl ( testUrl ) ;
94
- // fast forward for 1 milisecond
95
- jest . advanceTimersByTime ( 1 ) ;
96
- const secondGen = download . getTimeStampUrl ( testUrl ) ;
97
- expect ( firstGen ) . not . toEqual ( secondGen ) ;
98
- } ) ;
99
- } ) ;
100
-
101
90
describe ( 'downloadFile' , ( ) => {
102
91
let fetch ;
103
- let getTimeStampUrl ;
104
92
const blob = 'test-blob' ;
105
93
const file = files [ 0 ] ;
106
94
beforeEach ( ( ) => {
107
95
fetch = window . fetch ;
108
96
window . fetch = jest . fn ( ) ;
109
- getTimeStampUrl = download . getTimeStampUrl ;
110
- download . getTimeStampUrl = jest . fn ( ) ;
111
97
} ) ;
112
98
afterEach ( ( ) => {
113
99
window . fetch = fetch ;
114
- download . getTimeStampUrl = getTimeStampUrl ;
115
100
} ) ;
116
101
it ( 'returns blob output if successful' , ( ) => {
117
102
window . fetch . mockReturnValue ( Promise . resolve ( { ok : true , blob : ( ) => blob } ) ) ;
118
103
expect ( download . downloadFile ( file ) ) . resolves . toEqual ( blob ) ;
119
- expect ( download . getTimeStampUrl ) . toBeCalledWith ( file . downloadUrl ) ;
120
104
} ) ;
121
105
it ( 'throw if not successful' , ( ) => {
122
106
const failFetchStatusText = 'failed to fetch' ;
123
107
window . fetch . mockReturnValue ( Promise . resolve ( { ok : false , statusText : failFetchStatusText } ) ) ;
124
108
expect ( ( ) => download . downloadFile ( file ) ) . rejects . toThrow ( failFetchStatusText ) ;
125
- expect ( download . getTimeStampUrl ) . toBeCalledWith ( file . downloadUrl ) ;
126
109
} ) ;
127
110
} ) ;
128
111
@@ -135,10 +118,10 @@ describe('download thunkActions', () => {
135
118
afterEach ( ( ) => { download . downloadFile = downloadFile ; } ) ;
136
119
137
120
it ( 'returns a mapping of all files to download action' , async ( ) => {
138
- const downloadedBlobs = await download . downloadBlobs ( files ) ;
121
+ const downloadBlobs = await download . downloadBlobs ( files ) ;
139
122
expect ( download . downloadFile ) . toHaveBeenCalledTimes ( files . length ) ;
140
- expect ( downloadedBlobs . length ) . toEqual ( files . length ) ;
141
- expect ( downloadedBlobs ) . toEqual ( files . map ( file => file . name ) ) ;
123
+ expect ( downloadBlobs . blobs . length ) . toEqual ( files . length ) ;
124
+ expect ( downloadBlobs . blobs ) . toEqual ( files . map ( file => file . name ) ) ;
142
125
} ) ;
143
126
144
127
it ( 'returns a mapping of errors from download action' , ( ) => {
@@ -148,25 +131,51 @@ describe('download thunkActions', () => {
148
131
} ) ;
149
132
} ) ;
150
133
134
+ describe ( 'getSubmissionFiles' , ( ) => {
135
+ let fetchSubmissionFiles ;
136
+ beforeEach ( ( ) => {
137
+ fetchSubmissionFiles = api . fetchSubmissionFiles ;
138
+ api . fetchSubmissionFiles = jest . fn ( ) ;
139
+ } ) ;
140
+ afterEach ( ( ) => { api . fetchSubmissionFiles = fetchSubmissionFiles ; } ) ;
141
+ it ( 'return api.fetchSubmissionFiles on success' , async ( ) => {
142
+ api . fetchSubmissionFiles = ( ) => Promise . resolve ( { files } ) ;
143
+ const data = await download . getSubmissionFiles ( ) ;
144
+ expect ( data ) . toEqual ( files ) ;
145
+ } ) ;
146
+
147
+ it ( 'throw FetchSubmissionFilesException on fetch failure' , ( ) => {
148
+ api . fetchSubmissionFiles = ( ) => Promise . reject ( ) ;
149
+ expect ( ( ) => download . getSubmissionFiles ( ) ) . rejects . toEqual ( download . FetchSubmissionFilesException ( ) ) ;
150
+ } ) ;
151
+ } ) ;
152
+
151
153
describe ( 'downloadFiles' , ( ) => {
152
154
let downloadBlobs ;
155
+ let getSubmissionFiles ;
153
156
beforeEach ( ( ) => {
154
157
dispatch = jest . fn ( ) ;
155
- selectors . grading . selected . response = ( ) => ( { files } ) ;
158
+ selectors . grading . selected . submissionUUID = ( ) => submissionUUID ;
156
159
selectors . grading . selected . username = ( ) => username ;
157
160
download . zipFiles = jest . fn ( ) ;
158
161
159
162
downloadBlobs = download . downloadBlobs ;
160
- download . downloadBlobs = ( ) => Promise . resolve ( blobs ) ;
163
+ download . downloadBlobs = ( ) => Promise . resolve ( { blobs, files } ) ;
164
+
165
+ getSubmissionFiles = download . getSubmissionFiles ;
166
+ download . getSubmissionFiles = ( ) => Promise . resolve ( files ) ;
167
+ } ) ;
168
+ afterEach ( ( ) => {
169
+ download . downloadBlobs = downloadBlobs ;
170
+ download . getSubmissionFiles = getSubmissionFiles ;
161
171
} ) ;
162
- afterEach ( ( ) => { download . downloadBlobs = downloadBlobs ; } ) ;
163
172
it ( 'dispatches network request with downloadFiles key' , ( ) => {
164
173
download . downloadFiles ( ) ( dispatch , getState ) ;
165
174
const { networkRequest } = dispatch . mock . calls [ 0 ] [ 0 ] ;
166
175
expect ( networkRequest . requestKey ) . toEqual ( RequestKeys . downloadFiles ) ;
167
176
} ) ;
168
177
it ( 'dispatches network request for downloadFiles, zipping output of downloadBlobs' , async ( ) => {
169
- download . downloadBlobs = ( ) => Promise . resolve ( blobs ) ;
178
+ download . downloadBlobs = ( ) => Promise . resolve ( { blobs, files } ) ;
170
179
download . downloadFiles ( ) ( dispatch , getState ) ;
171
180
const { networkRequest } = dispatch . mock . calls [ 0 ] [ 0 ] ;
172
181
await networkRequest . promise ;
0 commit comments