Skip to content

Commit 27cbbae

Browse files
author
Jagveer
committed
Teest Cases, sample App and docs update
1 parent a00c062 commit 27cbbae

File tree

4 files changed

+243
-27
lines changed

4 files changed

+243
-27
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,21 @@ If the upload succeed, `err` will be `null` and the `result` will be the same as
241241
If the upload fails, `err` will be the same as what is received from ImageKit's servers and the `result` will be null.
242242

243243

244+
### Demo Application
245+
246+
The fastest way to get started is running the demo application. You can run the code locally. The source code is in [samples/sample-app](https://github.com/imagekit-developer/imagekit-javascript/tree/master/samples/sample-app).
247+
248+
To run it:
249+
250+
```
251+
git clone https://github.com/imagekit-developer/imagekit-javascript.git
252+
253+
cd imagekit-javascript
254+
```
255+
256+
Find the file `.env` in the directory `samples/sample-app` and fill in your `PRIVATE_KEY`, `PUBLIC_KEY` and `URL_ENDPOINT` from your [imageKit dashboard](https://imagekit.io/dashboard#developers). The just run:
257+
258+
```
259+
yarn startSampleApp
260+
```
261+

samples/sample-app/views/index.pug

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ html
2626
urlEndpoint: "!{urlEndpoint}",
2727
authenticationEndpoint: "!{authenticationEndpoint}"
2828
});
29+
30+
window.imagekit = imageekit;
2931

3032
function upload(e) {
3133
e.preventDefault();

test/test.js

Lines changed: 223 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
const chai = require("chai");
22
const sinon = require("sinon");
3+
const requestUtils = require('../utils/request.js');
4+
const fs = require('fs');
5+
const path = require('path');
36

4-
const expect = chai.expect;
7+
global.FormData = require('formdata-node');
58

9+
const expect = chai.expect;
10+
const test_file_path = path.resolve(__dirname, "test_image.png");
611
opts = {
712
publicKey: "test_public_key",
813
urlEndpoint: "https://ik.imagekit.io/test_url_endpoint",
@@ -42,66 +47,257 @@ describe("Testing SDK", function () {
4247
"height" : "300",
4348
"width" : "400"
4449
}]
50+
});
51+
52+
expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint\/[^\/]/);
53+
expect(url).to.match(new RegExp("[^\/]\/tr:"));
54+
expect(url).to.match(new RegExp(":h-300,w-400"));
55+
expect(url).to.match(/[^\/]\/test_path\.jpg[^\/][\?]?/);
56+
expect(url).to.match(/ik\-sdk\-version\=javascript\-\d\.\d\.\d/);
57+
});
58+
59+
it('should generate the correct url with path param with multiple leading slash', function() {
60+
const url = imagekit.url({
61+
path: "///test_path.jpg",
62+
transformation : [{
63+
"height" : "300",
64+
"width" : "400"
65+
}]
4566
})
46-
expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint/);
47-
expect(url).to.match(new RegExp("\/tr:"));
48-
expect(url).to.match(new RegExp("w-400"));
49-
expect(url).to.match(new RegExp("h-300"));
50-
expect(url).to.match(/\/test_path\.jpg[^\/][\?]?/);
67+
expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint\/[^\/]/);
68+
expect(url).to.match(new RegExp("[^\/]\/tr:"));
69+
expect(url).to.match(new RegExp(":h-300,w-400"));
70+
expect(url).to.match(/[^\/]\/test_path\.jpg[^\/][\?]?/);
71+
expect(url).to.match(/ik\-sdk\-version\=javascript\-\d\.\d\.\d/);
72+
73+
});
74+
75+
it('should generate the correct url with path param with overidden urlEndpoint', function() {
76+
const url = imagekit.url({
77+
urlEndpoint: "https://ik.imagekit.io/test_url_endpoint_alt",
78+
path: "/test_path.jpg",
79+
transformation : [{
80+
"height" : "300",
81+
"width" : "400"
82+
}]
83+
})
84+
expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint\_alt\/[^\/]/);
85+
expect(url).to.match(new RegExp("[^\/]\/tr:"));
86+
expect(url).to.match(new RegExp(":h-300,w-400"));
87+
expect(url).to.match(/[^\/]\/test_path\.jpg[^\/][\?]?/);
88+
expect(url).to.match(/ik\-sdk\-version\=javascript\-\d\.\d\.\d/);
89+
90+
});
91+
92+
it('should generate the correct url with path param with transformationPostion as query', function() {
93+
const url = imagekit.url({
94+
path: "/test_path.jpg",
95+
transformationPosition: "query",
96+
transformation : [{
97+
"height" : "300",
98+
"width" : "400"
99+
}]
100+
});
101+
expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint\/test\_path\.jpg\?/);
102+
expect(url).to.match(new RegExp('&tr=h-300%2Cw-400'));
103+
expect(url).to.match(/ik\-sdk\-version\=javascript\-\d\.\d\.\d/);
51104
});
52105

