describe('AngularFireStorage', () => {
let app: FirebaseApp;
let afStorage: AngularFireStorage;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
AngularFireModule.initializeApp(COMMON_CONFIG),
AngularFireStorageModule
]
});
inject([FirebaseApp, AngularFireStorage], (app_: FirebaseApp, _storage: AngularFireStorage) => {
app = app_;
afStorage = _storage;
})();
});
afterEach(done => {
app.delete().then(done, done.fail);
});
it('should exist', () => {
expect(afStorage instanceof AngularFireStorage).toBe(true);
});
it('should have the Firebase storage instance', () => {
expect(afStorage.storage).toBeDefined();
});
it('should have an initialized Firebase app', () => {
expect(afStorage.storage.app).toBeDefined();
expect(afStorage.storage.app).toEqual(app);
});
describe('upload task', () => {
it('should upload and delete a file', (done) => {
const data = { angular: "fire" };
const blob = new Blob([JSON.stringify(data)], { type : 'application/json' });
const ref = afStorage.ref('af.json');
const task = ref.put(blob);
const sub = task.snapshotChanges()
.subscribe(
snap => { expect(snap).toBeDefined() },
e => { done.fail(); },
() => {
ref.delete().subscribe(done, done.fail);
});
});
it('should upload a file and observe the download url', (done) => {
const data = { angular: "fire" };
const blob = new Blob([JSON.stringify(data)], { type : 'application/json' });
const ref = afStorage.ref('af.json');
const task = ref.put(blob);
const url$ = task.downloadURL();
url$.subscribe(
url => { expect(url).toBeDefined(); },
e => { done.fail(); },
() => { ref.delete().subscribe(done, done.fail); }
);
});
it('should resolve the task as a promise', (done) => {
const data = { angular: "promise" };
const blob = new Blob([JSON.stringify(data)], { type : 'application/json' });
const ref = afStorage.ref('af.json');
const task: AngularFireUploadTask = ref.put(blob);
task.then(snap => {
expect(snap).toBeDefined();
done();
}).catch(done.fail);
});
});
describe('reference', () => {
it('it should upload, download, and delete', (done) => {
const data = { angular: "fire" };
const blob = new Blob([JSON.stringify(data)], { type : 'application/json' });
const ref = afStorage.ref('af.json');
const task = ref.put(blob);
// Wait for the upload
const sub = forkJoin(task.snapshotChanges())
// get the url download
.mergeMap(() => ref.getDownloadURL())
// assert the URL
.do(url => expect(url).toBeDefined())
// Delete the file
.mergeMap(url => ref.delete())
// finish the test
.subscribe(done, done.fail);
});
it('should upload, get metadata, and delete', (done) => {
const data = { angular: "fire" };
const blob = new Blob([JSON.stringify(data)], { type : 'application/json' });
const ref = afStorage.ref('af.json');
const task = ref.put(blob, { customMetadata: { blah: 'blah' } });
//.........这里部分代码省略.........
请发表评论