Skip to content

Commit 31fa2dc

Browse files
committed
Added Test cases and increased coverage
1 parent f261504 commit 31fa2dc

File tree

7 files changed

+100
-27
lines changed

7 files changed

+100
-27
lines changed

package-lock.json

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"rollup-plugin-terser": "^6.1.0",
3434
"sinon": "^8.1.1",
3535
"ts-node": "^10.0.0",
36-
"typescript": "^4.3.2"
36+
"typescript": "^4.3.2",
37+
"web-file-polyfill": "^1.0.1"
3738
},
3839
"scripts": {
3940
"dev": "rollup -c -w",

src/upload/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ export const upload = (
3636
if (typeof param === "string" || typeof param === "boolean") {
3737
formData.append(i, String(param));
3838
} else if (param instanceof Buffer) {
39-
formData.append(i, new Blob([param]));
39+
formData.append(i, new Blob([param]), uploadOptions.fileName);
4040
} else {
4141
formData.append(i, param);
4242
}
4343
}
4444
}
4545

46-
formData.append("publicKey", options.publicKey || "");
46+
formData.append("publicKey", options.publicKey);
4747

4848
request(formData, { ...options, authenticationEndpoint: options.authenticationEndpoint }, callback);
4949
};

src/utils/request.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,17 @@ interface SignatureResponse {
1111
export const request = (formData: FormData, options: ImageKitOptions & { authenticationEndpoint: string }, callback?: (err: Error | null, response: UploadResponse | null) => void) => {
1212
generateSignatureToken(options, (err, signaturObj) => {
1313
if (err) {
14-
if (typeof callback != "function") return;
15-
callback(err, null);
16-
return;
14+
return respond(true, err, callback)
1715
} else {
1816
formData.append("signature", signaturObj?.signature || "");
1917
formData.append("expire", String(signaturObj?.expire || 0));
2018
formData.append("token", signaturObj?.token || "");
2119

2220
uploadFile(formData, (err, responseSucessText) => {
23-
if (typeof callback != "function") return;
2421
if (err) {
25-
callback(err, null);
26-
} else {
27-
callback(null, responseSucessText!);
22+
return respond(true, err, callback)
2823
}
24+
return respond(false, responseSucessText!, callback)
2925
});
3026
}
3127
});
@@ -36,13 +32,10 @@ export const generateSignatureToken = (options: ImageKitOptions & { authenticati
3632
xhr.timeout = 60000;
3733
xhr.open('GET', options.authenticationEndpoint);
3834
xhr.ontimeout = function (e) {
39-
if (typeof callback != "function") return;
4035
respond(true, errorMessages.AUTH_ENDPOINT_TIMEOUT, callback);
41-
return;
4236
};
4337
xhr.onerror = function() {
4438
respond(true, errorMessages.AUTH_ENDPOINT_NETWORK_ERROR, callback);
45-
return;
4639
}
4740
xhr.onload = function () {
4841
if (xhr.status === 200) {
@@ -53,20 +46,16 @@ export const generateSignatureToken = (options: ImageKitOptions & { authenticati
5346
expire: body.expire,
5447
token: body.token
5548
}
56-
callback(null, obj);
57-
return;
49+
respond(false, obj, callback)
5850
} catch (ex) {
59-
if (typeof callback != "function") return;
60-
callback(ex, null);
51+
respond(true, ex, callback)
6152
}
6253
} else {
6354
try {
6455
var error = JSON.parse(xhr.responseText);
65-
if (typeof callback != "function") return;
66-
callback(error, null);
56+
respond(true, error, callback);
6757
} catch (ex) {
68-
if (typeof callback != "function") return;
69-
callback(ex, null);
58+
respond(true, ex, callback);
7059
}
7160
}
7261
};
@@ -83,12 +72,10 @@ export const uploadFile = (formData: FormData, callback: (err: Error | null, res
8372
}
8473
uploadFileXHR.onload = function () {
8574
if (uploadFileXHR.status === 200) {
86-
if (typeof callback != "function") return;
8775
var uploadResponse = JSON.parse(uploadFileXHR.responseText);
8876
callback(null, uploadResponse);
8977
}
9078
else if (uploadFileXHR.status !== 200) {
91-
if (typeof callback != "function") return;
9279
try {
9380
callback(JSON.parse(uploadFileXHR.responseText), null);
9481
} catch (ex) {

test/upload.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const chai = require("chai");
22
const sinon = require("sinon");
3-
global.FormData = require('formdata-node');
3+
global.FormData = require("formdata-node");
4+
global.Blob = require("web-file-polyfill").Blob
5+
global.File = require("web-file-polyfill").File
46
const expect = chai.expect;
57
const initializationParams = require("./data").initializationParams
68
import ImageKit from "../src/index";
@@ -337,7 +339,8 @@ describe("File upload", function () {
337339
it('Bare minimum request', function () {
338340
const fileOptions = {
339341
fileName: "test_file_name",
340-
file: "test_file"
342+
file: "test_file",
343+
tags: undefined
341344
};
342345

343346
var callback = sinon.spy();
@@ -366,6 +369,39 @@ describe("File upload", function () {
366369
sinon.assert.calledWith(callback, null, uploadSuccessResponseObj);
367370
});
368371

372+
it('Bare minimum request: Blob', function () {
373+
const buffer = Buffer.from("test_buffer")
374+
const fileOptions = {
375+
fileName: "test_file_name",
376+
file: buffer
377+
};
378+
379+
var callback = sinon.spy();
380+
381+
imagekit.upload(fileOptions, callback);
382+
383+
expect(server.requests.length).to.be.equal(1);
384+
successSignature();
385+
expect(server.requests.length).to.be.equal(2);
386+
successUploadResponse();
387+
388+
var arg = server.requests[1].requestBody;
389+
expect(arg.get('file').size).to.be.eq(buffer.length);
390+
expect(arg.get('fileName')).to.be.equal("test_file_name");
391+
expect(arg.get('token')).to.be.equal("test_token");
392+
expect(arg.get('expire')).to.be.equal("123");
393+
expect(arg.get('signature')).to.be.equal("test_signature");
394+
expect(arg.get('publicKey')).to.be.equal('test_public_key');
395+
expect(arg.get('tags')).to.be.equal(undefined);
396+
expect(arg.get('isPrivateFile')).to.be.equal(undefined);
397+
expect(arg.get('useUniqueFileName')).to.be.equal(undefined);
398+
expect(arg.get('customCoordinates')).to.be.equal(undefined);
399+
expect(arg.get('responseFields')).to.be.equal(undefined);
400+
401+
expect(callback.calledOnce).to.be.true;
402+
sinon.assert.calledWith(callback, null, uploadSuccessResponseObj);
403+
});
404+
369405
it('Error during upload', function () {
370406
const fileOptions = {
371407
fileName: "test_file_name",

test/url-generation.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ describe("URL generation", function () {
4848
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/test_path_alt.jpg?ik-sdk-version=javascript-${pkg.version}`);
4949
});
5050

51+
it('should generate the url without sdk-version', function () {
52+
const ik = new ImageKit({...initializationParams, sdkVersion: ""})
53+
54+
const url = ik.url({
55+
path: "/test_path.jpg",
56+
transformation: [{
57+
"height": "300",
58+
"width": "400"
59+
}]
60+
});
61+
62+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/tr:h-300,w-400/test_path.jpg`);
63+
});
64+
5165
it('should generate the correct url with path param', function () {
5266
const url = imagekit.url({
5367
path: "/test_path.jpg",
@@ -245,7 +259,18 @@ describe("URL generation", function () {
245259
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/tr:undefined-transform-true/test_path.jpg?ik-sdk-version=javascript-${pkg.version}`);
246260
});
247261

248-
it('transformation with empty key and value', function () {
262+
it('transformation with empty value', function () {
263+
const url = imagekit.url({
264+
path: "/test_path.jpg",
265+
transformation: [{
266+
overlayImage: ""
267+
}]
268+
})
269+
270+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/tr:oi-/test_path.jpg?ik-sdk-version=javascript-${pkg.version}`);
271+
});
272+
273+
it('transformation with - value', function () {
249274
const url = imagekit.url({
250275
path: "/test_path.jpg",
251276
transformation: [{

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
1515
// "sourceMap": true, /* Generates corresponding '.map' file. */
1616
// "outFile": "./", /* Concatenate and emit output to single file. */
17-
"outDir": "./out-tsc", /* Redirect output structure to the directory. */
17+
// "outDir": "./out-tsc", /* Redirect output structure to the directory. */
1818
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
1919
// "composite": true, /* Enable project compilation */
2020
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */

0 commit comments

Comments
 (0)