Skip to content

Commit b5f22dd

Browse files
committed
chore: added test cases for image component
1 parent 5aa2beb commit b5f22dd

File tree

4 files changed

+136
-68
lines changed

4 files changed

+136
-68
lines changed

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

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import { ImagekitService } from '../imagekit.service';
88
export class IkImageComponent implements AfterViewInit, OnInit {
99
@Input('src') src:string;
1010
@Input('path') path:string;
11+
@Input('urlEndpoint') urlEndpoint:string;
1112
@Input('transformation') transformation:Array<Object> = [];
13+
@Input('transformationPosition') transformationPosition:string;
14+
@Input('queryParameters') queryParameters:Object;
1215
@Input('lqip') lqip:any;
1316
url = '';
1417
lqipUrl = '';
@@ -19,7 +22,7 @@ export class IkImageComponent implements AfterViewInit, OnInit {
1922
}
2023

2124
ngOnInit(): void {
22-
this.setUrl(this.src, this.path, this.transformation, this.lqip);
25+
this.setUrl(this.src, this.path, this.transformation, this.lqip, this.urlEndpoint, this.transformationPosition, this.queryParameters);
2326
}
2427

2528
ngAfterViewInit() {
@@ -35,18 +38,36 @@ export class IkImageComponent implements AfterViewInit, OnInit {
3538
imageObserver.observe(this.el.nativeElement);
3639
}
3740

38-
setUrl(src?, path?, transformation?, lqip?) {
41+
setUrl(src?, path?, transformation?, lqip?, urlEndpoint?, transformationPosition?, queryParameters?) {
42+
const config = this.getConfigObject(src, path, transformation, transformationPosition, urlEndpoint, queryParameters)
43+
this.url = this.imagekit.getUrl(config);
44+
if (lqip && lqip.active === true) {
45+
this.lqipUrl = this.lqipload(lqip.quality, this.url, this.path);
46+
}
47+
}
48+
49+
getConfigObject(src?, path?, transformation?, transformationPosition?, urlEndpoint?, queryParameters?) {
50+
const config = {
51+
transformation: transformation,
52+
};
53+
if (urlEndpoint) {
54+
config['urlEndpoint'] = urlEndpoint;
55+
}
56+
if (queryParameters) {
57+
config['queryParameters'] = queryParameters;
58+
}
3959
if (src) {
40-
this.url = this.imagekit.getUrl({ src: src, transformation: transformation, transformationPosition: "query" });
60+
config['src'] = src;
61+
config['transformationPosition'] = 'query';
4162
} else if (path) {
42-
this.url = this.imagekit.getUrl({ path: path, transformation: transformation });
63+
config['path'] = path;
64+
if (transformationPosition) {
65+
config['transformationPosition'] = transformationPosition;
66+
}
4367
} else {
4468
throw new Error('Missing src / path during initialization!');
4569
}
46-
47-
if (lqip && lqip.active === true) {
48-
this.lqipUrl = this.lqipload(lqip.quality, this.url, this.path);
49-
}
70+
return config;
5071
}
5172

5273
loadImage(url:string) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class ImagekitService {
3535
}
3636

3737
getUrl(config: object): string {
38-
return this._ikInstance.url(config);
38+
const url = this._ikInstance.url(config);
39+
return url;
3940
}
4041
}
Lines changed: 101 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,128 @@
11
import { ElementRef } from "@angular/core";
22
import { IkImageComponent } from "../../lib/src/imagekitio-angular/ik-image/ik-image.component";
33
import { ImagekitService } from "../../lib/src/imagekitio-angular/imagekit.service";
4+
const pjson = require("../../lib/package.json");
5+
const version = `angular-${pjson.version}`;
46

57
describe("IkImageComponent", () => {
68
let component: IkImageComponent;
79
let imageKitService: ImagekitService;
810

911
beforeEach(() => {
1012
imageKitService = new ImagekitService({
11-
urlEndpoint: "url",
12-
publicKey: "pub",
13-
authenticationEndpoint: "auth"
13+
urlEndpoint: "https://ik.imagekit.io/company/",
14+
publicKey: "abc",
15+
authenticationEndpoint: "http://example.com/auth"
1416
});
1517
let elRef: ElementRef;
1618
component = new IkImageComponent(elRef, imageKitService);
1719
});
1820

21+
it("urlEndpoint passed to component should be used over initialized value", () => {
22+
component.setUrl(null, "def", null, null, 'localhost:3000');
23+
expect(component.url).toBe(`localhost:3000/def?ik-sdk-version=${version}`);
24+
});
25+
26+
it("Presence and absence of trailing slash in should not result in double slash (//) in the returned url", () => {
27+
let comp: IkImageComponent;
28+
let iKService: ImagekitService;
29+
iKService = new ImagekitService({
30+
urlEndpoint: "https://ik.imagekit.io/company",
31+
publicKey: "abc",
32+
authenticationEndpoint: "http://example.com/auth"
33+
});
34+
let elRef: ElementRef;
35+
comp = new IkImageComponent(elRef, iKService);
36+
37+
comp.setUrl(null, "/abc.png");
38+
expect(comp.url).toBe(
39+
`https://ik.imagekit.io/company/abc.png?ik-sdk-version=${version}`
40+
);
41+
iKService = new ImagekitService({
42+
urlEndpoint: "https://ik.imagekit.io/company/",
43+
publicKey: "abc",
44+
authenticationEndpoint: "http://example.com/auth"
45+
});
46+
comp.setUrl(null, "/def.png");
47+
expect(comp.url).toBe(
48+
`https://ik.imagekit.io/company/def.png?ik-sdk-version=${version}`
49+
);
50+
});
51+
52+
it("Presence and absence of leading slash in parameter should not result in double slash (//) in the returned url", () => {
53+
let comp: IkImageComponent;
54+
let iKService: ImagekitService;
55+
iKService = new ImagekitService({
56+
urlEndpoint: "https://ik.imagekit.io/company/",
57+
publicKey: "abc",
58+
authenticationEndpoint: "http://example.com/auth"
59+
});
60+
let elRef: ElementRef;
61+
comp = new IkImageComponent(elRef, iKService);
62+
comp.setUrl(null, "/abc.png");
63+
expect(comp.url).toContain(
64+
`https://ik.imagekit.io/company/abc.png?ik-sdk-version=${version}`
65+
);
66+
comp.setUrl(null, "abc.png");
67+
expect(comp.url).toContain(
68+
`https://ik.imagekit.io/company/abc.png?ik-sdk-version=${version}`
69+
);
70+
});
71+
72+
it("new unsupported transformation parameter is passed then it should come in URL as it is", () => {
73+
const transformation = [{ foo: "200", bar: "200" }];
74+
component.setUrl("https://abc.com/def", null, transformation);
75+
expect(component.url).toBe(`https://abc.com/def?ik-sdk-version=${version}&tr=foo-200%2Cbar-200`);
76+
});
77+
78+
it("supported transformation parameter is passed then it should come in query parameters after transformation", () => {
79+
const transformation = [{ height: "200", width: "200" }, { rotation: "90"}];
80+
component.setUrl("https://abc.com/def", null, transformation);
81+
expect(component.url).toBe(`https://abc.com/def?ik-sdk-version=${version}&tr=h-200%2Cw-200%3Art-90`);
82+
});
83+
84+
it("if SRC is used to create URL, transformartioPosition should be query", () => {
85+
const config = component.getConfigObject('abc');
86+
expect(config['transformationPosition']).toBe('query')
87+
});
88+
89+
it("if SRC is used to create URL, transformartioPosition should be query even if anything else is passed", () => {
90+
const transformation = [{ height: "200", width: "200" }, { rotation: "90"}];
91+
const config = component.getConfigObject('https://example.com/ab.png', null, transformation, 'path', null);
92+
expect(config['transformationPosition']).toBe('query')
93+
component.setUrl('https://example.com/ab.png', null, transformation, 'path', null)
94+
expect(component.url).toContain('&tr=');
95+
});
96+
97+
it("Parameters passed to queryParameters should be present in URL if src is used", () => {
98+
component.setUrl('https://example.com/ab.png', null, null, null, null, null, {version:5, name: 'check'});
99+
expect(component.url).toContain('&version=5&name=check');
100+
});
101+
102+
it("Parameters passed to queryParameters should be present in URL if src with existing query is used", () => {
103+
component.setUrl('https://example.com/ab.png?foo=bar&baz=nax', null, null, null, null, null, {version:5, name: 'check'});
104+
expect(component.url).toContain('&version=5&name=check');
105+
expect(component.url).toBe('https://example.com/ab.png?foo=bar&baz=nax&ik-sdk-version=angular-0.0.1&version=5&name=check');
106+
});
107+
108+
it("Parameters passed to queryParameters should be present in URL if path is used", () => {
109+
component.setUrl(null, '/default.png', null, null, null, null, {version:6, name: 'bar'});
110+
expect(component.url).toContain('&version=6&name=bar');
111+
});
112+
19113
it("setUrl should create correct URL when src is provided", () => {
20114
component.setUrl("abc");
21-
expect(component.url).toContain("/abc?ik-sdk-version=angular-");
115+
expect(component.url).toContain(`/abc?ik-sdk-version=${version}`);
22116
});
23117

24118
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-");
119+
component.setUrl(null, "def", null, null);
120+
expect(component.url).toContain(`https://ik.imagekit.io/company/def?ik-sdk-version=${version}`);
27121
});
28122

29123
it("setUrl should create correct lqipURL in addition to URL when lqip is provided", () => {
30124
component.setUrl("abc", null, null, { active: true, quality: 1 });
31-
expect(component.url).toContain("/abc?ik-sdk-version=angular-");
125+
expect(component.url).toContain(`/abc?ik-sdk-version=${version}`);
32126
expect(component.lqipUrl).toContain("tr=q-1");
33127
});
34128

@@ -49,54 +143,4 @@ describe("IkImageComponent", () => {
49143
);
50144
expect(lqipURl).toContain("tr=q-10");
51145
});
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-
102146
});

sdk/src/sdk-tests/imagekit.service.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { ImagekitService } from "../../lib/src/imagekitio-angular/imagekit.service";
2+
const pjson = require("../../lib/package.json");
3+
const version = `angular-${pjson.version}`;
24

35
describe("ImagekitService", () => {
46
let imagekitService: ImagekitService;
@@ -10,7 +12,7 @@ describe("ImagekitService", () => {
1012
});
1113
})
1214

13-
it("getUrl should return correct url with correct SDK name", () => {
14-
expect(imagekitService.getUrl({src: 'abc'})).toContain('ik-sdk-version=angular-')
15+
it("The url returned should have parameter with the value", () => {
16+
expect(imagekitService.getUrl({src: 'abc'})).toContain(`ik-sdk-version=${version}`)
1517
});
1618
});

0 commit comments

Comments
 (0)