diff --git a/package.json b/package.json index 7b9f993a..619c8aaf 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ "@commitlint/cli": "^13.1.0", "@commitlint/config-conventional": "^13.1.0", "@types/cpx": "^1.5.2", - "@types/jasmine": "^3.9.1", + "@types/jasmine": "~3.10.1", "@types/jasminewd2": "~2.0.10", "@types/node": "12.12.41", "@uploadx/core": "^4.4.2", @@ -65,9 +65,9 @@ "core-js": "2.6.11", "cpx": "^1.5.0", "husky": "^7.0.2", - "jasmine-core": "^3.9.0", + "jasmine-core": "~3.10.1", "jasmine-spec-reporter": "^7.0.0", - "karma": "^5.2.3", + "karma": "~6.3.5", "karma-chrome-launcher": "~3.1.0", "karma-coverage-istanbul-reporter": "~3.0.3", "karma-jasmine": "~4.0.1", diff --git a/src/uploadx/lib/tus.spec.ts b/src/uploadx/lib/tus.spec.ts index a62df1c6..3c029ac5 100644 --- a/src/uploadx/lib/tus.spec.ts +++ b/src/uploadx/lib/tus.spec.ts @@ -4,35 +4,38 @@ import { Tus } from './tus'; // tslint:disable: no-any const fileWithType = new File(['123456'], 'filename.txt', { type: 'text/plain' }); describe('getFileUrl', () => { - let upx: Tus; + let uploader: Tus; let req: jasmine.Spy; let getValueFromResponse: jasmine.Spy; it('should set headers', async () => { - upx = new Tus(fileWithType, {}, () => {}, {} as Ajax); - req = spyOn(upx, 'request').and.callFake(({ headers }: any) => { + uploader = new Tus(fileWithType, {}, () => {}, {} as Ajax); + req = spyOn(uploader, 'request').and.callFake(({ headers }: any) => { expect(headers['Upload-Metadata']).toContain('name'); expect(headers['Upload-Length']).toEqual('6'); }); - getValueFromResponse = spyOn(upx, 'getValueFromResponse').and.returnValue('/12345678'); - expect(upx.name).toEqual('filename.txt'); - expect(upx.size).toEqual(6); - expect(await upx.getFileUrl()).toEqual('/12345678'); + getValueFromResponse = spyOn(uploader, 'getValueFromResponse').and.returnValue( + '/12345678' + ); + expect(uploader.name).toEqual('filename.txt'); + expect(uploader.size).toEqual(6); + expect(await uploader.getFileUrl()).toEqual('/12345678'); expect(req).toHaveBeenCalled(); expect(getValueFromResponse).toHaveBeenCalled(); }); }); describe('sendFileContent', () => { - let upx: Tus; + let uploader: Tus; let req: jasmine.Spy; let getOffsetFromResponse: jasmine.Spy; it('should set Upload-Offset header', async () => { - upx = new Tus(fileWithType, {}, () => {}, {} as Ajax); - req = spyOn(upx, 'request').and.callFake(({ headers }: any) => { + uploader = new Tus(fileWithType, {}, () => {}, {} as Ajax); + uploader.offset = 0; + req = spyOn(uploader, 'request').and.callFake(({ headers }: any) => { expect(headers['Content-Type']).toEqual('application/offset+octet-stream'); expect(headers['Upload-Offset']).toEqual('0'); }); - getOffsetFromResponse = spyOn(upx, 'getOffsetFromResponse').and.returnValue(6); - expect(await upx.sendFileContent()).toEqual(6); + getOffsetFromResponse = spyOn(uploader, 'getOffsetFromResponse').and.returnValue(6); + expect(await uploader.sendFileContent()).toEqual(6); expect(req).toHaveBeenCalled(); expect(getOffsetFromResponse).toHaveBeenCalled(); }); diff --git a/src/uploadx/lib/uploader.spec.ts b/src/uploadx/lib/uploader.spec.ts index 4a7ad3c9..5f9e9507 100644 --- a/src/uploadx/lib/uploader.spec.ts +++ b/src/uploadx/lib/uploader.spec.ts @@ -133,11 +133,13 @@ describe('Uploader', () => { const updateToken = spyOn(uploader, 'updateToken').and.callThrough(); const getFileUrl = spyOn(uploader, 'getFileUrl').and.callThrough(); const getOffset = spyOn(uploader, 'getOffset').and.callThrough(); + const sendFileContent = spyOn(uploader, 'sendFileContent').and.callThrough(); const cleanup = spyOn(uploader, 'cleanup').and.callThrough(); await uploader.upload(); expect(updateToken).toHaveBeenCalledTimes(1); expect(getFileUrl).toHaveBeenCalledTimes(1); - expect(getOffset).toHaveBeenCalledTimes(0); + expect(getOffset).toHaveBeenCalledTimes(1); + expect(sendFileContent).toHaveBeenCalledTimes(1); expect(cleanup).toHaveBeenCalled(); expect(uploader.status).toEqual('complete'); }); diff --git a/src/uploadx/lib/uploader.ts b/src/uploadx/lib/uploader.ts index e5476083..590dd750 100644 --- a/src/uploadx/lib/uploader.ts +++ b/src/uploadx/lib/uploader.ts @@ -48,7 +48,7 @@ export abstract class Uploader implements UploadState { /** Auth token/tokenGetter */ token: UploadxControlEvent['token']; /** Byte offset within the whole file */ - offset? = 0; + offset?: number; /** Retries handler */ retry: RetryHandler; canceler = new Canceler(); diff --git a/src/uploadx/lib/uploaderx.spec.ts b/src/uploadx/lib/uploaderx.spec.ts index 4d2d1fa3..7bcab975 100644 --- a/src/uploadx/lib/uploaderx.spec.ts +++ b/src/uploadx/lib/uploaderx.spec.ts @@ -6,52 +6,57 @@ const fileWithType = new File(['123456'], 'filename.txt', { type: 'text/plain' } const fileWithoutType = new File([''], 'filename'); describe('getFileUrl', () => { - let upx: UploaderX; + let uploader: UploaderX; let req: jasmine.Spy; let getValueFromResponse: jasmine.Spy; it('should set headers', async () => { - upx = new UploaderX(fileWithType, {}, () => {}, {} as Ajax); - req = spyOn(upx, 'request').and.callFake(({ headers }: any) => { + uploader = new UploaderX(fileWithType, {}, () => {}, {} as Ajax); + req = spyOn(uploader, 'request').and.callFake(({ headers }: any) => { expect(headers['X-Upload-Content-Type']).toEqual('text/plain'); expect(headers['X-Upload-Content-Length']).toEqual('6'); }); - getValueFromResponse = spyOn(upx, 'getValueFromResponse').and.returnValue('/12345678'); - expect(upx.name).toEqual('filename.txt'); - expect(upx.size).toEqual(6); - expect(await upx.getFileUrl()).toEqual('/12345678'); + getValueFromResponse = spyOn(uploader, 'getValueFromResponse').and.returnValue( + '/12345678' + ); + expect(uploader.name).toEqual('filename.txt'); + expect(uploader.size).toEqual(6); + expect(await uploader.getFileUrl()).toEqual('/12345678'); expect(req).toHaveBeenCalled(); expect(getValueFromResponse).toHaveBeenCalled(); }); it('should set default type header', async () => { - upx = new UploaderX(fileWithoutType, {}, () => {}, {} as Ajax); - req = spyOn(upx, 'request').and.callFake(({ headers }: any) => { + uploader = new UploaderX(fileWithoutType, {}, () => {}, {} as Ajax); + req = spyOn(uploader, 'request').and.callFake(({ headers }: any) => { expect(headers['X-Upload-Content-Type']).toEqual('application/octet-stream'); expect(headers['X-Upload-Content-Length']).toEqual('0'); }); - getValueFromResponse = spyOn(upx, 'getValueFromResponse').and.returnValue('/12345678'); - expect(upx.name).toEqual('filename'); - expect(upx.size).toEqual(0); - await upx.getFileUrl(); + getValueFromResponse = spyOn(uploader, 'getValueFromResponse').and.returnValue( + '/12345678' + ); + expect(uploader.name).toEqual('filename'); + expect(uploader.size).toEqual(0); + await uploader.getFileUrl(); expect(req).toHaveBeenCalled(); expect(getValueFromResponse).toHaveBeenCalled(); }); }); describe('sendFileContent', () => { - let upx: UploaderX; + let uploaderX: UploaderX; let req: jasmine.Spy; let getValueFromResponse: jasmine.Spy; it('should set Content-Range header', async () => { - upx = new UploaderX(fileWithType, {}, () => {}, {} as Ajax); - req = spyOn(upx, 'request').and.callFake(({ headers }: any) => { + uploaderX = new UploaderX(fileWithType, {}, () => {}, {} as Ajax); + uploaderX.offset = 0; + req = spyOn(uploaderX, 'request').and.callFake(({ headers }: any) => { expect(headers['Content-Type']).toEqual('application/octet-stream'); expect(headers['Content-Range']).toEqual('bytes 0-5/6'); }); - getValueFromResponse = spyOn(upx, 'getValueFromResponse').and.returnValue( + getValueFromResponse = spyOn(uploaderX, 'getValueFromResponse').and.returnValue( 'Range: bytes=0-5' ); - upx.responseStatus = 308; - expect(await upx.sendFileContent()).toEqual(6); + uploaderX.responseStatus = 308; + expect(await uploaderX.sendFileContent()).toEqual(6); expect(req).toHaveBeenCalled(); expect(getValueFromResponse).toHaveBeenCalled(); });