From 061a60a106d8aac821ada2013bb5534ff6fdaa3c Mon Sep 17 00:00:00 2001 From: Salakar Date: Sun, 13 May 2018 18:30:04 +0100 Subject: [PATCH] [tests][storage] migrate and re-write legacy tests --- bridge/e2e/storage/storage.e2e.js | 121 ++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 bridge/e2e/storage/storage.e2e.js diff --git a/bridge/e2e/storage/storage.e2e.js b/bridge/e2e/storage/storage.e2e.js new file mode 100644 index 00000000..eb8e4189 --- /dev/null +++ b/bridge/e2e/storage/storage.e2e.js @@ -0,0 +1,121 @@ +describe.only('storage()', () => { + describe('ref()', () => { + describe('toString()', () => { + it('returns the correct bucket path to the file', () => { + const app = firebase.app(); + firebase + .storage() + .ref('/uploadNope.jpeg') + .toString() + .should.equal(`gs://${app.options.storageBucket}/uploadNope.jpeg`); + }); + }); + + describe('downloadFile()', () => { + it('errors if permission denied', async () => { + try { + await firebase + .storage() + .ref('/not.jpg') + .downloadFile( + `${firebase.storage.Native.DOCUMENT_DIRECTORY_PATH}/not.jpg` + ); + return Promise.reject(new Error('No permission denied error')); + } catch (error) { + error.code.should.equal('storage/unauthorized'); + error.message.includes('not authorized').should.be.true(); + return Promise.resolve(); + } + }); + + it('downloads a file', async () => { + const meta = await firebase + .storage() + .ref('/ok.jpeg') + .downloadFile( + `${firebase.storage.Native.DOCUMENT_DIRECTORY_PATH}/ok.jpeg` + ); + + meta.state.should.eql(firebase.storage.TaskState.SUCCESS); + meta.bytesTransferred.should.eql(meta.totalBytes); + }); + }); + + describe('putFile()', () => { + before(async () => { + await firebase + .storage() + .ref('/ok.jpeg') + .downloadFile( + `${firebase.storage.Native.DOCUMENT_DIRECTORY_PATH}/ok.jpeg` + ); + }); + + it('errors if permission denied', async () => { + try { + await firebase + .storage() + .ref('/uploadNope.jpeg') + .putFile( + `${firebase.storage.Native.DOCUMENT_DIRECTORY_PATH}/ok.jpeg` + ); + return Promise.reject(new Error('No permission denied error')); + } catch (error) { + error.code.should.equal('storage/unauthorized'); + error.message.includes('not authorized').should.be.true(); + return Promise.resolve(); + } + }); + + it('uploads a file', async () => { + const uploadTaskSnapshot = await firebase + .storage() + .ref('/uploadOk.jpeg') + .putFile( + `${firebase.storage.Native.DOCUMENT_DIRECTORY_PATH}/ok.jpeg` + ); + + uploadTaskSnapshot.state.should.eql(firebase.storage.TaskState.SUCCESS); + uploadTaskSnapshot.bytesTransferred.should.eql( + uploadTaskSnapshot.totalBytes + ); + uploadTaskSnapshot.metadata.should.be.an.Object(); + uploadTaskSnapshot.downloadURL.should.be.a.String(); + }); + }); + + describe('on()', () => { + before(async () => { + await firebase + .storage() + .ref('/ok.jpeg') + .downloadFile( + `${firebase.storage.Native.DOCUMENT_DIRECTORY_PATH}/ok.jpeg` + ); + }); + + it('listens to upload state', () => { + const { resolve, reject, promise } = Promise.defer(); + const path = `${ + firebase.storage.Native.DOCUMENT_DIRECTORY_PATH + }/ok.jpeg`; + const ref = firebase.storage().ref('/uploadOk.jpeg'); + + const unsubscribe = ref.putFile(path).on( + firebase.storage.TaskEvent.STATE_CHANGED, + snapshot => { + if (snapshot.state === firebase.storage.TaskState.SUCCESS) { + resolve(); + } + }, + error => { + unsubscribe(); + reject(error); + } + ); + + return promise; + }); + }); + }); +});