53106
it('should generate the correct url with src param', function() {
54107
const url = imagekit.url({
55-
src : "https://ik.imagekit.io/test_url_endpoint/endpoint/test_path.jpg",
108+
src : "https://ik.imagekit.io/test_url_endpoint/test_path_alt.jpg",
56109
transformation : [{
57110
"height" : "300",
58111
"width" : "400"
59112
}]
60113
});
61-
expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test_url_endpoint\/endpoint\/test\_path\.jpg/);
62-
expect(url).to.match(/\&tr=/);
63-
expect(url).to.match(new RegExp("w-400"));
64-
expect(url).to.match(new RegExp("h-300"));
114+
expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint\/test\_path\_alt\.jpg[^\/]/);
115+
expect(url).to.match(new RegExp('&tr=h-300%2Cw-400'));
116+
expect(url).to.match(/ik\-sdk\-version\=javascript\-\d\.\d\.\d/);
65117
});
66-
});
67118

68-
describe(".upload method", function() {
69-
const requestUtils = require('../utils/request.js');
70-
sinon.stub(requestUtils, 'generateSignatureToken').callsFake(function (options, callback) {
71-
callback(null, {
72-
signature: "test_signature",
73-
expire: 123,
74-
token: "test_token"
119+
it('should generate the correct url with transformationPostion as query', function() {
120+
const url = imagekit.url({
121+
src : "https://ik.imagekit.io/test_url_endpoint/test_path_alt.jpg",
122+
transformationPosition: "query",
123+
transformation : [{
124+
"height" : "300",
125+
"width" : "400"
126+
}]
127+
});
128+
expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint\/test\_path\_alt\.jpg\?/);
129+
expect(url).to.match(new RegExp('&tr=h-300%2Cw-400'));
130+
expect(url).to.match(/ik\-sdk\-version\=javascript\-\d\.\d\.\d/);
131+
});
132+
133+
it('should generate the correct url with query params properly merged', function() {
134+
const url = imagekit.url({
135+
src : "https://ik.imagekit.io/test_url_endpoint/test_path_alt.jpg?t1=v1",
136+
queryParameters: {t2: "v2", t3: "v3"},
137+
transformation : [{
138+
"height" : "300",
139+
"width" : "400"
140+
}]
141+
});
142+
143+
expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint\/test\_path\_alt\.jpg\?t1=v1\&/);
144+
expect(url).to.match(new RegExp('&t2=v2&'));
145+
expect(url).to.match(new RegExp('&t3=v3&'));
146+
expect(url).to.match(new RegExp('&tr=h-300%2Cw-400'));
147+
expect(url).to.match(/ik\-sdk\-version\=javascript\-\d\.\d\.\d/);
148+
});
149+
150+
151+
it('should generate the correct chained transformation', function() {
152+
const url = imagekit.url({
153+
path: "/test_path.jpg",
154+
transformation : [{
155+
"height" : "300",
156+
"width" : "400"
157+
}, {
158+
"rt": "90"
159+
}]
75160
})
161+
162+
expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint\/[^\/]/);
163+
expect(url).to.match(new RegExp("[^\/]\/tr:"));
164+
expect(url).to.match(new RegExp(":h-300,w-400:rt-90"));
165+
expect(url).to.not.match(new RegExp(":rt-90:h-300,w-400"));
166+
expect(url).to.match(/[^\/]\/test_path\.jpg[^\/][\?]?/);
167+
expect(url).to.match(/ik\-sdk\-version\=javascript\-\d\.\d\.\d/);
76168
});
77169

78-
sinon.stub(requestUtils, 'uploadFile').callsFake(function (options, callback) {
79-
callback(null, "success")
170+
171+
it('should generate the correct chained transformation url with new undocumented tranforamtion parameter', function() {
172+
const url = imagekit.url({
173+
path: "/test_path.jpg",
174+
transformation : [{
175+
"height" : "300",
176+
"width" : "400"
177+
}, {
178+
"rndm_trnsf": "abcd"
179+
}]
180+
})
181+
182+
expect(url).to.match(/^https\:\/\/ik\.imagekit\.io\/test\_url\_endpoint\/[^\/]/);
183+
expect(url).to.match(new RegExp("[^\/]\/tr:"));
184+
expect(url).to.match(new RegExp(":h-300,w-400:rndm_trnsf-abcd"));
185+
expect(url).to.not.match(new RegExp(":rndm_trnsf-abcd:h-300,w-400"));
186+
expect(url).to.match(/[^\/]\/test_path\.jpg[^\/][\?]?/);
187+
expect(url).to.match(/ik\-sdk\-version\=javascript\-\d\.\d\.\d/);
80188
});
189+
190+
});
191+
192+
describe(".upload method", function() {
193+
let orderCop;
81194

82-
global.FormData = require('formdata-node');
195+
before(function() {
196+
orderCop = sinon.spy();
197+
sinon.stub(requestUtils, 'generateSignatureToken').callsFake(function (options, callback) {
198+
orderCop('signatureCall');
199+
callback(null, {
200+
signature: "test_signature",
201+
expire: 123,
202+
token: "test_token"
203+
});
204+
});
205+
sinon.stub(requestUtils, 'uploadFile').callsFake(function (options, callback) {
206+
orderCop('uploadCall');
207+
callback(null, "success");
208+
});
83209

210+
});
84211

85-
const fileOptions = {
86-
fileName: "test_file_name",
87-
file: "test_file",
88-
};
212+
beforeEach(function() {
213+
orderCop.resetHistory();
214+
requestUtils.generateSignatureToken.resetHistory();
215+
requestUtils.uploadFile.resetHistory();
216+
})
89217

90218
it('calls relavant functions in order', function() {
219+
const fileOptions = {
220+
fileName: "test_file_name",
221+
file: "test_file",
222+
};
91223
imagekit.upload(fileOptions, function(){});
224+
92225
expect(requestUtils.generateSignatureToken.calledOnce).to.be.true;
93226
expect(requestUtils.uploadFile.calledOnce).to.be.true;
227+
expect(orderCop.firstCall.args[0]).to.be.equal('signatureCall');
228+
expect(orderCop.secondCall.args[0]).to.be.equal('uploadCall');
94229
});
95230

96231
it('calls functions with correct arguments', function() {
232+
233+
const fileOptions = {
234+
fileName: "test_file_name",
235+
file: "test_file",
236+
tags : ["test_tag1", "test_tag2"],
237+
customCoordinates: "test_customCoordinates",
238+
responseFields: "tags, customCoordinates, isPrivateFile, metadata",
239+
useUniqueFileName: true
240+
};
241+
imagekit.upload(fileOptions, function(){});
242+
97243
const arg = requestUtils.uploadFile.getCall(0).args[0];
98244
expect(arg.get('file')).to.be.equal("test_file");
99245
expect(arg.get('fileName')).to.be.equal("test_file_name");
100246
expect(arg.get('token')).to.be.equal("test_token");
101247
expect(arg.get('expire')).to.be.equal("123");
102248
expect(arg.get('signature')).to.be.equal("test_signature");
103-
});
249+
expect(arg.get('tags')).to.match(new RegExp("test_tag1"));
250+
expect(arg.get('tags')).to.match(new RegExp("test_tag2"));
251+
expect(arg.get('customCoordinates')).to.match(new RegExp("test_customCoordinates"));
252+
expect(arg.get('responseFields')).to.match(new RegExp("tags, customCoordinates, isPrivateFile, metadata"));
253+
expect(arg.get('useUniqueFileName')).to.be.equal('true');
254+
});
104255

256+
it('calls functions with correct arguments for missing parameters', function() {
257+
const fileOptions = {
258+
fileName: "test_file_name",
259+
file: "test_file",
260+
};
261+
imagekit.upload(fileOptions, function(){});
262+
263+
const arg = requestUtils.uploadFile.getCall(0).args[0];
264+
expect(arg.get('file')).to.be.equal("test_file");
265+
expect(arg.get('fileName')).to.be.equal("test_file_name");
266+
expect(arg.get('tags')).to.not.equal("undefined");
267+
expect(arg.get('customCoordinates')).to.be.equal(undefined);
268+
expect(arg.get('customCoordinates')).to.not.equal("undefined");
269+
expect(arg.get('responseFields')).to.be.equal(undefined);
270+
expect(arg.get('responseFields')).to.not.equal("undefined");
271+
expect(arg.get('useUniqueFileName')).to.be.equal(undefined);
272+
expect(arg.get('useUniqueFileName')).to.not.equal("undefined");
273+
});
274+
275+
it('calls functions with base64 file argument', function() {
276+
277+
const fileOptions = {
278+
fileName: "test_file_name",
279+
file: fs.readFileSync(test_file_path, 'base64'),
280+
};
281+
imagekit.upload(fileOptions, function(){});
282+
283+
const arg = requestUtils.uploadFile.getCall(0).args[0];
284+
expect(arg.get('file')).to.match(/^[a-zA-Z0-9\+/]*={0,2}$/);
285+
expect(arg.get('fileName')).to.be.equal("test_file_name");
286+
});
287+
288+
it('calls functions with url file argument', function() {
289+
290+
291+
const fileOptions = {
292+
fileName: "test_file_name",
293+
file: "http://test.url",
294+
};
295+
imagekit.upload(fileOptions, function(){});
296+
297+
const arg = requestUtils.uploadFile.getCall(0).args[0];
298+
expect(arg.get('file')).to.be.equal("http://test.url");
299+
expect(arg.get('fileName')).to.be.equal("test_file_name");
300+
});
105301
});
106302
});
107303
});

test/test_image.png

4.28 KB
Loading

0 commit comments

Comments
 (0)