Skip to content

Commit

Permalink
refactor(buildDocs): consolidate setupDocs (#214)
Browse files Browse the repository at this point in the history
* index, move setupDocs, case CACHE
* buildDocs, consolidate setup, build docs funcs
  • Loading branch information
cdcabrera committed Mar 8, 2023
1 parent 2ed4c8f commit 85b173d
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 184 deletions.
46 changes: 0 additions & 46 deletions src/__tests__/__snapshots__/index.test.js.snap

This file was deleted.

81 changes: 1 addition & 80 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,15 @@
const { apiDocMock, setupDocs, setupResponse } = require('../');
const { OPTIONS } = require('../global');
const { apiDocMock, setupResponse } = require('../');

describe('ApiDocMock', () => {
it('should have specific defined properties', () => {
expect(apiDocMock).toBeDefined();
expect(setupDocs).toBeDefined();
expect(setupResponse).toBeDefined();
});

it('should have support functions that fail gracefully', () => {
expect(setupResponse()).toBe(null);
});

it('should setup api docs and create a predictable output', () => {
const apiFixture = generateFixture(
`/**
* @api {get} /hello/world/
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "success": "test"
* }
* @apiErrorExample {json} Error-Response:
* HTTP/1.1 400 OK
* {
* "error": "test"
* }
*/`,
{ dir: './.fixtures/predictable', filename: 'test.js' }
);

const [helloWorld] = setupDocs({ ...OPTIONS, watchPath: [apiFixture.dir], docsPath: 'lorem-ipsum' });

expect({
...helloWorld,
success: JSON.stringify(helloWorld.success),
error: JSON.stringify(helloWorld.error),
group: undefined,
groupTitle: undefined
}).toMatchSnapshot('setupDocs');
});

it('should handle additional response content types', () => {
const htmlFixture = generateFixture(
`/**
* @api {get} /hello/world/html.html
* @apiSuccessExample {html} Success-Response:
* HTTP/1.1 200 OK
* <!DOCTYPE html>
* <html>
* <head>hello</head>
* <body>world</body>
* </html>
*/`,
{ dir: './.fixtures/content-types', filename: 'html.js' }
);

generateFixture(
`/**
* @api {get} /hello/world/svg.svg
* @apiSuccessExample {svg} Success-Response:
* HTTP/1.1 200 OK
* <?xml version="1.0" encoding="utf-8"?>
* <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
* <svg version="1.1" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="10px" height="10px" viewBox="0 0 10 10">
* <g/>
* </svg>
*/`,
{ dir: './.fixtures/content-types', filename: 'svg.js', resetDir: false }
);

const [html, svg] = setupDocs({ ...OPTIONS, watchPath: [htmlFixture.dir], docsPath: 'lorem-ipsum' });

expect({
...html,
success: JSON.stringify(html.success),
error: JSON.stringify(html.error),
group: undefined,
groupTitle: undefined
}).toMatchSnapshot('html mock');

expect({
...svg,
success: JSON.stringify(svg.success),
error: JSON.stringify(svg.error),
group: undefined,
groupTitle: undefined
}).toMatchSnapshot('svg mock');
});

it('should throw an error during testing', async () => {
const func = async () => apiDocMock();
await expect(func).rejects.toThrow('Server failed to load');
Expand Down
47 changes: 46 additions & 1 deletion src/docs/__tests__/__snapshots__/buildDocs.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,48 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`BuildDocs should fail a build gracefully: fail 1`] = `{}`;
exports[`BuildDocs should fail a build gracefully: fail 1`] = `[]`;

exports[`BuildDocs should handle additional response content types: html mock 1`] = `
{
"error": undefined,
"filename": "html.js",
"group": undefined,
"groupTitle": undefined,
"name": "GetHelloWorldHtmlHtml",
"success": "{"examples":[{"title":"Success-Response:","content":"HTTP/1.1 200 OK\\n<!DOCTYPE html>\\n<html>\\n <head>hello</head>\\n <body>world</body>\\n</html>","type":"html"}]}",
"title": "",
"type": "get",
"url": "/hello/world/html.html",
"version": "0.0.0",
}
`;

exports[`BuildDocs should handle additional response content types: svg mock 1`] = `
{
"error": undefined,
"filename": "svg.js",
"group": undefined,
"groupTitle": undefined,
"name": "GetHelloWorldSvgSvg",
"success": "{"examples":[{"title":"Success-Response:","content":"HTTP/1.1 200 OK\\n<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>\\n<!DOCTYPE svg PUBLIC \\"-//W3C//DTD SVG 1.1//EN\\" \\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\\">\\n<svg version=\\"1.1\\" xmlns=\\"http://www.w3.org/2000/svg\\" x=\\"0px\\" y=\\"0px\\" width=\\"10px\\" height=\\"10px\\" viewBox=\\"0 0 10 10\\">\\n<g/>\\n</svg>","type":"svg"}]}",
"title": "",
"type": "get",
"url": "/hello/world/svg.svg",
"version": "0.0.0",
}
`;

exports[`BuildDocs should setup api docs and create a predictable output: setupDocs 1`] = `
{
"error": "{"examples":[{"title":"Error-Response:","content":"HTTP/1.1 400 OK\\n{\\n \\"error\\": \\"test\\"\\n}","type":"json"}]}",
"filename": "test.js",
"group": undefined,
"groupTitle": undefined,
"name": "GetHelloWorld",
"success": "{"examples":[{"title":"Success-Response:","content":"HTTP/1.1 200 OK\\n{\\n \\"success\\": \\"test\\"\\n}","type":"json"}]}",
"title": "",
"type": "get",
"url": "/hello/world/",
"version": "0.0.0",
}
`;
84 changes: 81 additions & 3 deletions src/docs/__tests__/buildDocs.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,89 @@
const { buildDocs } = require('../buildDocs');
const { setupDocs } = require('../buildDocs');
const { OPTIONS } = require('../../global');

describe('BuildDocs', () => {
it('should have specific defined properties', () => {
expect(buildDocs).toBeDefined();
expect(setupDocs()).toBeDefined();
});

it('should fail a build gracefully', () => {
expect(buildDocs({})).toMatchSnapshot('fail');
expect(setupDocs({})).toMatchSnapshot('fail');
});

it('should setup api docs and create a predictable output', () => {
const apiFixture = generateFixture(
`/**
* @api {get} /hello/world/
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "success": "test"
* }
* @apiErrorExample {json} Error-Response:
* HTTP/1.1 400 OK
* {
* "error": "test"
* }
*/`,
{ dir: './.fixtures/predictable', filename: 'test.js' }
);

const [helloWorld] = setupDocs({ ...OPTIONS, watchPath: [apiFixture.dir], docsPath: 'lorem-ipsum' });

expect({
...helloWorld,
success: JSON.stringify(helloWorld.success),
error: JSON.stringify(helloWorld.error),
group: undefined,
groupTitle: undefined
}).toMatchSnapshot('setupDocs');
});

it('should handle additional response content types', () => {
const htmlFixture = generateFixture(
`/**
* @api {get} /hello/world/html.html
* @apiSuccessExample {html} Success-Response:
* HTTP/1.1 200 OK
* <!DOCTYPE html>
* <html>
* <head>hello</head>
* <body>world</body>
* </html>
*/`,
{ dir: './.fixtures/content-types', filename: 'html.js' }
);

generateFixture(
`/**
* @api {get} /hello/world/svg.svg
* @apiSuccessExample {svg} Success-Response:
* HTTP/1.1 200 OK
* <?xml version="1.0" encoding="utf-8"?>
* <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
* <svg version="1.1" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="10px" height="10px" viewBox="0 0 10 10">
* <g/>
* </svg>
*/`,
{ dir: './.fixtures/content-types', filename: 'svg.js', resetDir: false }
);

const [html, svg] = setupDocs({ ...OPTIONS, watchPath: [htmlFixture.dir], docsPath: 'lorem-ipsum' });

expect({
...html,
success: JSON.stringify(html.success),
error: JSON.stringify(html.error),
group: undefined,
groupTitle: undefined
}).toMatchSnapshot('html mock');

expect({
...svg,
success: JSON.stringify(svg.success),
error: JSON.stringify(svg.error),
group: undefined,
groupTitle: undefined
}).toMatchSnapshot('svg mock');
});
});
45 changes: 29 additions & 16 deletions src/docs/buildDocs.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
const apidoc = require('apidoc');
const { logger } = require('../logger/configLogger');
const { OPTIONS } = require('../global');

/**
* Compile/build ApiDoc documentation.
* Build ApiDoc documentation.
*
* @param {object} params
* @param {object} params.apiDocsConfig
* @returns {object}
* @param {object} options
* @param {OPTIONS.apiDocBaseConfig} options.apiDocBaseConfig
* @param {string[]} options.watchPath
* @param {string} options.docsPath
* @param {string} options.silent
* @returns {*|{}|null}
*/
const buildDocs = ({ apiDocsConfig = null } = {}) => {
if (apiDocsConfig) {
try {
const { data } = apidoc.createDoc(apiDocsConfig);
const updatedResult = JSON.parse(data);
logger.info('buildDocs.read.apiJsonFile');
return updatedResult;
} catch (e) {
logger.error(`buildDocs.apiDoc.createDoc[${e.message}]`);
}
const setupDocs = ({ apiDocBaseConfig, watchPath: src, docsPath: dest, silent } = OPTIONS) => {
if ((!Array.isArray(src) && !src?.length) || !dest) {
return [];
}

return {};
const apiDocsConfig = {
...apiDocBaseConfig,
src,
dest,
silent: apiDocBaseConfig.silent || silent
};

try {
const { data } = apidoc.createDoc(apiDocsConfig);
const updatedResult = JSON.parse(data);
logger.info('buildDocs.read.apiJsonFile');
return updatedResult;
} catch (e) {
logger.error(`buildDocs.apiDoc.createDoc[${e.message}]`);
}

return [];
};

module.exports = { buildDocs };
module.exports = { setupDocs };
Loading

0 comments on commit 85b173d

Please sign in to comment.