Skip to content

Commit 4deafc3

Browse files
committed
fix: added test cases for missing scenarios, fixes #1
1 parent 9590999 commit 4deafc3

File tree

4 files changed

+312
-63
lines changed

4 files changed

+312
-63
lines changed

sdk/lib/src/imagekitio-angular/ik-upload/ik-upload.component.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,26 @@ export class IkUploadComponent implements OnInit {
2424
}
2525

2626
handleFileInput(e) {
27+
const onError = this.onError;
28+
const onSuccess = this.onSuccess;
2729
const files = e.target.files;
2830
this.fileToUpload = files.item(0);
2931
if (this.onFileInput) {
3032
this.onFileInput(e);
3133
return;
3234
}
33-
this.upload(this.fileToUpload, this.fileName, this.useUniqueFileName, this.tags, this.folder, this.isPrivateFile, this.customCoordinates, this.responseFields)
35+
const params = this.getUploadParams(this.fileToUpload, this.fileName, this.useUniqueFileName, this.tags, this.folder, this.isPrivateFile, this.customCoordinates, this.responseFields)
36+
const ik = this.imagekit.ikInstance;
37+
ik.upload(params, function (err, result) {
38+
if (err) {
39+
onError.emit(err);
40+
} else {
41+
onSuccess.emit(result);
42+
}
43+
});
3444
}
3545

