Skip to content
This repository was archived by the owner on Mar 30, 2021. It is now read-only.

Commit 96ea5da

Browse files
Merge pull request #34 from Nikhil-Vats/add_tests
Add tests
2 parents 228a584 + c76c5f2 commit 96ea5da

File tree

3 files changed

+95
-14
lines changed

3 files changed

+95
-14
lines changed

__tests__/app.js

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,54 @@ describe("generator-biojs-webcomponents:app - Make a new Web Component", () => {
6363
]);
6464
});
6565
});
66-
it("throws an error if user enters an empty string as toolNameHuman", async () => {
66+
it("passes if toolNameComputer is correctly formatted", async () => {
6767
assert.equal(
68-
await validators.toolNameHuman(""),
69-
chalk.red("This is a mandatory field, please answer.")
68+
await validators.toolNameComputer("tool-name-computer-1"),
69+
true
7070
);
7171
});
72+
it("passes if toolNameHuman is correctly formatted", async () => {
73+
assert.equal(await validators.toolNameHuman("Tool Name Human"), true);
74+
});
75+
it("throws an error if toolNameComputer is wrongly formatted", async () => {
76+
let err1 = await validators.toolNameComputer("-tool-name");
77+
let err2 = await validators.toolNameComputer("tool-name-");
78+
let err3 = await validators.toolNameComputer("toolName-computer");
79+
let err4 = await validators.toolNameComputer("*toolname-comp");
80+
let err5 = await validators.toolNameComputer("tool name");
81+
if (
82+
err1 !== true &&
83+
err2 !== true &&
84+
err3 !== true &&
85+
err4 !== true &&
86+
err5 !== true
87+
) {
88+
assert.ok(true);
89+
} else {
90+
assert.fail();
91+
}
92+
});
93+
it("throws an error if toolNameHuman is wrongly formatted", async () => {
94+
let err1 = await validators.toolNameHuman("tool-name");
95+
let err2 = await validators.toolNameHuman("toolname1");
96+
if (err1 !== true && err2 !== true) {
97+
assert.ok(true);
98+
} else {
99+
assert.fail();
100+
}
101+
});
72102
it("throws an error if user enters an empty string as toolNameComputer", async () => {
73103
assert.equal(
74104
await validators.toolNameComputer(""),
75105
chalk.red("This is a mandatory field, please answer.")
76106
);
77107
});
108+
it("throws an error if user enters an empty string as toolNameHuman", async () => {
109+
assert.equal(
110+
await validators.toolNameHuman(""),
111+
chalk.red("This is a mandatory field, please answer.")
112+
);
113+
});
78114
});
79115

