1
1
import { waitFor , renderHook , act } from '@testing-library/react' ;
2
2
import nock from 'nock' ;
3
- import { spy } from 'sinon' ;
3
+ import sinon from 'sinon' ;
4
4
import { createTestQueryClient } from '@support/queryClient' ;
5
5
import {
6
6
getFileStorageFiles ,
7
7
getFileStorageFilesError ,
8
8
deletePublicItem ,
9
9
deletePublicItemError ,
10
- uploadPublicFile ,
11
- uploadPublicFileError ,
12
10
createPublicDirectory ,
13
11
createPublicDirectoryError ,
14
12
} from '@support/nocks/fileStorage' ;
15
13
import useFileStorage from './useFileStorage' ;
16
14
import { getFileStorageData } from '../../test/frontend/support/data/fileStorageData' ;
15
+ import federalist from '@util/federalistApi' ;
17
16
18
17
const createWrapper = createTestQueryClient ( ) ;
19
18
@@ -26,6 +25,7 @@ const props = {
26
25
} ;
27
26
describe ( 'useFileStorage' , ( ) => {
28
27
beforeEach ( ( ) => nock . cleanAll ( ) ) ;
28
+ afterEach ( ( ) => sinon . restore ( ) ) ;
29
29
afterAll ( ( ) => nock . restore ( ) ) ;
30
30
31
31
it ( 'should fetch public files successfully' , async ( ) => {
@@ -119,7 +119,7 @@ describe('useFileStorage', () => {
119
119
wrapper : createWrapper ( ) ,
120
120
} ) ;
121
121
122
- const qcSpy = spy ( result . current . queryClient , 'invalidateQueries' ) ;
122
+ const qcSpy = sinon . spy ( result . current . queryClient , 'invalidateQueries' ) ;
123
123
await act ( async ( ) => {
124
124
await result . current . deleteItem ( fileItem ) ;
125
125
} ) ;
@@ -141,7 +141,7 @@ describe('useFileStorage', () => {
141
141
wrapper : createWrapper ( ) ,
142
142
} ) ;
143
143
144
- const qcSpy = spy ( result . current . queryClient , 'invalidateQueries' ) ;
144
+ const qcSpy = sinon . spy ( result . current . queryClient , 'invalidateQueries' ) ;
145
145
146
146
await act ( async ( ) => {
147
147
await result . current . deleteItem ( fileItem ) . catch ( ( ) => { } ) ;
@@ -155,100 +155,42 @@ describe('useFileStorage', () => {
155
155
it ( 'should upload a file successfully and invalidate the query' , async ( ) => {
156
156
const parent = '/' ;
157
157
const mockFile = new File ( [ 'file content' ] , 'test-file.txt' , { type : 'text/plain' } ) ;
158
+ const stub = sinon . stub ( federalist , 'uploadPublicFile' ) ;
158
159
159
- await uploadPublicFile ( props . fileStorageId , parent , mockFile ) ;
160
160
await getFileStorageFiles ( { ...props , path : parent } , { times : 3 } ) ;
161
161
162
+ stub . resolves ( ) ;
163
+
162
164
const { result } = renderHook ( ( ) => useFileStorage ( props . fileStorageId ) , {
163
165
wrapper : createWrapper ( ) ,
164
166
} ) ;
165
167
166
- const qcSpy = spy ( result . current . queryClient , 'invalidateQueries' ) ;
167
-
168
- await act ( async ( ) => {
169
- await result . current . uploadFile ( parent , mockFile ) ;
170
- } ) ;
171
-
172
- await waitFor ( ( ) =>
173
- expect ( result . current . uploadSuccess ) . toContain ( 'File uploaded successfully' ) ,
174
- ) ;
175
- expect ( qcSpy . calledOnce ) . toBe ( true ) ;
176
- } ) ;
177
-
178
- it ( 'should successfully upload a file even inside a nested folder' , async ( ) => {
179
- const parent = '/nested/folder' ;
180
- const mockFile = new File ( [ 'file content' ] , 'nested-file.txt' , {
181
- type : 'text/plain' ,
182
- } ) ;
183
-
184
- await uploadPublicFile ( props . fileStorageId , parent , mockFile ) ;
185
- await getFileStorageFiles ( { ...props , path : parent } , { times : 3 } ) ;
186
-
187
- const { result } = renderHook (
188
- ( ) =>
189
- // don't spread from Object.values() if you're going to overwrite something
190
- useFileStorage (
191
- props . fileStorageId ,
192
- parent ,
193
- props . sortKey ,
194
- props . sortOrder ,
195
- props . page ,
196
- ) ,
197
- {
198
- wrapper : createWrapper ( ) ,
199
- } ,
200
- ) ;
201
-
202
- const qcSpy = spy ( result . current . queryClient , 'invalidateQueries' ) ;
168
+ const qcSpy = sinon . spy ( result . current . queryClient , 'invalidateQueries' ) ;
203
169
204
- await act ( async ( ) => {
205
- await result . current . uploadFile ( parent , mockFile ) ;
206
- } ) ;
170
+ await waitFor ( async ( ) => result . current . uploadFile ( parent , mockFile ) ) ;
207
171
208
- await waitFor ( ( ) =>
209
- expect ( result . current . uploadSuccess ) . toContain ( 'File uploaded successfully' ) ,
210
- ) ;
211
172
expect ( qcSpy . calledOnce ) . toBe ( true ) ;
212
173
} ) ;
213
174
214
- it ( 'should return an error message when file upload fails' , async ( ) => {
175
+ it ( 'should throw error when upload file fails' , async ( ) => {
215
176
const parent = '/' ;
216
177
const mockFile = new File ( [ 'file content' ] , 'test-file.txt' , { type : 'text/plain' } ) ;
178
+ const stub = sinon . stub ( federalist , 'uploadPublicFile' ) ;
179
+ stub . rejects ( ) ;
217
180
218
- const errorMessage = 'Upload failed.' ;
219
- await uploadPublicFileError ( props . fileStorageId , parent , mockFile , errorMessage ) ;
220
181
await getFileStorageFiles ( { ...props , path : parent } , { times : 3 } ) ;
221
182
222
183
const { result } = renderHook ( ( ) => useFileStorage ( props . fileStorageId ) , {
223
184
wrapper : createWrapper ( ) ,
224
185
} ) ;
225
186
226
- await act ( async ( ) => {
227
- await result . current . uploadFile ( parent , mockFile ) . catch ( ( ) => { } ) ;
228
- } ) ;
229
-
230
- await waitFor ( ( ) => expect ( result . current . uploadError ) . toBe ( errorMessage ) ) ;
231
- } ) ;
187
+ const qcSpy = sinon . spy ( result . current . queryClient , 'invalidateQueries' ) ;
232
188
233
- it ( 'should return a specific error when uploading a duplicate file' , async ( ) => {
234
- const parent = '/' ;
235
- const mockFile = new File ( [ 'file content' ] , 'existing-file.txt' , {
236
- type : 'text/plain' ,
237
- } ) ;
238
- const errorMessage = `A file named "${ mockFile . name } " already exists in this folder.` ;
239
-
240
- await uploadPublicFileError ( props . fileStorageId , parent , mockFile , errorMessage ) ;
241
- await getFileStorageFiles ( { ...props , path : parent } , { times : 3 } ) ;
242
-
243
- const { result } = renderHook ( ( ) => useFileStorage ( props . fileStorageId ) , {
244
- wrapper : createWrapper ( ) ,
245
- } ) ;
246
-
247
- await act ( async ( ) => {
248
- await result . current . uploadFile ( parent , mockFile ) . catch ( ( ) => { } ) ;
249
- } ) ;
189
+ await waitFor ( async ( ) =>
190
+ result . current . uploadFile ( parent , mockFile ) . catch ( ( e ) => e ) ,
191
+ ) ;
250
192
251
- await waitFor ( ( ) => expect ( result . current . uploadError ) . toBe ( errorMessage ) ) ;
193
+ expect ( qcSpy . calledOnce ) . toBe ( false ) ;
252
194
} ) ;
253
195
254
196
it ( 'should create a folder successfully and invalidate the query' , async ( ) => {
@@ -262,7 +204,7 @@ describe('useFileStorage', () => {
262
204
wrapper : createWrapper ( ) ,
263
205
} ) ;
264
206
265
- const qcSpy = spy ( result . current . queryClient , 'invalidateQueries' ) ;
207
+ const qcSpy = sinon . spy ( result . current . queryClient , 'invalidateQueries' ) ;
266
208
267
209
await act ( async ( ) => {
268
210
await result . current . createFolder ( parent , folderName ) ;
0 commit comments