Skip to content

Commit 4d30974

Browse files
committed
improve test coverage
1 parent 990b74d commit 4d30974

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

src/url.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { UrlOptions } from "./interfaces";
22
import { ImageOverlay, SolidColorOverlay, SubtitleOverlay, TextOverlay, Transformation, VideoOverlay } from "./interfaces/Transformation";
3-
import transformationUtils from "./utils/transformation";
4-
import { safeBtoa } from "./utils/transformation";
3+
import transformationUtils, { safeBtoa } from "./utils/transformation";
54
const TRANSFORMATION_PARAMETER = "tr";
65
const SIMPLE_OVERLAY_PATH_REGEX = new RegExp('^[a-zA-Z0-9-._/ ]*$')
76
const SIMPLE_OVERLAY_TEXT_REGEX = new RegExp('^[a-zA-Z0-9-._ ]*$') // These characters are selected by testing actual URLs on both path and query parameters. If and when backend starts supporting wide range of characters, this regex should be updated to improve URL readability.
@@ -118,13 +117,11 @@ function processText(str: string, enccoding: TextOverlay["encoding"]): string {
118117

119118
function processOverlay(overlay: Transformation["overlay"]): string | undefined {
120119
const entries = [];
121-
if (!overlay) {
122-
return;
123-
}
124-
const { type, position = {}, timing = {}, transformation = [] } = overlay;
120+
121+
const { type, position = {}, timing = {}, transformation = [] } = overlay || {};
125122

126123
if (!type) {
127-
throw new Error("Overlay type is required");
124+
return;
128125
}
129126

130127
switch (type) {

src/utils/transformation.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,9 @@ const TRANSFORM_DELIMITER: string = ",";
1010
const TRANSFORM_KEY_VALUE_DELIMITER: string = "-";
1111

1212
export default {
13-
getDefault: (): TransformationPosition => {
14-
return DEFAULT_TRANSFORMATION_POSITION;
15-
},
1613
addAsQueryParameter: (options: UrlOptions) => {
1714
return options.transformationPosition === QUERY_TRANSFORMATION_POSITION;
1815
},
19-
validParameters: (options: UrlOptions) => {
20-
if (typeof options.transformationPosition == "undefined") return false;
21-
return VALID_TRANSFORMATION_POSITIONS.indexOf(options.transformationPosition) != -1;
22-
},
2316
getTransformKey: function (transform: string) {
2417
if (!transform) { return ""; }
2518

@@ -38,6 +31,7 @@ export default {
3831

3932
export const safeBtoa = function (str: string): string {
4033
if (typeof window !== "undefined") {
34+
/* istanbul ignore next */
4135
return btoa(str);
4236
} else {
4337
// Node fallback

test/url-generation/basic.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe("URL generation", function () {
1212
expect(url).equal("");
1313
});
1414

15-
it('should return an empty string for an invalid src URL', function () {
15+
it('should return an empty string when src is /', function () {
1616
const url = buildURL({
1717
urlEndpoint: "https://ik.imagekit.io/test_url_endpoint",
1818
transformationPosition: "query",
@@ -22,6 +22,16 @@ describe("URL generation", function () {
2222
expect(url).equal("https://ik.imagekit.io/test_url_endpoint/");
2323
});
2424

25+
it('should return an empty string when src is invalid', function () {
26+
const url = buildURL({
27+
urlEndpoint: "https://ik.imagekit.io/test_url_endpoint",
28+
transformationPosition: "query",
29+
src: "https://"
30+
});
31+
32+
expect(url).equal("");
33+
});
34+
2535
it('should generate a valid URL when src is provided without transformation', function () {
2636
const url = buildURL({
2737
urlEndpoint: "https://ik.imagekit.io/test_url_endpoint",

test/url-generation/overlay.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ describe("Overlay Transformation Test Cases", function () {
1919
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/base-image.jpg`);
2020
});
2121

22+
it('Ignore if type is missing', function () {
23+
const url = buildURL({
24+
transformationPosition: "path",
25+
urlEndpoint: "https://ik.imagekit.io/test_url_endpoint",
26+
src: "/base-image.jpg",
27+
transformation: [{
28+
overlay: {
29+
}
30+
}]
31+
});
32+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/base-image.jpg`);
33+
});
34+
2235
it('Ignore invalid values if input (image)', function () {
2336
const url = buildURL({
2437
transformationPosition: "path",

0 commit comments

Comments
 (0)