1- import ' global-jsdom/register'
2- import assert from ' node:assert'
3- import { afterEach , describe , mock , test } from ' node:test'
4- import * as s3file from ' ../../s3file/static/s3file/js/s3file.js'
1+ import " global-jsdom/register"
2+ import assert from " node:assert"
3+ import { afterEach , describe , mock , test } from " node:test"
4+ import * as s3file from " ../../s3file/static/s3file/js/s3file.js"
55
66afterEach ( ( ) => {
77 mock . restoreAll ( )
88} )
99
10- describe ( 'getKeyFromResponse' , ( ) => {
11- test ( 'returns key' , ( ) => {
12- const responseText =
13- `<?xml version="1.0" encoding="UTF-8"?>
10+ describe ( "getKeyFromResponse" , ( ) => {
11+ test ( "returns key" , ( ) => {
12+ const responseText = `<?xml version="1.0" encoding="UTF-8"?>
1413 <PostResponse>
1514 <Location>https://example-bucket.s3.amazonaws.com/tmp%2Fs2file%2Fsome-file.jpeg</Location>
1615 <Bucket>example-bucket</Bucket>
1716 <Key>tmp/s2file/some%20file.jpeg</Key>
1817 <ETag>"a38155039ec348f97dfd63da4cb2619d"</ETag>
1918 </PostResponse>`
20- assert . strictEqual ( s3file . getKeyFromResponse ( responseText ) , 'tmp/s2file/some file.jpeg' )
19+ assert . strictEqual (
20+ s3file . getKeyFromResponse ( responseText ) ,
21+ "tmp/s2file/some file.jpeg" ,
22+ )
2123 } )
2224} )
2325
24- describe ( ' S3FileInput' , ( ) => {
25- test ( ' constructor' , ( ) => {
26+ describe ( " S3FileInput" , ( ) => {
27+ test ( " constructor" , ( ) => {
2628 const input = new s3file . S3FileInput ( )
27- assert . strictEqual ( input . type , ' file' )
29+ assert . strictEqual ( input . type , " file" )
2830 assert . deepStrictEqual ( input . keys , [ ] )
2931 assert . strictEqual ( input . upload , null )
3032 } )
3133
32- test ( ' connectedCallback' , ( ) => {
33- const form = document . createElement ( ' form' )
34+ test ( " connectedCallback" , ( ) => {
35+ const form = document . createElement ( " form" )
3436 document . body . appendChild ( form )
3537 const input = new s3file . S3FileInput ( )
3638 input . addEventListener = mock . fn ( input . addEventListener )
@@ -40,41 +42,41 @@ describe('S3FileInput', () => {
4042 assert ( input . addEventListener . mock . calls . length === 1 )
4143 } )
4244
43- test ( ' changeHandler' , ( ) => {
44- const form = document . createElement ( ' form' )
45+ test ( " changeHandler" , ( ) => {
46+ const form = document . createElement ( " form" )
4547 const input = new s3file . S3FileInput ( )
46- input . keys = [ ' key' ]
47- input . upload = ' upload'
48+ input . keys = [ " key" ]
49+ input . upload = " upload"
4850 form . appendChild ( input )
4951 input . changeHandler ( )
5052 assert ( ! input . keys . length )
5153 assert ( ! input . upload )
5254 } )
5355
54- test ( ' submitHandler' , async ( ) => {
55- const form = document . createElement ( ' form' )
56+ test ( " submitHandler" , async ( ) => {
57+ const form = document . createElement ( " form" )
5658 document . body . appendChild ( form )
5759 form . pendingRquests = [ ]
5860 form . requestSubmit = mock . fn ( form . requestSubmit )
5961 form . dispatchEvent = mock . fn ( form . dispatchEvent )
60- const submitButton = document . createElement ( ' button' )
62+ const submitButton = document . createElement ( " button" )
6163 form . appendChild ( submitButton )
62- submitButton . setAttribute ( ' type' , ' submit' )
63- const event = new window . SubmitEvent ( ' submit' , { submitter : submitButton } )
64+ submitButton . setAttribute ( " type" , " submit" )
65+ const event = new window . SubmitEvent ( " submit" , { submitter : submitButton } )
6466 const input = new s3file . S3FileInput ( )
6567 form . appendChild ( input )
6668 await input . submitHandler ( event )
6769 assert ( form . dispatchEvent . mock . calls . length === 2 )
6870 assert ( form . requestSubmit . mock . calls . length === 2 )
6971 } )
7072
71- test ( ' uploadHandler' , ( ) => {
72- const form = document . createElement ( ' form' )
73+ test ( " uploadHandler" , ( ) => {
74+ const form = document . createElement ( " form" )
7375 document . body . appendChild ( form )
7476 const input = new s3file . S3FileInput ( )
7577 form . appendChild ( input )
76- Object . defineProperty ( input , ' files' , {
77- get : ( ) => [ new globalThis . File ( [ '' ] , ' file.txt' ) ]
78+ Object . defineProperty ( input , " files" , {
79+ get : ( ) => [ new globalThis . File ( [ "" ] , " file.txt" ) ] ,
7880 } )
7981 assert ( ! input . upload )
8082 assert . strictEqual ( input . files . length , 1 )
@@ -84,31 +86,30 @@ describe('S3FileInput', () => {
8486 assert ( form . pendingRquests )
8587 } )
8688
87- test ( ' fromDataHandler' , ( ) => {
88- const event = new globalThis . CustomEvent ( ' formdata' , { formData : new FormData ( ) } )
89- const form = document . createElement ( ' form' )
89+ test ( " fromDataHandler" , ( ) => {
90+ const event = new globalThis . CustomEvent ( " formdata" , { formData : new FormData ( ) } )
91+ const form = document . createElement ( " form" )
9092 document . body . appendChild ( form )
9193 const input = new s3file . S3FileInput ( )
9294 form . appendChild ( input )
93- input . name = ' file'
94- input . keys = [ ' key1' , ' key2' ]
95+ input . name = " file"
96+ input . keys = [ " key1" , " key2" ]
9597 event . formData = new FormData ( )
9698 input . fromDataHandler ( event )
97- assert . deepStrictEqual ( event . formData . getAll ( ' file' ) , [ ' key1' , ' key2' ] )
98- assert . strictEqual ( event . formData . get ( ' s3file' ) , ' file' )
99+ assert . deepStrictEqual ( event . formData . getAll ( " file" ) , [ " key1" , " key2" ] )
100+ assert . strictEqual ( event . formData . get ( " s3file" ) , " file" )
99101 } )
100102
101- test ( ' uploadFiles' , async ( ) => {
102- const form = document . createElement ( ' form' )
103+ test ( " uploadFiles" , async ( ) => {
104+ const form = document . createElement ( " form" )
103105 document . body . appendChild ( form )
104106 const input = new s3file . S3FileInput ( )
105- input . setAttribute ( ' data-fields-policy' , ' policy' )
107+ input . setAttribute ( " data-fields-policy" , " policy" )
106108 form . appendChild ( input )
107- Object . defineProperty ( input , ' files' , {
108- get : ( ) => [ new globalThis . File ( [ '' ] , ' file.txt' ) ]
109+ Object . defineProperty ( input , " files" , {
110+ get : ( ) => [ new globalThis . File ( [ "" ] , " file.txt" ) ] ,
109111 } )
110- const responseText =
111- `<?xml version="1.0" encoding="UTF-8"?>
112+ const responseText = `<?xml version="1.0" encoding="UTF-8"?>
112113 <PostResponse>
113114 <Location>https://example-bucket.s3.amazonaws.com/tmp%2Fs2file%2Fsome-file.jpeg</Location>
114115 <Bucket>example-bucket</Bucket>
@@ -120,39 +121,41 @@ describe('S3FileInput', () => {
120121 assert ( input . files . length === 1 )
121122 await input . uploadFiles ( )
122123 assert ( globalThis . fetch . mock . calls . length === 1 )
123- assert . deepStrictEqual ( input . keys , [ ' tmp/s2file/some file.jpeg' ] )
124+ assert . deepStrictEqual ( input . keys , [ " tmp/s2file/some file.jpeg" ] )
124125 } )
125126
126- test ( ' uploadFiles with HTTP error' , async ( ) => {
127- const form = document . createElement ( ' form' )
127+ test ( " uploadFiles with HTTP error" , async ( ) => {
128+ const form = document . createElement ( " form" )
128129 document . body . appendChild ( form )
129130 const input = new s3file . S3FileInput ( )
130131 form . appendChild ( input )
131- Object . defineProperty ( input , ' files' , {
132- get : ( ) => [ new globalThis . File ( [ '' ] , ' file.txt' ) ]
132+ Object . defineProperty ( input , " files" , {
133+ get : ( ) => [ new globalThis . File ( [ "" ] , " file.txt" ) ] ,
133134 } )
134- const response = { status : 400 , statusText : ' Bad Request' }
135+ const response = { status : 400 , statusText : " Bad Request" }
135136 globalThis . fetch = mock . fn ( async ( ) => response )
136137 assert ( input . files . length === 1 )
137138 await input . uploadFiles ( )
138139 assert ( globalThis . fetch . mock . calls . length === 1 )
139140 assert . deepStrictEqual ( input . keys , [ ] )
140- assert . strictEqual ( input . validationMessage , ' Bad Request' )
141+ assert . strictEqual ( input . validationMessage , " Bad Request" )
141142 } )
142143
143- test ( ' uploadFiles with network error' , async ( ) => {
144- const form = document . createElement ( ' form' )
144+ test ( " uploadFiles with network error" , async ( ) => {
145+ const form = document . createElement ( " form" )
145146 document . body . appendChild ( form )
146147 const input = new s3file . S3FileInput ( )
147148 form . appendChild ( input )
148- Object . defineProperty ( input , 'files' , {
149- get : ( ) => [ new globalThis . File ( [ '' ] , 'file.txt' ) ]
149+ Object . defineProperty ( input , "files" , {
150+ get : ( ) => [ new globalThis . File ( [ "" ] , "file.txt" ) ] ,
151+ } )
152+ globalThis . fetch = mock . fn ( async ( ) => {
153+ throw new Error ( "Network Error" )
150154 } )
151- globalThis . fetch = mock . fn ( async ( ) => { throw new Error ( 'Network Error' ) } )
152155 assert ( input . files . length === 1 )
153156 await input . uploadFiles ( )
154157 assert ( globalThis . fetch . mock . calls . length === 1 )
155158 assert . deepStrictEqual ( input . keys , [ ] )
156- assert . strictEqual ( input . validationMessage , ' Error: Network Error' )
159+ assert . strictEqual ( input . validationMessage , " Error: Network Error" )
157160 } )
158161} )
0 commit comments