1
1
import { forkJoin } from 'rxjs' ;
2
2
import { mergeMap , tap } from 'rxjs/operators' ;
3
- import { inject , TestBed } from '@angular/core/testing' ;
3
+ import { TestBed } from '@angular/core/testing' ;
4
4
import { AngularFireModule , FIREBASE_APP_NAME , FIREBASE_OPTIONS , FirebaseApp } from '@angular/fire' ;
5
5
import { AngularFireStorage , AngularFireStorageModule , AngularFireUploadTask , BUCKET } from './public_api' ;
6
6
import { COMMON_CONFIG } from '../test-config' ;
@@ -18,10 +18,9 @@ describe('AngularFireStorage', () => {
18
18
AngularFireStorageModule
19
19
]
20
20
} ) ;
21
- inject ( [ FirebaseApp , AngularFireStorage ] , ( app_ : FirebaseApp , _storage : AngularFireStorage ) => {
22
- app = app_ ;
23
- afStorage = _storage ;
24
- } ) ( ) ;
21
+
22
+ app = TestBed . inject ( FirebaseApp ) ;
23
+ afStorage = TestBed . inject ( AngularFireStorage ) ;
25
24
} ) ;
26
25
27
26
afterEach ( ( ) => {
@@ -47,35 +46,41 @@ describe('AngularFireStorage', () => {
47
46
48
47
it ( 'should upload and delete a file' , ( done ) => {
49
48
const data = { angular : 'fire' } ;
50
- const blob = new Blob ( [ JSON . stringify ( data ) ] , { type : 'application/json' } ) ;
49
+ const blob = new Blob ( [ JSON . stringify ( data ) ] , { type : 'application/json' } ) ;
51
50
const ref = afStorage . ref ( 'af.json' ) ;
52
51
const task = ref . put ( blob ) ;
53
- const sub = task . snapshotChanges ( )
52
+ task . snapshotChanges ( )
54
53
. subscribe (
55
- snap => { expect ( snap ) . toBeDefined ( ) ; } ,
56
- e => { done . fail ( ) ; } ,
54
+ snap => {
55
+ expect ( snap ) . toBeDefined ( ) ;
56
+ } ,
57
+ done . fail ,
57
58
( ) => {
58
59
ref . delete ( ) . subscribe ( done , done . fail ) ;
59
60
} ) ;
60
61
} ) ;
61
62
62
63
it ( 'should upload a file and observe the download url' , ( done ) => {
63
64
const data = { angular : 'fire' } ;
64
- const blob = new Blob ( [ JSON . stringify ( data ) ] , { type : 'application/json' } ) ;
65
+ const blob = new Blob ( [ JSON . stringify ( data ) ] , { type : 'application/json' } ) ;
65
66
const ref = afStorage . ref ( 'af.json' ) ;
66
- const task = ref . put ( blob ) . then ( ( ) => {
67
+ ref . put ( blob ) . then ( ( ) => {
67
68
const url$ = ref . getDownloadURL ( ) ;
68
69
url$ . subscribe (
69
- url => { expect ( url ) . toBeDefined ( ) ; } ,
70
- e => { done . fail ( ) ; } ,
71
- ( ) => { ref . delete ( ) . subscribe ( done , done . fail ) ; }
70
+ url => {
71
+ expect ( url ) . toBeDefined ( ) ;
72
+ } ,
73
+ done . fail ,
74
+ ( ) => {
75
+ ref . delete ( ) . subscribe ( done , done . fail ) ;
76
+ }
72
77
) ;
73
78
} ) ;
74
79
} ) ;
75
80
76
81
it ( 'should resolve the task as a promise' , ( done ) => {
77
82
const data = { angular : 'promise' } ;
78
- const blob = new Blob ( [ JSON . stringify ( data ) ] , { type : 'application/json' } ) ;
83
+ const blob = new Blob ( [ JSON . stringify ( data ) ] , { type : 'application/json' } ) ;
79
84
const ref = afStorage . ref ( 'af.json' ) ;
80
85
const task : AngularFireUploadTask = ref . put ( blob ) ;
81
86
task . then ( snap => {
@@ -90,37 +95,37 @@ describe('AngularFireStorage', () => {
90
95
91
96
it ( 'it should upload, download, and delete' , ( done ) => {
92
97
const data = { angular : 'fire' } ;
93
- const blob = new Blob ( [ JSON . stringify ( data ) ] , { type : 'application/json' } ) ;
98
+ const blob = new Blob ( [ JSON . stringify ( data ) ] , { type : 'application/json' } ) ;
94
99
const ref = afStorage . ref ( 'af.json' ) ;
95
100
const task = ref . put ( blob ) ;
96
101
// Wait for the upload
97
- const sub = forkJoin ( task . snapshotChanges ( ) )
102
+ forkJoin ( [ task . snapshotChanges ( ) ] )
98
103
. pipe (
99
104
// get the url download
100
105
mergeMap ( ( ) => ref . getDownloadURL ( ) ) ,
101
106
// assert the URL
102
107
tap ( url => expect ( url ) . toBeDefined ( ) ) ,
103
108
// Delete the file
104
- mergeMap ( url => ref . delete ( ) )
109
+ mergeMap ( ( ) => ref . delete ( ) )
105
110
)
106
111
// finish the test
107
112
. subscribe ( done , done . fail ) ;
108
113
} ) ;
109
114
110
115
it ( 'should upload, get metadata, and delete' , ( done ) => {
111
116
const data = { angular : 'fire' } ;
112
- const blob = new Blob ( [ JSON . stringify ( data ) ] , { type : 'application/json' } ) ;
117
+ const blob = new Blob ( [ JSON . stringify ( data ) ] , { type : 'application/json' } ) ;
113
118
const ref = afStorage . ref ( 'af.json' ) ;
114
119
const task = ref . put ( blob , { customMetadata : { blah : 'blah' } } ) ;
115
120
// Wait for the upload
116
- const sub = forkJoin ( task . snapshotChanges ( ) )
121
+ forkJoin ( [ task . snapshotChanges ( ) ] )
117
122
. pipe (
118
123
// get the metadata download
119
124
mergeMap ( ( ) => ref . getMetadata ( ) ) ,
120
125
// assert the URL
121
126
tap ( meta => expect ( meta . customMetadata ) . toEqual ( { blah : 'blah' } ) ) ,
122
127
// Delete the file
123
- mergeMap ( meta => ref . delete ( ) )
128
+ mergeMap ( ( ) => ref . delete ( ) )
124
129
)
125
130
// finish the test
126
131
. subscribe ( done , done . fail ) ;
@@ -148,14 +153,13 @@ describe('AngularFireStorage w/options', () => {
148
153
] ,
149
154
providers : [
150
155
{ provide : FIREBASE_APP_NAME , useValue : firebaseAppName } ,
151
- { provide : FIREBASE_OPTIONS , useValue : COMMON_CONFIG } ,
156
+ { provide : FIREBASE_OPTIONS , useValue : COMMON_CONFIG } ,
152
157
{ provide : BUCKET , useValue : storageBucket }
153
158
]
154
159
} ) ;
155
- inject ( [ FirebaseApp , AngularFireStorage ] , ( app_ : FirebaseApp , _storage : AngularFireStorage ) => {
156
- app = app_ ;
157
- afStorage = _storage ;
158
- } ) ( ) ;
160
+
161
+ app = TestBed . inject ( FirebaseApp ) ;
162
+ afStorage = TestBed . inject ( AngularFireStorage ) ;
159
163
} ) ;
160
164
161
165
afterEach ( ( ) => {
@@ -181,26 +185,26 @@ describe('AngularFireStorage w/options', () => {
181
185
} ) ;
182
186
183
187
it ( 'storage be pointing towards a different bucket' , ( ) => {
184
- expect ( afStorage . storage . ref ( ) . toString ( ) ) . toEqual ( `gs://${ storageBucket } /` ) ;
188
+ expect ( afStorage . storage . ref ( ) . toString ( ) ) . toEqual ( `gs://${ storageBucket } /` ) ;
185
189
} ) ;
186
190
187
191
// TODO tests for Node?
188
192
if ( typeof Blob !== 'undefined' ) {
189
193
190
194
it ( 'it should upload, download, and delete' , ( done ) => {
191
195
const data = { angular : 'fire' } ;
192
- const blob = new Blob ( [ JSON . stringify ( data ) ] , { type : 'application/json' } ) ;
196
+ const blob = new Blob ( [ JSON . stringify ( data ) ] , { type : 'application/json' } ) ;
193
197
const ref = afStorage . ref ( 'af.json' ) ;
194
198
const task = ref . put ( blob ) ;
195
199
// Wait for the upload
196
- const sub = forkJoin ( task . snapshotChanges ( ) )
200
+ forkJoin ( [ task . snapshotChanges ( ) ] )
197
201
. pipe (
198
202
// get the url download
199
203
mergeMap ( ( ) => ref . getDownloadURL ( ) ) ,
200
204
// assert the URL
201
205
tap ( url => expect ( url ) . toMatch ( new RegExp ( `https:\\/\\/firebasestorage\\.googleapis\\.com\\/v0\\/b\\/${ storageBucket } \\/o\\/af\\.json` ) ) ) ,
202
206
// Delete the file
203
- mergeMap ( url => ref . delete ( ) )
207
+ mergeMap ( ( ) => ref . delete ( ) )
204
208
)
205
209
// finish the test
206
210
. subscribe ( done , done . fail ) ;
0 commit comments