1
- import FileUploader from "./FileUploader.vue" ;
1
+ import FileUploader , {
2
+ CommonImageProcessingError ,
3
+ CommonImage ,
4
+ CommonImageScaleFactorsImpl ,
5
+ CommonImageError
6
+ } from "./FileUploader.vue" ;
2
7
import { mount } from "@vue/test-utils" ;
3
8
import { render , fireEvent } from "@testing-library/vue" ;
4
9
import { jest } from '@jest/globals' ;
5
10
const fs = require ( 'fs' ) ;
11
+ const sha1 = require ( 'sha1' ) ;
12
+ import sampleImage from './test-files/sample-id.jpg' ;
13
+
14
+ function mockDropData ( files ) {
15
+ return {
16
+ dataTransfer : {
17
+ files,
18
+ items : files . map ( ( file ) => ( {
19
+ kind : "file" ,
20
+ type : file . type ,
21
+ getAsFile : ( ) => file ,
22
+ } ) ) ,
23
+ types : [ "Files" ] ,
24
+ } ,
25
+ } ;
26
+ }
27
+
28
+ const b64toBlob = ( b64Data , contentType = '' , sliceSize = 512 ) => {
29
+ const byteCharacters = atob ( b64Data ) ;
30
+ const byteArrays = [ ] ;
31
+
32
+ for ( let offset = 0 ; offset < byteCharacters . length ; offset += sliceSize ) {
33
+ const slice = byteCharacters . slice ( offset , offset + sliceSize ) ;
34
+
35
+ const byteNumbers = new Array ( slice . length ) ;
36
+ for ( let i = 0 ; i < slice . length ; i ++ ) {
37
+ byteNumbers [ i ] = slice . charCodeAt ( i ) ;
38
+ }
39
+
40
+ const byteArray = new Uint8Array ( byteNumbers ) ;
41
+ byteArrays . push ( byteArray ) ;
42
+ }
43
+
44
+ const blob = new Blob ( byteArrays , { type : contentType } ) ;
45
+ return blob ;
46
+ }
47
+
48
+ describe ( "CommonImageProcessingError" , ( ) => {
49
+ test ( "creates a new instance" , ( ) => {
50
+ const instance = new CommonImageProcessingError ( "error" ) ;
51
+ expect ( instance ) . toBeDefined ( ) ;
52
+ } ) ;
53
+ } ) ;
54
+
55
+ describe ( "CommonImage" , ( ) => {
56
+ test ( "creates a new instance" , ( ) => {
57
+ const instance = new CommonImage ( "file content" ) ;
58
+ expect ( instance ) . toBeDefined ( ) ;
59
+ } ) ;
60
+
61
+ test ( "toJSON()" , ( ) => {
62
+ const instance = new CommonImage ( "file content" ) ;
63
+ expect ( instance . toJSON ( ) ) . toBeDefined ( ) ;
64
+ } ) ;
65
+ } ) ;
6
66
7
67
describe ( "FileUploader component" , ( ) => {
8
68
test ( "matches the success snapshot" , ( ) => {
@@ -19,19 +79,20 @@ describe("FileUploader component", () => {
19
79
fireEvent . dragOver ( container . querySelector ( ".dropzone" ) ) ;
20
80
} ) ;
21
81
22
- test ( "drop event" , ( ) => {
82
+ test ( "drop event" , async ( ) => {
23
83
const { container } = render ( FileUploader , {
24
84
propsData : {
25
85
value : [ ] ,
26
86
}
27
87
} ) ;
28
- const dragEventInit = {
29
- dataTransfer : { }
30
- } ;
31
- fireEvent . drop ( container . querySelector ( ".dropzone" ) , dragEventInit ) ;
88
+ const file = new File ( [ JSON . stringify ( { test : true } ) ] , "test.json" , {
89
+ type : "application/json"
90
+ } ) ;
91
+ const data = mockDropData ( [ file ] ) ;
92
+ fireEvent . drop ( container . querySelector ( ".dropzone" ) , data ) ;
32
93
} ) ;
33
94
34
- test ( "change event" , ( ) => {
95
+ test ( "change event" , ( done ) => {
35
96
const { container } = render ( FileUploader , {
36
97
propsData : {
37
98
value : [ ] ,
@@ -40,14 +101,37 @@ describe("FileUploader component", () => {
40
101
} ) ;
41
102
const blob = new Blob ( fs . readFileSync ( 'src/components/file-uploader/test-files/sample-id.jpg' ) ) ;
42
103
const file = new File ( [ blob ] , 'sample-id.jpg' ) ;
104
+ console . log ( "file is File" , sampleImage ) ;
105
+ // const file = new File([JSON.stringify({ ping: true })], "ping.json", {
106
+ // type: "application/json",
107
+ // });
43
108
const changeEventInit = {
44
109
target : {
45
110
files : [ file ]
46
111
}
47
112
} ;
48
113
fireEvent . change ( container . querySelector ( "#test" ) , changeEventInit ) ;
114
+ setTimeout ( ( ) => {
115
+ done ( ) ;
116
+ } , 4000 ) ;
49
117
} ) ;
50
118
119
+ test ( "change event with empty `files` array" , ( ) => {
120
+ const { container } = render ( FileUploader , {
121
+ propsData : {
122
+ value : [ ] ,
123
+ id : 'test'
124
+ }
125
+ } ) ;
126
+ const changeEventInit = {
127
+ target : {
128
+ files : [ ]
129
+ }
130
+ } ;
131
+ fireEvent . change ( container . querySelector ( "#test" ) , changeEventInit ) ;
132
+ } ) ;
133
+
134
+
51
135
test ( "Click `add` button." , ( ) => {
52
136
const { container } = render ( FileUploader , {
53
137
propsData : {
@@ -57,4 +141,170 @@ describe("FileUploader component", () => {
57
141
} ) ;
58
142
fireEvent . click ( container . querySelector ( "a.common-thumbnail" ) ) ;
59
143
} ) ;
144
+
145
+ test ( "checkImageExists(): false" , ( ) => {
146
+ const wrapper = mount ( FileUploader , { } ) ;
147
+ const image = new CommonImage ( "file content" )
148
+ const imageExists = wrapper . vm . checkImageExists ( image , [ ] ) ;
149
+ expect ( imageExists ) . toBeFalsy ( ) ;
150
+ } ) ;
151
+
152
+ test ( "checkImageExists(): true" , ( ) => {
153
+ const wrapper = mount ( FileUploader , { } ) ;
154
+ const fileContent = "file content" ;
155
+ const image = new CommonImage ( fileContent )
156
+ image . id = sha1 ( fileContent ) ;
157
+
158
+ const imageExists = wrapper . vm . checkImageExists ( image , [ image ] ) ;
159
+ expect ( imageExists ) . toBeTruthy ( ) ;
160
+ } ) ;
161
+
162
+ test ( "resizeImage()" , ( ) => {
163
+ jest . mock ( 'blueimp-load-image' , ( ) => ( image , onload ) => {
164
+ onload ( ) ;
165
+ } ) ;
166
+ const wrapper = mount ( FileUploader , { } ) ;
167
+ const fileContent = "file content" ;
168
+ // const image = new CommonImage(fileContent)
169
+ const image = document . createElement ( "img" ) ;
170
+ image . src = sampleImage ;
171
+ const observer = {
172
+ next : jest . fn ( )
173
+ }
174
+ wrapper . vm . resizeImage (
175
+ image ,
176
+ wrapper . vm ,
177
+ new CommonImageScaleFactorsImpl ( 1 , 1 ) ,
178
+ observer ,
179
+ 0 ,
180
+ true
181
+ ) ;
182
+ } ) ;
183
+
184
+ test ( "retryStrategy()" , ( ) => {
185
+ const wrapper = mount ( FileUploader , { } ) ;
186
+ const error = {
187
+ errorCode : CommonImageError . TooBig ,
188
+ pipe : jest . fn ( )
189
+ }
190
+ const callback = wrapper . vm . retryStrategy ( 1 ) ;
191
+ callback ( error ) ;
192
+ } ) ;
193
+
194
+ test ( "readPDF()" , ( done ) => {
195
+ const wrapper = mount ( FileUploader , { } ) ;
196
+ const pdfContents = fs . readFileSync ( 'src/components/file-uploader/test-files/sample.pdf' , { encoding : 'base64' } ) ;
197
+ const blob = b64toBlob ( pdfContents , 'application/pdf' ) ;
198
+ const pdfFile = new File ( [ blob ] , 'sample.pdf' ) ;
199
+ wrapper . vm . readPDF (
200
+ pdfFile ,
201
+ new CommonImageScaleFactorsImpl ( 1 , 1 ) ,
202
+ ( ) => {
203
+ done ( ) ;
204
+ } ,
205
+ ( ) => {
206
+ done ( ) ;
207
+ }
208
+ ) ;
209
+ } ) ;
210
+
211
+ test ( "makeGrayScale()" , ( ) => {
212
+ const wrapper = mount ( FileUploader , { } ) ;
213
+ const canvas = document . createElement ( 'canvas' ) ;
214
+ canvas . width = 10 ;
215
+ canvas . height = 10 ;
216
+ wrapper . vm . makeGrayScale ( canvas ) ;
217
+ } ) ;
218
+
219
+ test ( "handleImageFile()" , ( ) => {
220
+ const wrapper = mount ( FileUploader , { } ) ;
221
+ const image = new CommonImage ( "content" ) ;
222
+ wrapper . vm . handleImageFile ( image ) ;
223
+ } ) ;
224
+
225
+ test ( "filterError() TooBig" , ( ) => {
226
+ const wrapper = mount ( FileUploader , { } ) ;
227
+ const error = {
228
+ errorCode : CommonImageError . TooBig ,
229
+ image : new CommonImage ( "content" )
230
+ } ;
231
+ wrapper . vm . filterError ( error ) ;
232
+ } ) ;
233
+
234
+ test ( "filterError() CannotOpen" , ( ) => {
235
+ const wrapper = mount ( FileUploader , { } ) ;
236
+ const error = {
237
+ errorCode : CommonImageError . CannotOpen ,
238
+ rawImageFile : {
239
+ name : 'name.jpg'
240
+ }
241
+ } ;
242
+ wrapper . vm . filterError ( error ) ;
243
+ } ) ;
244
+
245
+ test ( "filterError() CannotOpenPDF" , ( ) => {
246
+ const wrapper = mount ( FileUploader , { } ) ;
247
+ const error = {
248
+ errorCode : CommonImageError . CannotOpenPDF ,
249
+ image : new CommonImage ( "content" )
250
+ } ;
251
+ wrapper . vm . filterError ( error ) ;
252
+ } ) ;
253
+
254
+ test ( "filterError() other error" , ( ) => {
255
+ const wrapper = mount ( FileUploader , { } ) ;
256
+ const error = {
257
+ errorCode : CommonImageError . AlreadyExists ,
258
+ image : new CommonImage ( "content" )
259
+ } ;
260
+ expect ( ( ) => {
261
+ wrapper . vm . filterError ( error ) ;
262
+ } ) . toThrow ( ) ;
263
+ } ) ;
264
+
265
+ test ( "handleError() handle empty image model" , ( ) => {
266
+ const wrapper = mount ( FileUploader , { } ) ;
267
+ wrapper . vm . handleError ( null , null , null ) ;
268
+ } ) ;
269
+
270
+ test ( "getErrorMessage()" , ( ) => {
271
+ const wrapper = mount ( FileUploader , { } ) ;
272
+ expect ( wrapper . vm . getErrorMessage ( CommonImageError . WrongType ) ) . toBe ( 'Wrong file type.' ) ;
273
+ expect ( wrapper . vm . getErrorMessage ( CommonImageError . TooSmall ) ) . toBe ( 'File too small.' ) ;
274
+ expect ( wrapper . vm . getErrorMessage ( CommonImageError . TooBig ) ) . toBe ( 'File too large.' ) ;
275
+ expect ( wrapper . vm . getErrorMessage ( CommonImageError . AlreadyExists ) ) . toBe ( 'File already exists.' ) ;
276
+ expect ( wrapper . vm . getErrorMessage ( CommonImageError . Unknown ) ) . toBe ( 'Unknown error.' ) ;
277
+ expect ( wrapper . vm . getErrorMessage ( CommonImageError . CannotOpen ) ) . toBe ( 'Cannot open file.' ) ;
278
+ expect ( wrapper . vm . getErrorMessage ( CommonImageError . PDFnotSupported ) ) . toBe ( 'This PDF file is not supported.' ) ;
279
+ expect ( wrapper . vm . getErrorMessage ( CommonImageError . CannotOpenPDF ) ) . toBe ( 'Cannot open PDF file.' ) ;
280
+ expect ( wrapper . vm . getErrorMessage ( null ) ) . toBe ( 'An error has occurred.' ) ;
281
+ } ) ;
282
+
283
+ test ( "deleteImage()" , ( ) => {
284
+ const wrapper = mount ( FileUploader , { } ) ;
285
+ const image = new CommonImage ( "content" ) ;
286
+ wrapper . vm . $emit ( 'input' , [ image ] ) ;
287
+ wrapper . vm . deleteImage ( image ) ;
288
+ expect ( wrapper . vm . images . length ) . toBe ( 0 ) ;
289
+ } ) ;
290
+
291
+ test ( "checkImageDimensions()" , ( ) => {
292
+ const wrapper = mount ( FileUploader , { } ) ;
293
+ expect ( wrapper . vm . checkImageDimensions ( {
294
+ naturalWidth : 10 ,
295
+ naturalHeight : 10
296
+ } ) ) . toBeTruthy ( ) ;
297
+
298
+ expect ( wrapper . vm . checkImageDimensions ( {
299
+ naturalWidth : - 10 ,
300
+ naturalHeight : 10
301
+ } ) ) . toBeFalsy ( ) ;
302
+
303
+ expect ( wrapper . vm . checkImageDimensions ( {
304
+ naturalWidth : 10 ,
305
+ naturalHeight : - 10
306
+ } ) ) . toBeFalsy ( ) ;
307
+ } ) ;
308
+
309
+
60
310
} ) ;
0 commit comments