1- const Readable = require ( 'stream' ) . Readable ;
2- const azure = require ( '../lib' ) ;
1+ /* eslint-disable @typescript-eslint/no-explicit-any */
2+ import stream from 'stream' ;
3+ import azure from '../src' ;
34
45//Mock Date functions
56const DATE_TO_USE = new Date ( '2017' ) ;
67const _Date = Date ;
7- global . Date = jest . fn ( ( ) => DATE_TO_USE ) ;
8+ global . Date = jest . fn ( ( ) => DATE_TO_USE ) as any ;
89global . Date . UTC = _Date . UTC ;
910global . Date . parse = _Date . parse ;
1011global . Date . now = _Date . now ;
1112
13+ const validOptions = {
14+ publicContainerName : 'pubcon' ,
15+ privateContainerName : 'privcon' ,
16+ accountName : 'name' ,
17+ accountKey : 'key' ,
18+ path : '/'
19+ } ;
20+
1221test ( 'should expose the correct methods' , ( ) => {
13- const options = {
14- publicContainerName : 'pubcon' ,
15- privateContainerName : 'privcon'
16- } ;
17- const client = new azure ( options ) ;
22+ const client = azure ( validOptions ) ;
1823
1924 [
2025 { method : 'adapterType' , value : 'azure-blob-storage' } ,
@@ -28,46 +33,44 @@ test('should expose the correct methods', () => {
2833 { method : 'putFileContent' , type : Function }
2934 ] . forEach ( api => {
3035 if ( api . type === Function ) {
31- expect ( client [ api . method ] ) . toBeInstanceOf ( api . type ) ;
36+ expect ( ( client as any ) [ api . method ] ) . toBeInstanceOf ( api . type ) ;
3237 } else {
33- expect ( client [ api . method ] ) . toBe ( api . value ) ;
38+ expect ( ( client as any ) [ api . method ] ) . toBe ( api . value ) ;
3439 }
3540 } ) ;
3641} ) ;
3742
3843test ( 'validate valid conf without credentials' , ( ) => {
3944 const options = {
4045 publicContainerName : 'pubcon' ,
41- privateContainerName : 'privcon'
46+ privateContainerName : 'privcon' ,
47+ path : 'path'
4248 } ;
43- const client = new azure ( options ) ;
44- expect ( client . isValid ( ) ) . toBe ( true ) ;
49+ // @ts -expect-error Bad config
50+ const client = azure ( options ) ;
51+ expect ( client . isValid ( ) ) . toBe ( false ) ;
4552} ) ;
4653
4754test ( 'validate valid conf with credentials' , ( ) => {
48- const options = {
49- publicContainerName : 'pubcon' ,
50- privateContainerName : 'privcon' ,
51- accountName : 'acc' ,
52- accountKey : 'accKey'
53- } ;
54- const client = new azure ( options ) ;
55+ const client = azure ( validOptions ) ;
5556 expect ( client . isValid ( ) ) . toBe ( true ) ;
5657} ) ;
5758
5859test ( 'validate missing public container' , ( ) => {
5960 const options = {
6061 privateContainerName : 'privcon'
6162 } ;
62- const client = new azure ( options ) ;
63+ // @ts -expect-error Bad config
64+ const client = azure ( options ) ;
6365 expect ( client . isValid ( ) ) . toBe ( false ) ;
6466} ) ;
6567
6668test ( 'validate missing private container' , ( ) => {
6769 const options = {
6870 publicContainerName : 'pubcon'
6971 } ;
70- const client = new azure ( options ) ;
72+ // @ts -expect-error Bad config
73+ const client = azure ( options ) ;
7174 expect ( client . isValid ( ) ) . toBe ( false ) ;
7275} ) ;
7376
@@ -77,7 +80,8 @@ test('validate partial credentials, no key', () => {
7780 privateContainerName : 'privcon' ,
7881 accountName : 'acc'
7982 } ;
80- const client = new azure ( options ) ;
83+ // @ts -expect-error Bad config
84+ const client = azure ( options ) ;
8185 expect ( client . isValid ( ) ) . toBe ( false ) ;
8286} ) ;
8387
@@ -87,7 +91,8 @@ test('validate partial credentials, no name', () => {
8791 privateContainerName : 'privcon' ,
8892 accountKey : 'accKey'
8993 } ;
90- const client = new azure ( options ) ;
94+ // @ts -expect-error Bad config
95+ const client = azure ( options ) ;
9196 expect ( client . isValid ( ) ) . toBe ( false ) ;
9297} ) ;
9398
@@ -123,11 +128,7 @@ test('validate partial credentials, no name', () => {
123128 }
124129] . forEach ( scenario => {
125130 test ( `test getFile ${ scenario . src } ` , async ( ) => {
126- const options = {
127- publicContainerName : 'pubcon' ,
128- privateContainerName : 'privcon'
129- } ;
130- const client = new azure ( options ) ;
131+ const client = azure ( validOptions ) ;
131132 const operation = ( ) =>
132133 client [ scenario . src . match ( / \. j s o n $ / ) ? 'getJson' : 'getFile' ] (
133134 scenario . src ,
@@ -143,11 +144,7 @@ test('validate partial credentials, no name', () => {
143144} ) ;
144145
145146test ( 'test getFile force mode' , async ( ) => {
146- const options = {
147- publicContainerName : 'pubcon' ,
148- privateContainerName : 'privcon'
149- } ;
150- const client = new azure ( options ) ;
147+ const client = azure ( validOptions ) ;
151148
152149 const data1 = await client . getFile ( 'path/to-mutable.txt' , false ) ;
153150 const data2 = await client . getFile ( 'path/to-mutable.txt' ) ;
@@ -158,15 +155,17 @@ test('test getFile force mode', async () => {
158155} ) ;
159156
160157test ( 'test getJson force mode' , async ( ) => {
161- const options = {
162- publicContainerName : 'pubcon' ,
163- privateContainerName : 'privcon'
164- } ;
165- const client = new azure ( options ) ;
158+ const client = azure ( validOptions ) ;
166159
167- const data1 = await client . getJson ( 'path/to-mutable.json' , false ) ;
168- const data2 = await client . getJson ( 'path/to-mutable.json' ) ;
169- const data3 = await client . getJson ( 'path/to-mutable.json' , true ) ;
160+ const data1 : { value : string } = await client . getJson (
161+ 'path/to-mutable.json' ,
162+ false
163+ ) ;
164+ const data2 : { value : string } = await client . getJson ( 'path/to-mutable.json' ) ;
165+ const data3 : { value : string } = await client . getJson (
166+ 'path/to-mutable.json' ,
167+ true
168+ ) ;
170169
171170 expect ( data1 . value ) . toBe ( data2 . value ) ;
172171 expect ( data3 . value ) . not . toBe ( data1 . value ) ;
@@ -179,10 +178,7 @@ test('test getJson force mode', async () => {
179178 { path : 'components/image/1.0.0/' , expected : [ ] }
180179] . forEach ( scenario => {
181180 test ( `test listObjects when bucket is not empty for folder ${ scenario . path } ` , async ( ) => {
182- const client = new azure ( {
183- publicContainerName : 'pubcon' ,
184- privateContainerName : 'privcon'
185- } ) ;
181+ const client = azure ( validOptions ) ;
186182
187183 const data = await client . listSubDirectories ( scenario . path ) ;
188184
@@ -192,10 +188,7 @@ test('test getJson force mode', async () => {
192188
193189[ 'hello' , 'path/' ] . forEach ( scenario => {
194190 test ( `test listObjects when bucket is empty for folder ${ scenario } ` , async ( ) => {
195- const client = new azure ( {
196- publicContainerName : 'my-empty-container' ,
197- privateContainerName : 'my-empty-container'
198- } ) ;
191+ const client = azure ( validOptions ) ;
199192
200193 const data = await client . listSubDirectories ( scenario ) ;
201194
@@ -204,15 +197,12 @@ test('test getJson force mode', async () => {
204197} ) ;
205198
206199test ( 'test getUrl ' , ( ) => {
207- const client = new azure ( { path : '/' } ) ;
200+ const client = azure ( validOptions ) ;
208201 expect ( client . getUrl ( 'test' , '1.0.0' , 'test.js' ) ) . toBe ( '/test/1.0.0/test.js' ) ;
209202} ) ;
210203
211204test ( 'test put dir (failure)' , ( ) => {
212- const client = new azure ( {
213- publicContainerName : 'pubcon' ,
214- privateContainerName : 'privcon'
215- } ) ;
205+ const client = azure ( validOptions ) ;
216206
217207 return expect (
218208 client . putDir (
@@ -223,10 +213,7 @@ test('test put dir (failure)', () => {
223213} ) ;
224214
225215test ( 'test put dir (stream failure throwing)' , ( ) => {
226- const client = new azure ( {
227- publicContainerName : 'pubcon' ,
228- privateContainerName : 'privcon'
229- } ) ;
216+ const client = azure ( validOptions ) ;
230217
231218 return expect (
232219 client . putDir (
@@ -237,28 +224,30 @@ test('test put dir (stream failure throwing)', () => {
237224} ) ;
238225
239226test ( 'test private putFileContent' , async ( ) => {
240- const client = new azure ( {
241- publicContainerName : 'pubcon' ,
242- privateContainerName : 'privcon'
243- } ) ;
227+ const client = azure ( validOptions ) ;
244228
245- const result = await client . putFileContent ( 'words' , 'filename.js' , true ) ;
229+ const result = ( await client . putFileContent (
230+ 'words' ,
231+ 'filename.js' ,
232+ true
233+ ) ) as any ;
246234
247235 expect ( result . container ) . toBe ( 'privcon' ) ;
248236} ) ;
249237
250238test ( 'test private putFileContent stream' , async ( ) => {
251- const client = new azure ( {
252- publicContainerName : 'pubcon' ,
253- privateContainerName : 'privcon'
254- } ) ;
239+ const client = azure ( validOptions ) ;
255240
256241 const fileContent = 'words' ;
257- const fileStream = new Readable ( ) ;
242+ const fileStream = new stream . Readable ( ) ;
258243 fileStream . push ( fileContent ) ;
259244 fileStream . push ( null ) ;
260245
261- const result = await client . putFileContent ( fileStream , 'filename.js' , true ) ;
246+ const result = ( await client . putFileContent (
247+ fileStream ,
248+ 'filename.js' ,
249+ true
250+ ) ) as any ;
262251
263252 expect ( result . container ) . toBe ( 'privcon' ) ;
264253 expect ( result . lengthWritten ) . toBe ( fileContent . length ) ;
@@ -268,38 +257,37 @@ test('test private putFileContent stream', async () => {
268257} ) ;
269258
270259test ( 'test public putFileContent' , async ( ) => {
271- const client = new azure ( {
272- publicContainerName : 'pubcon' ,
273- privateContainerName : 'privcon'
274- } ) ;
260+ const client = azure ( validOptions ) ;
275261
276- const result = await client . putFileContent ( 'words' , 'filename.gz' , false ) ;
262+ const result = ( await client . putFileContent (
263+ 'words' ,
264+ 'filename.gz' ,
265+ false
266+ ) ) as any ;
277267
278268 expect ( result . container ) . toBe ( 'pubcon' ) ;
279269} ) ;
280270
281271test ( 'test public putFileContent stream' , async ( ) => {
282- const client = new azure ( {
283- publicContainerName : 'pubcon' ,
284- privateContainerName : 'privcon'
285- } ) ;
272+ const client = azure ( validOptions ) ;
286273
287274 const fileContent = 'words' ;
288- const fileStream = new Readable ( ) ;
275+ const fileStream = new stream . Readable ( ) ;
289276 fileStream . push ( fileContent ) ;
290277 fileStream . push ( null ) ;
291278
292- const result = await client . putFileContent ( fileStream , 'filename.js' , false ) ;
279+ const result = ( await client . putFileContent (
280+ fileStream ,
281+ 'filename.js' ,
282+ false
283+ ) ) as any ;
293284
294285 expect ( result . container ) . toBe ( 'pubcon' ) ;
295286 expect ( result . lengthWritten ) . toBe ( fileContent . length ) ;
296287} ) ;
297288
298289test ( 'put a js file ' , async ( ) => {
299- const client = new azure ( {
300- publicContainerName : 'pubcon' ,
301- privateContainerName : 'privcon'
302- } ) ;
290+ const client = azure ( validOptions ) ;
303291
304292 await expect ( client . putFile ( '../path' , 'hello.js' , false ) ) . resolves . toEqual ( {
305293 container : 'pubcon' ,
0 commit comments