Skip to content

Commit

Permalink
Add test to check if alphabets were supplied to dimension
Browse files Browse the repository at this point in the history
  • Loading branch information
ClydeDz committed May 31, 2019
1 parent 8e2d1e2 commit bebb5b4
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/utility.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export declare class Utility {
* @param setting The setting that needs to be supplied as the value for that key.
*/
resolveSettings(key: string, setting: string | CharacterSets | BackgroundSets | undefined): string;
/**
* Validates dimensions and returns dimension is correct format.
* @param width Width of the image
* @param height Height of the image
*/
containsOnlyNumbers(width: number, height: number): boolean;
/**
* Validates dimensions and returns dimension is correct format.
* @param width Width of the image
Expand Down
12 changes: 12 additions & 0 deletions lib/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ class Utility {
}
return `${key}=${setting}&`;
}
/**
* Validates dimensions and returns dimension is correct format.
* @param width Width of the image
* @param height Height of the image
*/
containsOnlyNumbers(width, height) {
const onlyNumbersRegEx = /^\d+$/;
return onlyNumbersRegEx.test(width.toString()) && onlyNumbersRegEx.test(height.toString());
}
/**
* Validates dimensions and returns dimension is correct format.
* @param width Width of the image
Expand All @@ -24,6 +33,9 @@ class Utility {
if (!width || !height) {
return "";
}
if (!this.containsOnlyNumbers(width, height)) {
return "";
}
return `${width}x${height}`;
}
/**
Expand Down
15 changes: 15 additions & 0 deletions src/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ export class Utility {
return `${key}=${setting}&`;
}

/**
* Validates dimensions and returns dimension is correct format.
* @param width Width of the image
* @param height Height of the image
*/
public containsOnlyNumbers(width: number, height: number): boolean {
const onlyNumbersRegEx = /^\d+$/;
return onlyNumbersRegEx.test(width.toString()) && onlyNumbersRegEx.test(height.toString());
}

/**
* Validates dimensions and returns dimension is correct format.
* @param width Width of the image
Expand All @@ -27,6 +37,11 @@ export class Utility {
if (!width || !height) {
return "";
}

if (!this.containsOnlyNumbers(width, height)) {
return "";
}

return `${width}x${height}`;
}

Expand Down
61 changes: 60 additions & 1 deletion tests/robohash-avatars.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe("generateAvatar()", function () {
assert.equal(actual, "https://robohash.org/tonystark?set=set4&bgset=bg1&size=400x400");
});

it("allz inputs supplied", function () {
it("valid username but other undefined inputs supplied", function () {
console.log("\t", "[test]", this.test.title);
var actual = avatars.generateAvatar({
username: "tonystark",
Expand All @@ -78,5 +78,64 @@ describe("generateAvatar()", function () {
});
assert.equal(actual, "https://robohash.org/tonystark");
});

it("all undefined inputs supplied", function () {
console.log("\t", "[test]", this.test.title);
var actual = avatars.generateAvatar({
username: undefined,
background: undefined,
characters: undefined,
height: undefined,
width: undefined
});
assert.equal(actual, "https://robohash.org/RobohashAvatarNPM");
});

it("username and character set supplied", function () {
console.log("\t", "[test]", this.test.title);
var actual = avatars.generateAvatar({
username: "peterparker",
characters: avatars.CharacterSets.Monsters,
});
assert.equal(actual, "https://robohash.org/peterparker?set=set2");
});

it("width but no height supplied", function () {
console.log("\t", "[test]", this.test.title);
var actual = avatars.generateAvatar({
username: "peterparker",
width: 400
});
assert.equal(actual, "https://robohash.org/peterparker");
});

it("height but no width supplied", function () {
console.log("\t", "[test]", this.test.title);
var actual = avatars.generateAvatar({
username: "peterparker",
height: 400
});
assert.equal(actual, "https://robohash.org/peterparker");
});

it("height and width supplied as string", function () {
console.log("\t", "[test]", this.test.title);
var actual = avatars.generateAvatar({
username: "peterparker",
height: "400",
width: "600"
});
assert.equal(actual, "https://robohash.org/peterparker?size=600x400");
});

it("height and width supplied as string alphabets", function () {
console.log("\t", "[test]", this.test.title);
var actual = avatars.generateAvatar({
username: "peterparker",
height: "abc",
width: "ghi"
});
assert.equal(actual, "https://robohash.org/peterparker");
});

});

0 comments on commit bebb5b4

Please sign in to comment.