Skip to content

Commit

Permalink
Add tests for generateApiIndex task and processor
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomByte committed Mar 20, 2019
1 parent 413ccf0 commit 5905a2e
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/processors/jsdoc/apiIndexGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const createIndex = require("./lib/create-api-index");
const apiIndexGenerator = async function({
versionInfoPath, testResourcesRootPath, targetApiIndexPath, targetApiIndexDeprecatedPath,
targetApiIndexExperimentalPath, targetApiIndexSincePath, fs
}) {
} = {}) {
if (!versionInfoPath || !testResourcesRootPath || !targetApiIndexPath || !targetApiIndexDeprecatedPath ||
!targetApiIndexExperimentalPath || !targetApiIndexSincePath || !fs) {
throw new Error("[apiIndexGenerator]: One or more mandatory parameters not provided");
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/jsdoc/generateApiIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const apiIndexGenerator = require("../../processors/jsdoc/apiIndexGenerator");
* @param {string} parameters.options.projectName Project name
* @returns {Promise<undefined>} Promise resolving with <code>undefined</code> once data has been written
*/
module.exports = async function({workspace, dependencies, options}) {
module.exports = async function({workspace, dependencies, options} = {}) {
if (!options || !options.projectName) {
throw new Error("[generateApiIndex]: One or more mandatory options not provided");
}
Expand Down
85 changes: 85 additions & 0 deletions test/lib/processors/jsdoc/apiIndexGenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const {test} = require("ava");
const sinon = require("sinon");
const mock = require("mock-require");
const apiIndexGenerator = require("../../../../lib/processors/jsdoc/apiIndexGenerator");

test.afterEach.always((t) => {
sinon.restore();
});

test.serial("apiIndexGenerator", async (t) => {
const createApiIndexStub = sinon.stub().resolves({
"/some/path/api-index.json": "resource content A",
"/some/path/api-index-deprecated.json": "resource content B",
"/some/path/api-index-experimental.json": "resource content C",
"/some/path/api-index-since.json": "resource content D"
});
mock("../../../../lib/processors/jsdoc/lib/create-api-index", createApiIndexStub);
const createResourceStub = sinon.stub(require("@ui5/fs").resourceFactory, "createResource")
.onCall(0).returns("result resource A")
.onCall(1).returns("result resource B")
.onCall(2).returns("result resource C")
.onCall(3).returns("result resource D");

const apiIndexGenerator = mock.reRequire("../../../../lib/processors/jsdoc/apiIndexGenerator");

const res = await apiIndexGenerator({
versionInfoPath: "/some/path/sap-ui-version.json",
testResourcesRootPath: "/some/test-resources/path",
targetApiIndexPath: "/some/path/api-index.json",
targetApiIndexDeprecatedPath: "/some/path/api-index-deprecated.json",
targetApiIndexExperimentalPath: "/some/path/api-index-experimental.json",
targetApiIndexSincePath: "/some/path/api-index-since.json",
fs: "custom fs"
});

t.deepEqual(res.length, 4, "Returned one resource");
t.deepEqual(res[0], "result resource A", "Returned correct resource");
t.deepEqual(res[1], "result resource B", "Returned correct resource");
t.deepEqual(res[2], "result resource C", "Returned correct resource");
t.deepEqual(res[3], "result resource D", "Returned correct resource");

t.deepEqual(createApiIndexStub.callCount, 1, "create-api-index called once");
t.deepEqual(createApiIndexStub.getCall(0).args[0], "/some/path/sap-ui-version.json",
"create-api-index called with correct argument #1");
t.deepEqual(createApiIndexStub.getCall(0).args[1], "/some/test-resources/path",
"create-api-index called with correct argument #2");
t.deepEqual(createApiIndexStub.getCall(0).args[2], "/some/path/api-index.json",
"create-api-index called with correct argument #3");
t.deepEqual(createApiIndexStub.getCall(0).args[3], "/some/path/api-index-deprecated.json",
"create-api-index called with correct argument #4");
t.deepEqual(createApiIndexStub.getCall(0).args[4], "/some/path/api-index-experimental.json",
"create-api-index called with correct argument #5");
t.deepEqual(createApiIndexStub.getCall(0).args[5], "/some/path/api-index-since.json",
"create-api-index called with correct argument #6");
t.deepEqual(createApiIndexStub.getCall(0).args[6], {
fs: "custom fs",
returnOutputFiles: true
}, "create-api-index called with correct argument #7");

t.deepEqual(createResourceStub.callCount, 4, "createResource called once");
t.deepEqual(createResourceStub.getCall(0).args[0], {
path: "/some/path/api-index.json",
string: "resource content A"
}, "createResource called with correct arguments for resource 1");
t.deepEqual(createResourceStub.getCall(1).args[0], {
path: "/some/path/api-index-deprecated.json",
string: "resource content B"
}, "createResource called with correct arguments for resource 2");
t.deepEqual(createResourceStub.getCall(2).args[0], {
path: "/some/path/api-index-experimental.json",
string: "resource content C"
}, "createResource called with correct arguments for resource 3");
t.deepEqual(createResourceStub.getCall(3).args[0], {
path: "/some/path/api-index-since.json",
string: "resource content D"
}, "createResource called with correct arguments for resource 4");

mock.stop("../../../../lib/processors/jsdoc/lib/create-api-index");
});

test("apiIndexGenerator missing parameters", async (t) => {
const error = await t.throws(apiIndexGenerator());
t.deepEqual(error.message, "[apiIndexGenerator]: One or more mandatory parameters not provided",
"Correct error message thrown");
});
74 changes: 74 additions & 0 deletions test/lib/tasks/jsdoc/generateApiIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const {test} = require("ava");
const sinon = require("sinon");
const ui5Fs = require("@ui5/fs");

const mock = require("mock-require");

const generateApiIndex = require("../../../../lib/tasks/jsdoc/generateApiIndex");

test.afterEach.always((t) => {
sinon.restore();
});

test.serial("generateApiIndex", async (t) => {
const apiIndexGeneratorStub = sinon.stub().resolves(["resource A", "resource B"]);
const fsInterfaceStub = sinon.stub(ui5Fs, "fsInterface").returns("custom fs");
mock("../../../../lib/processors/jsdoc/apiIndexGenerator", apiIndexGeneratorStub);

class ReaderCollectionPrioritizedStubClass {
constructor(parameters) {
t.deepEqual(parameters, {
name: "generateApiIndex - workspace + dependencies: some.project",
readers: [workspace, dependencies]
}, "ReaderCollectionPrioritized got called with the correct arguments");
}
}
const readerCollectionPrioritizedStub = ReaderCollectionPrioritizedStubClass;
const readerCollectionPrioritizedOrig = ui5Fs.ReaderCollectionPrioritized;
ui5Fs.ReaderCollectionPrioritized = readerCollectionPrioritizedStub;
const generateApiIndex = mock.reRequire("../../../../lib/tasks/jsdoc/generateApiIndex");
ui5Fs.ReaderCollectionPrioritized = readerCollectionPrioritizedOrig;

const writeStub = sinon.stub().resolves();
const workspace = {
write: writeStub
};
const dependencies = {};
await generateApiIndex({
workspace,
dependencies,
options: {
projectName: "some.project"
}
});

t.deepEqual(fsInterfaceStub.callCount, 1, "fsInterface got called once");
t.true(fsInterfaceStub.getCall(0).args[0] instanceof ReaderCollectionPrioritizedStubClass,
"fsInterface got called with an instance of ReaderCollectionPrioritizedStubClass");

t.deepEqual(apiIndexGeneratorStub.callCount, 1, "apiIndexGenerator processor got called once");
t.deepEqual(apiIndexGeneratorStub.getCall(0).args[0], {
versionInfoPath: "/resources/sap-ui-version.json",
testResourcesRootPath: "/test-resources",
targetApiIndexPath: "/docs/api/api-index.json",
targetApiIndexDeprecatedPath: "/docs/api/api-index-deprecated.json",
targetApiIndexExperimentalPath: "/docs/api/api-index-experimental.json",
targetApiIndexSincePath: "/docs/api/api-index-since.json",
fs: "custom fs"
}, "apiIndexGenerator got called with correct arguments");

t.deepEqual(writeStub.callCount, 2, "Write got called twice");
t.deepEqual(writeStub.getCall(0).args[0], "resource A", "Write got called with correct arguments");
t.deepEqual(writeStub.getCall(1).args[0], "resource B", "Write got called with correct arguments");

mock.stop("../../../../lib/processors/jsdoc/apiIndexGenerator");

sinon.restore();
mock.reRequire("../../../../lib/tasks/jsdoc/generateApiIndex");
});

test("generateApiIndex with missing parameters", async (t) => {
const error = await t.throws(generateApiIndex());
t.deepEqual(error.message, "[generateApiIndex]: One or more mandatory options not provided",
"Correct error message thrown");
});

0 comments on commit 5905a2e

Please sign in to comment.