1
- import { forkJoin } from 'rxjs' ;
1
+ import { forkJoin , from } from 'rxjs' ;
2
2
import { mergeMap , tap } from 'rxjs/operators' ;
3
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' ;
7
- import 'firebase/storage' ;
8
7
import { rando } from '../firestore/utils.spec' ;
8
+ import { GetDownloadURLPipe } from './pipes/storageUrl.pipe' ;
9
+ import { ChangeDetectorRef } from '@angular/core' ;
10
+ import 'firebase/storage' ;
11
+
12
+ if ( typeof XMLHttpRequest === 'undefined' ) {
13
+ globalThis . XMLHttpRequest = require ( 'xhr2' ) ;
14
+ }
15
+
16
+ const blobOrBuffer = ( data : string , options : { } ) => {
17
+ if ( typeof Blob === 'undefined' ) {
18
+ return Buffer . from ( data , 'utf8' ) ;
19
+ } else {
20
+ return new Blob ( [ JSON . stringify ( data ) ] , options ) ;
21
+ }
22
+ } ;
9
23
10
24
describe ( 'AngularFireStorage' , ( ) => {
11
25
let app : FirebaseApp ;
12
26
let afStorage : AngularFireStorage ;
27
+ let cdr : ChangeDetectorRef ;
13
28
14
29
beforeEach ( ( ) => {
15
30
TestBed . configureTestingModule ( {
16
31
imports : [
17
32
AngularFireModule . initializeApp ( COMMON_CONFIG , rando ( ) ) ,
18
- AngularFireStorageModule
33
+ AngularFireStorageModule ,
34
+ ] ,
35
+ providers : [
36
+ ChangeDetectorRef
19
37
]
20
38
} ) ;
21
39
22
40
app = TestBed . inject ( FirebaseApp ) ;
23
41
afStorage = TestBed . inject ( AngularFireStorage ) ;
42
+ cdr = TestBed . inject ( ChangeDetectorRef ) ;
24
43
} ) ;
25
44
26
45
afterEach ( ( ) => {
@@ -39,101 +58,96 @@ describe('AngularFireStorage', () => {
39
58
expect ( afStorage . storage . app ) . toBeDefined ( ) ;
40
59
} ) ;
41
60
42
- // TODO tests for node?
43
- if ( typeof Blob !== 'undefined' ) {
44
-
45
- describe ( 'upload task' , ( ) => {
46
-
47
- it ( 'should upload and delete a file' , ( done ) => {
48
- const data = { angular : 'fire' } ;
49
- const blob = new Blob ( [ JSON . stringify ( data ) ] , { type : 'application/json' } ) ;
50
- const ref = afStorage . ref ( 'af.json' ) ;
51
- const task = ref . put ( blob ) ;
52
- task . snapshotChanges ( )
53
- . subscribe (
54
- snap => {
55
- expect ( snap ) . toBeDefined ( ) ;
56
- } ,
57
- done . fail ,
58
- ( ) => {
59
- ref . delete ( ) . subscribe ( done , done . fail ) ;
60
- } ) ;
61
- } ) ;
62
-
63
- it ( 'should upload a file and observe the download url' , ( done ) => {
64
- const data = { angular : 'fire' } ;
65
- const blob = new Blob ( [ JSON . stringify ( data ) ] , { type : 'application/json' } ) ;
66
- const ref = afStorage . ref ( 'af.json' ) ;
67
- ref . put ( blob ) . then ( ( ) => {
68
- const url$ = ref . getDownloadURL ( ) ;
69
- url$ . subscribe (
70
- url => {
71
- expect ( url ) . toBeDefined ( ) ;
72
- } ,
73
- done . fail ,
74
- ( ) => {
75
- ref . delete ( ) . subscribe ( done , done . fail ) ;
76
- }
77
- ) ;
78
- } ) ;
79
- } ) ;
61
+ describe ( 'upload task' , ( ) => {
62
+
63
+ it ( 'should upload and delete a file' , ( done ) => {
64
+ const data = { angular : 'fire' } ;
65
+ const blob = blobOrBuffer ( JSON . stringify ( data ) , { type : 'application/json' } ) ;
66
+ const ref = afStorage . ref ( 'af.json' ) ;
67
+ const task = ref . put ( blob ) ;
68
+ task . snapshotChanges ( )
69
+ . subscribe (
70
+ snap => {
71
+ expect ( snap ) . toBeDefined ( ) ;
72
+ } ,
73
+ done . fail ,
74
+ ( ) => {
75
+ ref . delete ( ) . subscribe ( done , done . fail ) ;
76
+ } ) ;
77
+ } ) ;
80
78
81
- it ( 'should resolve the task as a promise' , ( done ) => {
82
- const data = { angular : 'promise' } ;
83
- const blob = new Blob ( [ JSON . stringify ( data ) ] , { type : 'application/json' } ) ;
84
- const ref = afStorage . ref ( 'af.json' ) ;
85
- const task : AngularFireUploadTask = ref . put ( blob ) ;
86
- task . then ( snap => {
87
- expect ( snap ) . toBeDefined ( ) ;
88
- done ( ) ;
89
- } ) . catch ( done . fail ) ;
79
+ it ( 'should upload a file and observe the download url' , ( done ) => {
80
+ const data = { angular : 'fire' } ;
81
+ const blob = blobOrBuffer ( JSON . stringify ( data ) , { type : 'application/json' } ) ;
82
+ const ref = afStorage . ref ( 'af.json' ) ;
83
+ ref . put ( blob ) . then ( ( ) => {
84
+ const url$ = ref . getDownloadURL ( ) ;
85
+ url$ . subscribe (
86
+ url => {
87
+ expect ( url ) . toBeDefined ( ) ;
88
+ } ,
89
+ done . fail ,
90
+ ( ) => {
91
+ ref . delete ( ) . subscribe ( done , done . fail ) ;
92
+ }
93
+ ) ;
90
94
} ) ;
91
-
92
95
} ) ;
93
96
94
- describe ( 'reference' , ( ) => {
97
+ it ( 'should resolve the task as a promise' , ( done ) => {
98
+ const data = { angular : 'promise' } ;
99
+ const blob = blobOrBuffer ( JSON . stringify ( data ) , { type : 'application/json' } ) ;
100
+ const ref = afStorage . ref ( 'af.json' ) ;
101
+ const task : AngularFireUploadTask = ref . put ( blob ) ;
102
+ task . then ( snap => {
103
+ expect ( snap ) . toBeDefined ( ) ;
104
+ done ( ) ;
105
+ } ) . catch ( done . fail ) ;
106
+ } ) ;
95
107
96
- it ( 'it should upload, download, and delete' , ( done ) => {
97
- const data = { angular : 'fire' } ;
98
- const blob = new Blob ( [ JSON . stringify ( data ) ] , { type : 'application/json' } ) ;
99
- const ref = afStorage . ref ( 'af.json' ) ;
100
- const task = ref . put ( blob ) ;
101
- // Wait for the upload
102
- forkJoin ( [ task . snapshotChanges ( ) ] )
103
- . pipe (
104
- // get the url download
105
- mergeMap ( ( ) => ref . getDownloadURL ( ) ) ,
106
- // assert the URL
107
- tap ( url => expect ( url ) . toBeDefined ( ) ) ,
108
- // Delete the file
109
- mergeMap ( ( ) => ref . delete ( ) )
110
- )
111
- // finish the test
112
- . subscribe ( done , done . fail ) ;
113
- } ) ;
108
+ } ) ;
114
109
115
- it ( 'should upload, get metadata, and delete' , ( done ) => {
116
- const data = { angular : 'fire' } ;
117
- const blob = new Blob ( [ JSON . stringify ( data ) ] , { type : 'application/json' } ) ;
118
- const ref = afStorage . ref ( 'af.json' ) ;
119
- const task = ref . put ( blob , { customMetadata : { blah : 'blah' } } ) ;
120
- // Wait for the upload
121
- forkJoin ( [ task . snapshotChanges ( ) ] )
122
- . pipe (
123
- // get the metadata download
124
- mergeMap ( ( ) => ref . getMetadata ( ) ) ,
125
- // assert the URL
126
- tap ( meta => expect ( meta . customMetadata ) . toEqual ( { blah : 'blah' } ) ) ,
127
- // Delete the file
128
- mergeMap ( ( ) => ref . delete ( ) )
129
- )
130
- // finish the test
131
- . subscribe ( done , done . fail ) ;
132
- } ) ;
110
+ describe ( 'reference' , ( ) => {
111
+
112
+ it ( 'it should upload, download, and delete' , ( done ) => {
113
+ const data = { angular : 'fire' } ;
114
+ const blob = blobOrBuffer ( JSON . stringify ( data ) , { type : 'application/json' } ) ;
115
+ const ref = afStorage . ref ( 'af.json' ) ;
116
+ const task = ref . put ( blob ) ;
117
+ // Wait for the upload
118
+ forkJoin ( [ task . snapshotChanges ( ) ] )
119
+ . pipe (
120
+ // get the url download
121
+ mergeMap ( ( ) => ref . getDownloadURL ( ) ) ,
122
+ // assert the URL
123
+ tap ( url => expect ( url ) . toBeDefined ( ) ) ,
124
+ // Delete the file
125
+ mergeMap ( ( ) => ref . delete ( ) )
126
+ )
127
+ // finish the test
128
+ . subscribe ( done , done . fail ) ;
129
+ } ) ;
133
130
131
+ it ( 'should upload, get metadata, and delete' , ( done ) => {
132
+ const data = { angular : 'fire' } ;
133
+ const blob = blobOrBuffer ( JSON . stringify ( data ) , { type : 'application/json' } ) ;
134
+ const ref = afStorage . ref ( 'af.json' ) ;
135
+ const task = ref . put ( blob , { customMetadata : { blah : 'blah' } } ) ;
136
+ // Wait for the upload
137
+ forkJoin ( [ task . snapshotChanges ( ) ] )
138
+ . pipe (
139
+ // get the metadata download
140
+ mergeMap ( ( ) => ref . getMetadata ( ) ) ,
141
+ // assert the URL
142
+ tap ( meta => expect ( meta . customMetadata ) . toEqual ( { blah : 'blah' } ) ) ,
143
+ // Delete the file
144
+ mergeMap ( ( ) => ref . delete ( ) )
145
+ )
146
+ // finish the test
147
+ . subscribe ( done , done . fail ) ;
134
148
} ) ;
135
149
136
- }
150
+ } ) ;
137
151
138
152
} ) ;
139
153
@@ -193,7 +207,7 @@ describe('AngularFireStorage w/options', () => {
193
207
194
208
it ( 'it should upload, download, and delete' , ( done ) => {
195
209
const data = { angular : 'fire' } ;
196
- const blob = new Blob ( [ JSON . stringify ( data ) ] , { type : 'application/json' } ) ;
210
+ const blob = blobOrBuffer ( JSON . stringify ( data ) , { type : 'application/json' } ) ;
197
211
const ref = afStorage . ref ( 'af.json' ) ;
198
212
const task = ref . put ( blob ) ;
199
213
// Wait for the upload
0 commit comments