Skip to content

Commit 6143b7a

Browse files
authored
Merge pull request #3 from imagekit-developer/testing
Added missing test cases, fixes #1
2 parents e4c8bb8 + 4deafc3 commit 6143b7a

File tree

8 files changed

+385
-94
lines changed

8 files changed

+385
-94
lines changed

DEVELOPING.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ This document describes how to develop Imagekit Angular SDK.
44

55
- [Setting up development environment](#setting-up-development-environment)
66
- [Building the project](#building-the-project)
7-
- [File naming convention](#file-naming-convention)
87

98
## Setting up development environment
109

@@ -51,3 +50,13 @@ Use
5150
npm run build
5251
```
5352
from the `sdk` folder to build the library. This creates a package in `dist/imagekitio-angular` folder.
53+
54+
### Running Tests
55+
56+
The designated directory for tests is `sdk/src/sdk-tests` folder. All tests will be executed once on the Chrome Headless browser.
57+
58+
Use
59+
```sh
60+
npm run test
61+
```
62+
from the `sdk` folder to start testing.

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { ImagekitService } from '../imagekit.service';
44
@Component({
55
selector: 'ik-image',
66
template: `<img src={{src}}>`,
7-
providers: [ImagekitService]
87
})
98
export class IkImageComponent implements AfterViewInit, OnInit {
109
@Input('src') src:string;
@@ -36,17 +35,17 @@ export class IkImageComponent implements AfterViewInit, OnInit {
3635
imageObserver.observe(this.el.nativeElement);
3736
}
3837

39-
setUrl(src, path, transformation, lqip) {
38+
setUrl(src?, path?, transformation?, lqip?) {
4039
if (src) {
41-
this.url = this.imagekit.ikInstance.url({ src: src, transformation: transformation, transformationPosition: "query" });
40+
this.url = this.imagekit.getUrl({ src: src, transformation: transformation, transformationPosition: "query" });
4241
} else if (path) {
43-
this.url = this.imagekit.ikInstance.url({ path: path, transformation: transformation });
42+
this.url = this.imagekit.getUrl({ path: path, transformation: transformation });
4443
} else {
4544
throw new Error('Missing src / path during initialization!');
4645
}
4746

48-
if (lqip !== undefined && lqip.active === true) {
49-
this.lqipUrl = this.lqipload(lqip.quality);
47+
if (lqip && lqip.active === true) {
48+
this.lqipUrl = this.lqipload(lqip.quality, this.url, this.path);
5049
}
5150
}
5251

@@ -69,10 +68,9 @@ export class IkImageComponent implements AfterViewInit, OnInit {
6968
return target;
7069
};
7170

72-
lqipload(quality) {
73-
let url = this.url;
71+
lqipload(quality, url, path) {
7472
let lqip = "";
75-
if (this.path !== undefined) {
73+
if (path) {
7674
let newUrl = url.split("tr:");
7775
if (newUrl[0] === url) {
7876
let temp = url.split("/");

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
}
Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
1-
import { Injectable } from '@angular/core';
2-
import ImageKit from 'imagekit-javascript';
3-
const pjson = require('../../package.json');
1+
import { Injectable } from "@angular/core";
2+
import * as ImageKit from "imagekit-javascript";
3+
const pjson = require("../../package.json");
44

55
export interface Lqip {
66
readonly active: boolean;
77
readonly quality: number;
88
}
99

1010
export class ImageKitConfiguration {
11-
urlEndpoint: string;
12-
publicKey: string;
13-
authenticationEndpoint?: string;
14-
lqip?: Lqip;
15-
fileName?: string;
16-
tags?: Array<string>;
17-
useUniqueFileName?: boolean;
18-
responseFields?: any;
19-
isPrivateFile?: boolean;
20-
folder?: string;
21-
customCoordinates?: any;
22-
onError?: Function;
23-
onSuccess?: Function;
24-
sdkVersion?: string;
11+
urlEndpoint: string;
12+
publicKey: string;
13+
authenticationEndpoint?: string;
14+
lqip?: Lqip;
15+
fileName?: string;
16+
tags?: string;
17+
useUniqueFileName?: boolean;
18+
responseFields?: any;
19+
isPrivateFile?: boolean;
20+
folder?: string;
21+
customCoordinates?: any;
22+
sdkVersion?: string;
2523
}
2624

2725
@Injectable()
2826
export class ImagekitService {
2927
_ikInstance: any;
30-
constructor(configuration: ImageKitConfiguration) {
31-
configuration.sdkVersion = `angular-${pjson.version}`,
32-
this._ikInstance = new ImageKit(configuration)
28+
constructor(private configuration: ImageKitConfiguration) {
29+
(configuration.sdkVersion = `angular-${pjson.version}`),
30+
(this._ikInstance = new ImageKit(this.configuration));
3331
}
3432

3533
get ikInstance(): any {
3634
return this._ikInstance;
3735
}
36+
37+
getUrl(config: object): string {
38+
return this._ikInstance.url(config);
39+
}
3840
}

sdk/lib/src/imagekitio-angular/imagekitio-angular.module.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { ModuleWithProviders, NgModule, Optional, SkipSelf } from '@angular/core';
22
import { IkImageComponent } from './ik-image/ik-image.component';
33
import { IkUploadComponent } from './ik-upload/ik-upload.component';
4-
import { ImageKitConfiguration } from './imagekit.service';
4+
import { ImageKitConfiguration, ImagekitService } from './imagekit.service';
55

66

77
@NgModule({
88
declarations: [IkUploadComponent, IkImageComponent],
99
imports: [
1010
],
11-
exports: [IkUploadComponent, IkImageComponent]
11+
exports: [IkUploadComponent, IkImageComponent],
12+
providers: [ ImagekitService ]
1213
})
1314
export class ImagekitioAngularModule {
1415
constructor (@Optional() @SkipSelf() parentModule: ImagekitioAngularModule) {
Lines changed: 95 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,102 @@
1-
// import { async, ComponentFixture, TestBed } from '@angular/core/testing';
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";
24

3-
// import { IkImageComponent } from '../../lib/src/imagekitio-angular/ik-image/ik-image.component';
4-
// import { ImageKitConfiguration } from '../../lib/src/imagekitio-angular/imagekit.service';
5+
describe("IkImageComponent", () => {
6+
let component: IkImageComponent;
7+
let imageKitService: ImagekitService;
58

6-
describe('IkImageComponent', () => {
7-
// let component: IkImageComponent;
8-
// let fixture: ComponentFixture<IkImageComponent>;
9+
beforeEach(() => {
10+
imageKitService = new ImagekitService({
11+
urlEndpoint: "url",
12+
publicKey: "pub",
13+
authenticationEndpoint: "auth"
14+
});
15+
let elRef: ElementRef;
16+
component = new IkImageComponent(elRef, imageKitService);
17+
});
18+
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-");
22+
});
23+
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-");
27+
});
928

10-
// beforeEach(async(() => {
11-
// TestBed.configureTestingModule({
12-
// declarations: [ IkImageComponent ],
13-
// providers: [ImageKitConfiguration]
14-
// })
15-
// .compileComponents();
16-
// }));
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");
33+
});
1734

18-
// beforeEach(() => {
19-
// fixture = TestBed.createComponent(IkImageComponent);
20-
// component = fixture.componentInstance;
21-
// fixture.detectChanges();
22-
// });
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");
42+
});
2343

24-
it('is dummy and should pass', () => {
25-
expect(true).toBeTruthy();
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");
2651
});
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+
27102
});

0 commit comments

Comments
 (0)