36-
upload(file, fileName, useUniqueFileName, tags, folder, isPrivateFile, customCoordinates, responseFields) {
37-
let ik = this.imagekit.ikInstance;
38-
const onError = this.onError;
39-
const onSuccess = this.onSuccess;
46+
getUploadParams(file, fileName, useUniqueFileName?, tags?, folder?, isPrivateFile?, customCoordinates?, responseFields?) {
4047
const params:object = {
4148
file: file,
4249
fileName: fileName,
@@ -64,14 +71,6 @@ export class IkUploadComponent implements OnInit {
6471
if (responseFields !== undefined) {
6572
Object.assign(params, { responseFields: responseFields });
6673
}
67-
68-
ik.upload(params, function (err, result) {
69-
if (err) {
70-
onError.emit(err);
71-
} else {
72-
onSuccess.emit(result);
73-
}
74-
});
74+
return params;
7575
}
76-
7776
}

sdk/lib/src/imagekitio-angular/imagekit.service.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ export class ImageKitConfiguration {
1919
isPrivateFile?: boolean;
2020
folder?: string;
2121
customCoordinates?: any;
22-
onError?: Function;
23-
onSuccess?: Function;
2422
sdkVersion?: string;
2523
}
2624

Lines changed: 78 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2-
import { ElementRef } from '@angular/core';
3-
import { IkImageComponent } from '../../lib/src/imagekitio-angular/ik-image/ik-image.component';
4-
import { ImagekitService } from '../../lib/src/imagekitio-angular/imagekit.service';
1+
import { ElementRef } from "@angular/core";
2+
import { IkImageComponent } from "../../lib/src/imagekitio-angular/ik-image/ik-image.component";
3+
import { ImagekitService } from "../../lib/src/imagekitio-angular/imagekit.service";
54

6-
describe('IkImageComponent', () => {
5+
describe("IkImageComponent", () => {
76
let component: IkImageComponent;
87
let imageKitService: ImagekitService;
98

@@ -17,30 +16,87 @@ describe('IkImageComponent', () => {
1716
component = new IkImageComponent(elRef, imageKitService);
1817
});
1918

20-
it('setUrl should create correct URL when src is provided', () => {
21-
component.setUrl('abc');
22-
expect(component.url).toContain('/abc?ik-sdk-version=angular-');
19+
it("setUrl should create correct URL when src is provided", () => {
20+
component.setUrl("abc");
21+
expect(component.url).toContain("/abc?ik-sdk-version=angular-");
2322
});
2423

25-
it('setUrl should create correct URL when path is provided', () => {
26-
component.setUrl(null, 'def');
27-
expect(component.url).toContain('/url/def?ik-sdk-version=angular-');
24+
it("setUrl should create correct URL when path is provided", () => {
25+
component.setUrl(null, "def");
26+
expect(component.url).toContain("/url/def?ik-sdk-version=angular-");
2827
});
2928

30-
it('setUrl should create correct lqipURL in addition to URL when lqip is provided', () => {
31-
component.setUrl('abc', null, null, { active: true, quality: 1 });
32-
expect(component.url).toContain('/abc?ik-sdk-version=angular-');
33-
expect(component.lqipUrl).toContain('tr=q-1');
34-
console.log(component.lqipUrl)
29+
it("setUrl should create correct lqipURL in addition to URL when lqip is provided", () => {
30+
component.setUrl("abc", null, null, { active: true, quality: 1 });
31+
expect(component.url).toContain("/abc?ik-sdk-version=angular-");
32+
expect(component.lqipUrl).toContain("tr=q-1");
3533
});
3634

37-
it('lqipload should create correct url format if path is provided', () => {
38-
const lqipURl = component.lqipload(10, '/abc?ik-sdk-version=angular-0.0.0', '/xyz');
39-
expect(lqipURl).toContain('tr:q-10');
35+
it("lqipload should create correct query parameters if path is provided", () => {
36+
const lqipURl = component.lqipload(
37+
10,
38+
"/abc?ik-sdk-version=angular-0.0.0",
39+
"/xyz"
40+
);
41+
expect(lqipURl).toContain("tr:q-10");
4042
});
4143

42-
it('lqipload should create corrext url format if path is not provided', () => {
43-
const lqipURl = component.lqipload(10, '/abc?ik-sdk-version=angular-0.0.0', null);
44-
expect(lqipURl).toContain('tr=q-10');
44+
it("lqipload should create correct query parameters if path is not provided", () => {
45+
const lqipURl = component.lqipload(
46+
10,
47+
"/abc?ik-sdk-version=angular-0.0.0",
48+
null
49+
);
50+
expect(lqipURl).toContain("tr=q-10");
4551
});
52+
53+
it("setUrl should add transformations in query parameters", () => {
54+
const transformation = [
55+
{ height: "200", width: "200" },
56+
{
57+
rotation: "90"
58+
}
59+
];
60+
component.setUrl("abc", null, transformation);
61+
expect(component.url).toContain("&tr=h-200%2Cw-200%3Art-90");
62+
});
63+
64+
it("setUrl should handle the presence and absence of leading slash in path parameters", () => {
65+
let comp: IkImageComponent;
66+
let iKService: ImagekitService;
67+
iKService = new ImagekitService({
68+
urlEndpoint: "https://ik.imagekit.io/company/",
69+
publicKey: "abc",
70+
authenticationEndpoint: "http://example.com/auth"
71+
});
72+
let elRef: ElementRef;
73+
comp = new IkImageComponent(elRef, iKService);
74+
comp.setUrl(null, "/abc.png");
75+
expect(comp.url).toContain("https://ik.imagekit.io/company/abc.png?ik-sdk-version=angular-");
76+
comp.setUrl(null, "abc.png");
77+
expect(comp.url).toContain("https://ik.imagekit.io/company/abc.png?ik-sdk-version=angular-");
78+
});
79+
80+
it("setUrl should handle the presence and absence of leading slash in urlEndpoint parameters", () => {
81+
let comp: IkImageComponent;
82+
let iKService: ImagekitService;
83+
iKService = new ImagekitService({
84+
urlEndpoint: "https://ik.imagekit.io/company",
85+
publicKey: "abc",
86+
authenticationEndpoint: "http://example.com/auth"
87+
});
88+
let elRef: ElementRef;
89+
comp = new IkImageComponent(elRef, iKService);
90+
91+
comp.setUrl(null, "/abc.png");
92+
expect(comp.url).toContain("https://ik.imagekit.io/company/abc.png?ik-sdk-version=angular-");
93+
iKService = new ImagekitService({
94+
urlEndpoint: "https://ik.imagekit.io/company/",
95+
publicKey: "abc",
96+
authenticationEndpoint: "http://example.com/auth"
97+
});
98+
comp.setUrl(null, "/def.png");
99+
expect(comp.url).toContain("https://ik.imagekit.io/company/def.png?ik-sdk-version=angular-");
100+
});
101+
46102
});

0 commit comments

Comments
 (0)