|
|
@@ -211,12 +211,29 @@ describe( 'FileRepository', () => {
|
|
|
expect( fileRepository.getLoader( file1 ) ).to.be.null;
|
|
|
} );
|
|
|
|
|
|
- it( 'should return loader by file instance', () => {
|
|
|
+ it( 'should return loader by file instance (initialized with file)', () => {
|
|
|
const file = createNativeFileMock();
|
|
|
const loader = fileRepository.createLoader( file );
|
|
|
|
|
|
expect( fileRepository.getLoader( file ) ).to.equal( loader );
|
|
|
} );
|
|
|
+
|
|
|
+ it( 'should return loader by promise instance (initialized with promise)', () => {
|
|
|
+ const promise = Promise.resolve( createNativeFileMock() );
|
|
|
+ const loader = fileRepository.createLoader( promise );
|
|
|
+
|
|
|
+ expect( fileRepository.getLoader( promise ) ).to.equal( loader );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return loader by file instance (initialized with promise)', done => {
|
|
|
+ const promise = Promise.resolve( createNativeFileMock() );
|
|
|
+ const loader = fileRepository.createLoader( promise );
|
|
|
+
|
|
|
+ loader.file.then( fileInstance => {
|
|
|
+ expect( fileRepository.getLoader( fileInstance ) ).to.equal( loader );
|
|
|
+ done();
|
|
|
+ } );
|
|
|
+ } );
|
|
|
} );
|
|
|
|
|
|
describe( 'destroyLoader()', () => {
|
|
|
@@ -233,13 +250,55 @@ describe( 'FileRepository', () => {
|
|
|
|
|
|
sinon.assert.calledOnce( destroySpy );
|
|
|
expect( fileRepository.getLoader( file ) ).to.be.null;
|
|
|
+ expect( fileRepository.loaders.length ).to.equal( 0 );
|
|
|
+ expect( Array.from( fileRepository._loadersMap.keys ).length ).to.equal( 0 );
|
|
|
} );
|
|
|
|
|
|
- it( 'should destroy loader by provided file', () => {
|
|
|
+ it( 'should destroy loader by provided file (initialized with file)', () => {
|
|
|
fileRepository.destroyLoader( file );
|
|
|
|
|
|
sinon.assert.calledOnce( destroySpy );
|
|
|
expect( fileRepository.getLoader( file ) ).to.be.null;
|
|
|
+ expect( fileRepository.loaders.length ).to.equal( 0 );
|
|
|
+ expect( Array.from( fileRepository._loadersMap.keys ).length ).to.equal( 0 );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should destroy loader by provided promise (initialized with promise)', () => {
|
|
|
+ fileRepository.destroyLoader( loader );
|
|
|
+
|
|
|
+ const promise = new Promise( resolve => resolve( createNativeFileMock() ) );
|
|
|
+ const newLoader = fileRepository.createLoader( promise );
|
|
|
+
|
|
|
+ destroySpy = testUtils.sinon.spy( newLoader, '_destroy' );
|
|
|
+
|
|
|
+ fileRepository.destroyLoader( promise );
|
|
|
+
|
|
|
+ sinon.assert.calledOnce( destroySpy );
|
|
|
+ expect( fileRepository.getLoader( promise ) ).to.be.null;
|
|
|
+ expect( fileRepository.loaders.length ).to.equal( 0 );
|
|
|
+ expect( Array.from( fileRepository._loadersMap.keys() ).length ).to.equal( 0 );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should destroy loader by provided file (initialized with promise)', () => {
|
|
|
+ fileRepository.destroyLoader( loader );
|
|
|
+
|
|
|
+ const promise = Promise.resolve( createNativeFileMock() );
|
|
|
+ const newLoader = fileRepository.createLoader( promise );
|
|
|
+
|
|
|
+ destroySpy = testUtils.sinon.spy( newLoader, '_destroy' );
|
|
|
+
|
|
|
+ return newLoader.file.then( fileInstance => {
|
|
|
+ expect( fileRepository.loaders.length ).to.equal( 1 );
|
|
|
+ expect( Array.from( fileRepository._loadersMap.keys() ).length ).to.equal( 2 );
|
|
|
+
|
|
|
+ fileRepository.destroyLoader( fileInstance );
|
|
|
+
|
|
|
+ sinon.assert.calledOnce( destroySpy );
|
|
|
+
|
|
|
+ expect( fileRepository.getLoader( fileInstance ) ).to.be.null;
|
|
|
+ expect( fileRepository.loaders.length ).to.equal( 0 );
|
|
|
+ expect( Array.from( fileRepository._loadersMap.keys() ).length ).to.equal( 0 );
|
|
|
+ } );
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
@@ -262,8 +321,12 @@ describe( 'FileRepository', () => {
|
|
|
expect( loader.id ).to.be.a( 'string' );
|
|
|
} );
|
|
|
|
|
|
- it( 'should initialize file', () => {
|
|
|
- expect( loader.file ).to.equal( file );
|
|
|
+ it( 'should initialize filePromiseWrapper', () => {
|
|
|
+ expect( loader._filePromiseWrapper ).to.not.be.null;
|
|
|
+ expect( loader._filePromiseWrapper.promise ).to.be.instanceOf( Promise );
|
|
|
+ expect( loader._filePromiseWrapper.resolver ).to.be.instanceOf( Function );
|
|
|
+ expect( loader._filePromiseWrapper.rejecter ).to.be.instanceOf( Function );
|
|
|
+ expect( loader._filePromiseWrapper.isFulfilled ).to.be.false;
|
|
|
} );
|
|
|
|
|
|
it( 'should initialize adapter', () => {
|
|
|
@@ -333,8 +396,48 @@ describe( 'FileRepository', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
+ describe( 'file getter', () => {
|
|
|
+ it( 'should return promise', () => {
|
|
|
+ expect( loader.file ).to.instanceof( Promise );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return promise which resolves to a file', () => {
|
|
|
+ return loader.file.then( fileInstance => {
|
|
|
+ expect( fileInstance ).to.equal( file );
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return promise which resolves to null after loader is destroyed (destroy before)', () => {
|
|
|
+ loader._destroy();
|
|
|
+
|
|
|
+ return loader.file.then( fileInstance => {
|
|
|
+ expect( fileInstance ).to.be.null;
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return promise which resolves to null after loader is destroyed (destroy after)', () => {
|
|
|
+ const promise = loader.file.then( fileInstance => {
|
|
|
+ expect( fileInstance ).to.be.null;
|
|
|
+ } );
|
|
|
+
|
|
|
+ loader._destroy();
|
|
|
+
|
|
|
+ return promise;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return promise which resolves to null after loader is destroyed (file promise resolved, destroy after)', () => {
|
|
|
+ return loader._filePromiseWrapper.promise.then( () => {
|
|
|
+ loader.file.then( fileInstance => {
|
|
|
+ expect( fileInstance ).to.be.null;
|
|
|
+ } );
|
|
|
+
|
|
|
+ loader._destroy();
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+
|
|
|
describe( 'read()', () => {
|
|
|
- it( 'should throw error when status is defferent than idle', () => {
|
|
|
+ it( 'should throw error when status is different than idle', () => {
|
|
|
loader.status = 'uploading';
|
|
|
|
|
|
expect( () => {
|
|
|
@@ -352,7 +455,48 @@ describe( 'FileRepository', () => {
|
|
|
expect( loader.status ).to.equal( 'reading' );
|
|
|
} );
|
|
|
|
|
|
- it( 'should reject promise when reading is aborted', () => {
|
|
|
+ it( 'should resolve promise when file promise is resolved', () => {
|
|
|
+ let resolveFile = null;
|
|
|
+
|
|
|
+ const filePromise = new Promise( resolve => {
|
|
|
+ resolveFile = resolve;
|
|
|
+ } );
|
|
|
+
|
|
|
+ const loader = fileRepository.createLoader( filePromise );
|
|
|
+
|
|
|
+ const promise = loader.read()
|
|
|
+ .then( data => {
|
|
|
+ expect( data ).to.equal( 'result data' );
|
|
|
+ expect( loader.status ).to.equal( 'idle' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ resolveFile( createNativeFileMock() );
|
|
|
+
|
|
|
+ loader.file.then( () => nativeReaderMock.mockSuccess( 'result data' ) );
|
|
|
+
|
|
|
+ return promise;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should reject promise when file promise is rejected', () => {
|
|
|
+ let rejectFile = null;
|
|
|
+
|
|
|
+ const filePromise = new Promise( ( resolve, reject ) => {
|
|
|
+ rejectFile = reject;
|
|
|
+ } );
|
|
|
+
|
|
|
+ const loader = fileRepository.createLoader( filePromise );
|
|
|
+
|
|
|
+ const promise = loader.read().catch( e => {
|
|
|
+ expect( e ).to.equal( 'File loading error' );
|
|
|
+ expect( loader.status ).to.equal( 'error' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ rejectFile( 'File loading error' );
|
|
|
+
|
|
|
+ return promise;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should reject promise when reading is aborted (before file promise is resolved)', () => {
|
|
|
const promise = loader.read().catch( e => {
|
|
|
expect( e ).to.equal( 'aborted' );
|
|
|
expect( loader.status ).to.equal( 'aborted' );
|
|
|
@@ -363,32 +507,43 @@ describe( 'FileRepository', () => {
|
|
|
return promise;
|
|
|
} );
|
|
|
|
|
|
- it( 'should reject promise on reading error', () => {
|
|
|
+ it( 'should reject promise when reading is aborted (after file promise is resolved)', () => {
|
|
|
+ const promise = loader.read().catch( e => {
|
|
|
+ expect( e ).to.equal( 'aborted' );
|
|
|
+ expect( loader.status ).to.equal( 'aborted' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ loader.file.then( () => loader.abort() );
|
|
|
+
|
|
|
+ return promise;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should reject promise on reading error (after file promise is resolved)', () => {
|
|
|
const promise = loader.read().catch( e => {
|
|
|
expect( e ).to.equal( 'reading error' );
|
|
|
expect( loader.status ).to.equal( 'error' );
|
|
|
} );
|
|
|
|
|
|
- nativeReaderMock.mockError( 'reading error' );
|
|
|
+ loader.file.then( () => nativeReaderMock.mockError( 'reading error' ) );
|
|
|
|
|
|
return promise;
|
|
|
} );
|
|
|
|
|
|
- it( 'should resolve promise on reading complete', () => {
|
|
|
+ it( 'should resolve promise on reading complete (after file promise is resolved)', () => {
|
|
|
const promise = loader.read()
|
|
|
.then( data => {
|
|
|
expect( data ).to.equal( 'result data' );
|
|
|
expect( loader.status ).to.equal( 'idle' );
|
|
|
} );
|
|
|
|
|
|
- nativeReaderMock.mockSuccess( 'result data' );
|
|
|
+ loader.file.then( () => nativeReaderMock.mockSuccess( 'result data' ) );
|
|
|
|
|
|
return promise;
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
describe( 'upload()', () => {
|
|
|
- it( 'should throw error when status is defferent than idle', () => {
|
|
|
+ it( 'should throw error when status is different than idle', () => {
|
|
|
loader.status = 'reading';
|
|
|
|
|
|
expect( () => {
|
|
|
@@ -406,7 +561,48 @@ describe( 'FileRepository', () => {
|
|
|
expect( loader.status ).to.equal( 'uploading' );
|
|
|
} );
|
|
|
|
|
|
- it( 'should reject promise when uploading is aborted', () => {
|
|
|
+ it( 'should resolve promise when file promise is resolved', () => {
|
|
|
+ let resolveFile = null;
|
|
|
+
|
|
|
+ const filePromise = new Promise( resolve => {
|
|
|
+ resolveFile = resolve;
|
|
|
+ } );
|
|
|
+
|
|
|
+ const loader = fileRepository.createLoader( filePromise );
|
|
|
+
|
|
|
+ const promise = loader.upload()
|
|
|
+ .then( data => {
|
|
|
+ expect( data ).to.equal( 'result data' );
|
|
|
+ expect( loader.status ).to.equal( 'idle' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ resolveFile( createNativeFileMock() );
|
|
|
+
|
|
|
+ loader.file.then( () => adapterMock.mockSuccess( 'result data' ) );
|
|
|
+
|
|
|
+ return promise;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should reject promise when file promise is rejected', () => {
|
|
|
+ let rejectFile = null;
|
|
|
+
|
|
|
+ const filePromise = new Promise( ( resolve, reject ) => {
|
|
|
+ rejectFile = reject;
|
|
|
+ } );
|
|
|
+
|
|
|
+ const loader = fileRepository.createLoader( filePromise );
|
|
|
+
|
|
|
+ const promise = loader.upload().catch( e => {
|
|
|
+ expect( e ).to.equal( 'File loading error' );
|
|
|
+ expect( loader.status ).to.equal( 'error' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ rejectFile( 'File loading error' );
|
|
|
+
|
|
|
+ return promise;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should reject promise when uploading is aborted (before file promise is resolved)', () => {
|
|
|
const promise = loader.upload().catch( e => {
|
|
|
expect( e ).to.equal( 'aborted' );
|
|
|
expect( loader.status ).to.equal( 'aborted' );
|
|
|
@@ -417,25 +613,36 @@ describe( 'FileRepository', () => {
|
|
|
return promise;
|
|
|
} );
|
|
|
|
|
|
- it( 'should reject promise on reading error', () => {
|
|
|
+ it( 'should reject promise when uploading is aborted (after file promise is resolved)', () => {
|
|
|
+ const promise = loader.upload().catch( e => {
|
|
|
+ expect( e ).to.equal( 'aborted' );
|
|
|
+ expect( loader.status ).to.equal( 'aborted' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ loader.file.then( () => loader.abort() );
|
|
|
+
|
|
|
+ return promise;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should reject promise on reading error (after file promise is resolved)', () => {
|
|
|
const promise = loader.upload().catch( e => {
|
|
|
expect( e ).to.equal( 'uploading error' );
|
|
|
expect( loader.status ).to.equal( 'error' );
|
|
|
} );
|
|
|
|
|
|
- adapterMock.mockError( 'uploading error' );
|
|
|
+ loader.file.then( () => adapterMock.mockError( 'uploading error' ) );
|
|
|
|
|
|
return promise;
|
|
|
} );
|
|
|
|
|
|
- it( 'should resolve promise on reading complete', () => {
|
|
|
+ it( 'should resolve promise on reading complete (after file promise is resolved)', () => {
|
|
|
const promise = loader.upload()
|
|
|
.then( data => {
|
|
|
expect( data ).to.equal( 'result data' );
|
|
|
expect( loader.status ).to.equal( 'idle' );
|
|
|
} );
|
|
|
|
|
|
- adapterMock.mockSuccess( 'result data' );
|
|
|
+ loader.file.then( () => adapterMock.mockSuccess( 'result data' ) );
|
|
|
|
|
|
return promise;
|
|
|
} );
|
|
|
@@ -447,18 +654,74 @@ describe( 'FileRepository', () => {
|
|
|
expect( loader.status ).to.equal( 'idle' );
|
|
|
} );
|
|
|
|
|
|
- expect( loader.uploaded ).to.equal( 0 );
|
|
|
- expect( loader.uploadTotal ).to.be.null;
|
|
|
+ loader.file.then( () => {
|
|
|
+ expect( loader.uploaded ).to.equal( 0 );
|
|
|
+ expect( loader.uploadTotal ).to.be.null;
|
|
|
+
|
|
|
+ adapterMock.mockProgress( 1, 10 );
|
|
|
+ expect( loader.uploaded ).to.equal( 1 );
|
|
|
+ expect( loader.uploadTotal ).to.equal( 10 );
|
|
|
+
|
|
|
+ adapterMock.mockProgress( 6, 10 );
|
|
|
+ expect( loader.uploaded ).to.equal( 6 );
|
|
|
+ expect( loader.uploadTotal ).to.equal( 10 );
|
|
|
+
|
|
|
+ adapterMock.mockSuccess( 'result data' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ return promise;
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+
|
|
|
+ describe( 'abort()', () => {
|
|
|
+ let filePromiseRejecterSpy, readerAbortSpy, adapterAbortSpy;
|
|
|
+
|
|
|
+ beforeEach( () => {
|
|
|
+ filePromiseRejecterSpy = testUtils.sinon.spy( loader._filePromiseWrapper, 'rejecter' );
|
|
|
+ readerAbortSpy = testUtils.sinon.spy( loader._reader, 'abort' );
|
|
|
+ adapterAbortSpy = testUtils.sinon.spy( loader._adapter, 'abort' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should abort correctly before read/upload is called', () => {
|
|
|
+ loader.abort();
|
|
|
+
|
|
|
+ expect( filePromiseRejecterSpy.callCount ).to.equal( 1 );
|
|
|
+ expect( readerAbortSpy.callCount ).to.equal( 0 );
|
|
|
+ expect( adapterAbortSpy.callCount ).to.equal( 0 );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should abort correctly after successful read', () => {
|
|
|
+ const promise = loader.read()
|
|
|
+ .then( data => {
|
|
|
+ expect( data ).to.equal( 'result data' );
|
|
|
+ expect( loader.status ).to.equal( 'idle' );
|
|
|
+
|
|
|
+ loader.abort();
|
|
|
|
|
|
- adapterMock.mockProgress( 1, 10 );
|
|
|
- expect( loader.uploaded ).to.equal( 1 );
|
|
|
- expect( loader.uploadTotal ).to.equal( 10 );
|
|
|
+ expect( filePromiseRejecterSpy.callCount ).to.equal( 0 );
|
|
|
+ expect( readerAbortSpy.callCount ).to.equal( 0 );
|
|
|
+ expect( adapterAbortSpy.callCount ).to.equal( 0 );
|
|
|
+ } );
|
|
|
|
|
|
- adapterMock.mockProgress( 6, 10 );
|
|
|
- expect( loader.uploaded ).to.equal( 6 );
|
|
|
- expect( loader.uploadTotal ).to.equal( 10 );
|
|
|
+ loader.file.then( () => nativeReaderMock.mockSuccess( 'result data' ) );
|
|
|
+
|
|
|
+ return promise;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should abort correctly after successful upload', () => {
|
|
|
+ const promise = loader.upload()
|
|
|
+ .then( data => {
|
|
|
+ expect( data ).to.equal( 'result data' );
|
|
|
+ expect( loader.status ).to.equal( 'idle' );
|
|
|
+
|
|
|
+ loader.abort();
|
|
|
+
|
|
|
+ expect( filePromiseRejecterSpy.callCount ).to.equal( 0 );
|
|
|
+ expect( readerAbortSpy.callCount ).to.equal( 0 );
|
|
|
+ expect( adapterAbortSpy.callCount ).to.equal( 0 );
|
|
|
+ } );
|
|
|
|
|
|
- adapterMock.mockSuccess( 'result data' );
|
|
|
+ loader.file.then( () => adapterMock.mockSuccess( 'result data' ) );
|
|
|
|
|
|
return promise;
|
|
|
} );
|