80116
describe("generator-biojs-webcomponents:app - Upgrade an existing component by importing build file locally", () => {
@@ -83,12 +119,16 @@ describe("generator-biojs-webcomponents:app - Upgrade an existing component by i
83119
assert.file("test-component");
84120
});
85121
});
122+
it("does not throw an error if user enters path of a project directory which already exists", async () => {
123+
let res = await validators.storeArg("test-component");
124+
assert.equal(res, true);
125+
});
86126
it("makes a new directory named - component-dist", async () => {
87127
await validators
88128
.directoryName("component-dist")
89129
.then(() => assert.file(["test-component/component-dist"]));
90130
});
91-
it("imports the build file", async () => {
131+
it("imports the build file locally", async () => {
92132
await validators
93133
.importBuildFileLocally(
94134
path.join(__dirname, "../generators/app/validator.js")
@@ -97,6 +137,18 @@ describe("generator-biojs-webcomponents:app - Upgrade an existing component by i
97137
assert.file(["test-component/component-dist/validator.js"]);
98138
});
99139
});
140+
it("overwrites the directory content if user enters o or O", async () => {
141+
await validators
142+
.directoryName("o")
143+
.then(() =>
144+
assert.noFile(["test-component/component-dist/validator.js"])
145+
);
146+
});
147+
it("makes a new directory if user enters a new name", async () => {
148+
await validators.directoryName("new-build-dir").then(() => {
149+
assert.file("test-component/new-build-dir");
150+
});
151+
});
100152
it("throws an error if user enters an empty string as path of build file", async () => {
101153
assert.equal(
102154
await validators.importBuildFileFromNPM(""),
@@ -105,7 +157,27 @@ describe("generator-biojs-webcomponents:app - Upgrade an existing component by i
105157
});
106158
});
107159

160+
describe("generator-biojs-webcomponents:app - Upgrade an existing component by installing component from npm package", () => {
161+
it("installs the latest component from its npm package if user enters a valid version", async () => {
162+
jest.setTimeout(10000); // Increases timeout for this test
163+
let res = await validators.checkVersionAndInstallComponent("latest", {
164+
packageNameToInstallComponent: "is-odd"
165+
});
166+
assert.equal(res, true);
167+
});
168+
});
169+
108170
describe("generator-biojs-webcomponents:app - Upgrade an existing component by importing build file using npm", () => {
171+
it("runs the generator in the directory passed in arguments", async () => {
172+
await validators.storeArg("test-component").then(() => {
173+
assert.file("test-component");
174+
});
175+
});
176+
it("makes a new directory named - component-dist", async () => {
177+
await validators
178+
.directoryName("component-dist")
179+
.then(() => assert.file(["test-component/component-dist"]));
180+
});
109181
it("throws an error if version entered does not exist", async () => {
110182
assert.equal(
111183
await validators.version("fkdk", { packageName: "node" }),
@@ -118,6 +190,15 @@ describe("generator-biojs-webcomponents:app - Upgrade an existing component by i
118190
)
119191
);
120192
});
193+
it("downloads the URL of build file entered is correct", async () => {
194+
await validators
195+
.importBuildFileFromNPM(
196+
"https://cdn.jsdelivr.net/npm/[email protected]/index.min.js"
197+
)
198+
.then(() => {
199+
assert.file("test-component/component-dist/index.min.js");
200+
});
201+
});
121202
it("throws an error if an npm package doesn't exist", async () => {
122203
assert.equal(
123204
await validators.packageName("cbaxyz"),

generators/app/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,9 @@ module.exports = class extends Generator {
481481
* @return {aString}, but with spaces removed and camelCased.
482482
**/
483483
function toCamelCase(aString) {
484-
var tokens = aString.split(" ");
484+
let tokens = aString.split(" ");
485485

486-
var camelString = "";
486+
let camelString = "";
487487
tokens.map(function(token, index) {
488488
if (token.trim() !== "") {
489489
// Remove extra space between the words

generators/app/validator.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ validators.storeArg = async function(props) {
1313
return true;
1414
}
1515

16-
var res = await executeCommand("mkdir " + projectDirectory)
16+
let res = await executeCommand("mkdir " + projectDirectory)
1717
.then(() => true)
1818
.catch(async () => {
19-
var tryAgain = await executeCommand(
19+
let tryAgain = await executeCommand(
2020
"ls " + projectDirectory,
2121
"checkDirExistence"
2222
)
@@ -70,7 +70,7 @@ validators.packageName = async function(props) {
7070
validators.version = async function(props, answers) {
7171
if (props) {
7272
let command = "npm view " + answers.packageName + "@" + props;
73-
var res = await executeCommand(command, "version")
73+
let res = await executeCommand(command, "version")
7474
.then(() => {
7575
return true;
7676
})
@@ -97,9 +97,9 @@ validators.checkVersionAndInstallComponent = async function(props, answers) {
9797
if (props) {
9898
let command =
9999
"npm view " + answers.packageNameToInstallComponent + "@" + props;
100-
var res = await executeCommand(command, "version")
100+
let res = await executeCommand(command, "version")
101101
.then(async () => {
102-
var res = await executeCommand(
102+
let res = await executeCommand(
103103
"cd " +
104104
projectDirectory +
105105
" && npm i " +
@@ -139,7 +139,7 @@ validators.checkVersionAndInstallComponent = async function(props, answers) {
139139
};
140140

141141
validators.directoryName = async props => {
142-
var res;
142+
let res;
143143
if (props.trim() === "o" || props.trim() === "O") {
144144
res = await executeCommand(`rm -rf ${projectDirectory}/${buildDirectory}/*`)
145145
.then(() => true)
@@ -173,7 +173,7 @@ validators.directoryName = async props => {
173173

174174
validators.importBuildFileFromNPM = async function(props) {
175175
if (props) {
176-
var res = await executeCommand(
176+
let res = await executeCommand(
177177
"cd " + projectDirectory + "/" + buildDirectory + " && curl -O " + props,
178178
"importBuildFileFromNPM"
179179
)
@@ -205,7 +205,7 @@ validators.importBuildFileFromNPM = async function(props) {
205205

206206
validators.importBuildFileLocally = async props => {
207207
if (props) {
208-
var res = await executeCommand(
208+
let res = await executeCommand(
209209
"cp " + props + " " + projectDirectory + "/" + buildDirectory,
210210
"importBuildFileLocally"
211211
)

0 commit comments

Comments
 (0)