Skip to content

Commit

Permalink
create app unit test (#1737)
Browse files Browse the repository at this point in the history
  • Loading branch information
yishengjiang99 authored Oct 1, 2020
1 parent 4cfc660 commit c7f1f03
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ describe("hapi 17 electrode-react-webapp", () => {
});

it("should add a nonce value as provided by a function in the config", () => {
configOptions.cspNonceValue = function(request, type) {
configOptions.cspNonceValue = function (request, type) {
return `==${type}`;
};

Expand Down Expand Up @@ -1239,7 +1239,7 @@ describe("hapi 17 electrode-react-webapp", () => {

it("should inject critical css with a nonce value provided by a function", () => {
configOptions.criticalCSS = "test/data/critical.css";
configOptions.cspNonceValue = function(request, type) {
configOptions.cspNonceValue = function (request, type) {
return `==${type}`;
};

Expand Down Expand Up @@ -1664,6 +1664,7 @@ describe("hapi 17 electrode-react-webapp", () => {
.inject({
method: "GET",
url: "/"

})
.then(() => {
stdoutIntercept.restore();
Expand All @@ -1684,7 +1685,7 @@ describe("hapi 17 electrode-react-webapp", () => {
.then(res => {
expect(res.result).includes(
`<meta data-react-helmet="true" name="description" content="Nested component"/>` +
`<title data-react-helmet="true">Nested Title</title>`
`<title data-react-helmet="true">Nested Title</title>`
);
expect(res.result)
.includes(`window._config.ui = {"webappPrefix":""};\n</script><script>test-1 script;</script>
Expand All @@ -1698,7 +1699,7 @@ describe("hapi 17 electrode-react-webapp", () => {
});
});

describe("with webpackDev", function() {
describe("with webpackDev", function () {
it("should skip if webpack dev is not valid", () => {
return electrodeServer(config).then(server => {
server.ext({
Expand Down
57 changes: 52 additions & 5 deletions packages/xarc-create-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"description": "Create react.js/node.js webapp using the Electrode Platform",
"main": "dist/index.js",
"scripts": {
"prepublishOnly": "webpack"
"prepublishOnly": "webpack",
"test": "mocha",
"coverage": "nyc npm test"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/",
Expand All @@ -21,9 +23,9 @@
],
"files": [
"bin",
"dist",
"lib",
"template",
"dist"
"template"
],
"author": "Electrode (http://www.electrode.io/)",
"contributors": [
Expand All @@ -33,21 +35,66 @@
"devDependencies": {
"@babel/core": "^7.6.2",
"@babel/preset-env": "^7.6.2",
"@types/chai": "^4.2.11",
"@types/mocha": "^7.0.2",
"@xarc/module-dev": "^2.2.4",
"babel-loader": "^8.0.6",
"chai": "^4.2.0",
"chalker": "^1.2.0",
"lodash": "^4.17.15",
"mocha": "^7.1.0",
"nyc": "^15.0.0",
"opfs": "^1.1.1",
"prompts": "^2.3.2",
"shcmd": "^0.7.9",
"sinon": "^7.2.6",
"sinon-chai": "^3.3.0",
"webpack": "^4.41.0",
"webpack-bundle-analyzer": "^3.5.2",
"webpack-cli": "^3.3.9",
"xclap": "^0.2.51"
"xclap": "^0.2.51",
"run-verify": "^1.2.5"
},
"prettier": {
"printWidth": 100,
"arrowParens": "avoid",
"trailingComma": "none"
},
"dependencies": {}
"mocha": {
"require": [
"./test/setup.js"
],
"recursive": true
},
"nyc": {
"extends": [],
"all": true,
"reporter": [
"lcov",
"text",
"text-summary"
],
"include": [
"src"
],
"exclude": [
"*clap.js",
"*clap.ts",
"coverage",
"dist",
"docs",
"gulpfile.js",
"test",
"xrun*.js",
"xrun*.ts",
"template",
"bin"
],
"check-coverage": true,
"statements": 100,
"branches": 100,
"functions": 100,
"lines": 100,
"cache": true
}
}
Empty file.
3 changes: 3 additions & 0 deletions packages/xarc-create-app/test/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const chai = require("chai")
const sinon = require("sinon");

Empty file.
34 changes: 34 additions & 0 deletions packages/xarc-create-app/test/spec/check-dir.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { expect } = require('chai');
const { runFinally, asyncVerify } = require("run-verify");
const { Transform } = require('stream');
const checkDir = require('../../src/check-dir');
const shcmd = require("shcmd");
const { execSync, spawn } = require('child_process');

describe('checkDir', function () {
const cwd = process.cwd();
afterEach(() => {
process.chdir(cwd);
});

it('checks if directory has existing files', function () {
shcmd.mkdir("test/data/empty-dir");
process.chdir("test/data/empty-dir");
asyncVerify(
() => checkDir(process.cwd()),
(ret) => {
expect(ret).to.equal(true)
}
);
runFinally(() => {
shcmd.rmdir("test/data/empty-dir")
})
});

it('check non empty prompt', async function () {
const output = execSync(`echo 'n' | node -e "require('./src/check-dir')('test/data')"`).toString();
expect(output).to.include("not empty");

});

})
25 changes: 25 additions & 0 deletions packages/xarc-create-app/test/spec/create-app.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const expect = require("chai").expect;
const shcmd = require("shcmd");
const { resolve } = require("path");
const { execSync } = require("child_process");
describe('create app', () => {
beforeEach(() => {
process.chdir(resolve(__dirname, "../../"));

shcmd.mkdir("test/data/create-app-unit-test");
process.chdir("test/data/create-app-unit-test");

});

it('precondition: directory is created, and create app in cwd', () => {
shcmd.exec("echo 'y' | node ../../../src/create-app");

})
afterEach(() => {
process.chdir(resolve(__dirname, "../../"));

execSync("rm -rf test/data/create-app-unit-test");

});

})
26 changes: 26 additions & 0 deletions packages/xarc-create-app/test/spec/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const expect = require("chai").expect;
const shcmd = require("shcmd");
const { resolve } = require("path");
const { execSync } = require("child_process");

describe('index', function () {
it('creates directory and then app in it', function () {
process.chdir(resolve(__dirname, "../data"));
shcmd.exec("echo 'y' | node ../../src/index new-app");
const output = shcmd.exec("ls new-app").stdout;

expect(output).to.include("src");
expect(output).to.include("package.json");
expect(output).to.include("static");
expect(output).to.include("xclap");
execSync("rm -rf new-app");
process.chdir("..");
}).timeout(10000);

it("aborts for non-empty directory", function () {
process.chdir(resolve(__dirname, "../.."));
shcmd.exec("echo 'n' | node ./src/index node_modules");

})
})
//require("../../src/");
76 changes: 76 additions & 0 deletions packages/xarc-create-app/test/spec/prep-app-dir.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

const { expect, should } = require("chai");
const { spawn, execSync } = require("child_process");
const Fs = require('fs')
require("chai")
describe('prepAppDir', function () {
this.beforeEach(() => {
process.chdir(require("path").resolve(__dirname, '../..'))
})
const callSubroutineWithArgv2 = arg => {
return spawn("node", ["-e", `process.argv[2]='${arg}'; require('./src/prep-app-dir')()`]);
}
it('requires process.argv2', async function () {
const routine = callSubroutineWithArgv2("");
var output = ""
routine.stdout.on("data", function (d) {
output += d.toString();
})

routine.on("exit", () => {
expect(output).to.include("pass '.' to use current directory")
});
});


it('creates directory with name from process.argv2', async function () {
expect(execSync("ls test/data").toString()).to.not.include("hello_app");
callSubroutineWithArgv2("./test/data/hello_app").on("exit", () => {
expect(execSync("ls test/data").toString()).to.include("hello_app");
Fs.rmdirSync("test/data/hello_app");
});
});

it('when it throws a non-EEXIST exception, console logs msg Failed to create..', async function () {
try {
execSync("rm -rf test/data/no-writing && mkdir test/data/no-writing && chmod -w test/data/no-writing");
const routine = callSubroutineWithArgv2("./test/data/no-writing/new-app");
} catch (e) {
expect(e.message).to.include("failed to")
execSync("rm -rf test/data/no-writing");
}


});
it('when process.argv2 ', function () {
// expect(execSync("ls test/data").toString()).to.not.include("hello_app");
const nodeproc = callSubroutineWithArgv2(".");
var output = ""
nodeproc.stdout.on("data", function (d) {
output += d.toString();
})
nodeproc.on("exit", () => {
expect(output).to.include("Using current directory")
});
});
it('when process.argv2 is existing directory', async function () {
// expect(execSync("ls test/data").toString()).to.not.include("hello_app");
await new Promise(resolve => {
const nodeproc = callSubroutineWithArgv2("./test/spec");
var output = ""
nodeproc.stdout.on("data", function (d) {
output += d.toString();
})
nodeproc.on("exit", (cod3) => {
expect(cod3).to.equal(0);
resolve();
});
})
});





});

46 changes: 46 additions & 0 deletions packages/xarc-create-app/test/spec/sort-deps.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const sortDeps = require("../../src/sort-deps");
const { resolve } = require("path");
const _ = require("lodash");
const { execSync } = require("child_process");
const { expect } = require("chai");
const { unlink } = require("fs");
const { doesNotReject } = require("assert");

const xrequire = eval("require");

describe('sort-deps', function () {
this.beforeEach(() => {
process.chdir(resolve(__dirname, "../.."));
})
it('sorts packages by keys', async function () {

await new Promise(resolve => {
const pkg = xrequire(resolve(__dirname, "../../template/_package"))({}, _.merge);
sortDeps(pkg);
const sortedDeps = pkg.dependencies;
const sortedStr = Object.keys(sortedDeps).join("\n");
const qaSortedStr = execSync(`echo '${sortedStr}' > temp.txt && cat temp.txt |sort`).toString();
expect(sortedStr.trim()).to.equal(qaSortedStr.trim());
unlink('temp.txt', () => {
resolve();
});
})
});

it("skip undefined sections", async () => {
await new Promise(resolve => {

const pkg = {
dependencies: { "a": 1, "b": 1 }
};
sortDeps(pkg);
const sortedDeps = pkg.dependencies;
const sortedStr = Object.keys(sortedDeps).join("\n");
const qaSortedStr = execSync(`echo '${sortedStr}' > temp.txt && cat temp.txt |sort`).toString();
expect(sortedStr.trim()).to.equal(qaSortedStr.trim());
unlink('temp.txt', () => {
resolve();
});
});
});
})
2 changes: 2 additions & 0 deletions packages/xarc-create-app/xrun-tasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { loadTasks } from "@xarc/module-dev";
loadTasks();

0 comments on commit c7f1f03

Please sign in to comment.