Skip to content

Commit

Permalink
DEV expand unit test, setupDocs
Browse files Browse the repository at this point in the history
* testing, expand setupDocs
  • Loading branch information
cdcabrera committed Aug 8, 2019
1 parent f56d5e4 commit f2b149b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ api-debug*
# misc
.env
.docs
.fixtures
9 changes: 9 additions & 0 deletions src/__tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ApiDocMock should create a predictable docs output: setupDocs 1`] = `
Object {
"error": "{\\"examples\\":[{\\"title\\":\\"Error-Response:\\",\\"content\\":\\"HTTP/1.1 400 OK\\\\n{\\\\n error: test\\\\n}\\",\\"type\\":\\"json\\"}]}",
"success": "{\\"examples\\":[{\\"title\\":\\"Success-Response:\\",\\"content\\":\\"HTTP/1.1 200 OK\\\\n{\\\\n success: test\\\\n}\\",\\"type\\":\\"json\\"}]}",
"type": "get",
"url": "/hello/world/",
}
`;

exports[`ApiDocMock should throw an error during testing: error 1`] = `[Error: Server failed to load]`;
35 changes: 33 additions & 2 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
const { execSync } = require('child_process');
const { apiDocMock, setupDocs, setupResponse } = require('../');

describe('ApiDocMock', () => {
const tempFile = contents => {
const dir = './.fixtures';
const stdout = execSync(`mkdir -p ${dir}; echo "${contents}" > ${dir}/test.js`);
return { dir, stdout };
};

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

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

it('should create a predictable docs output', () => {
const { dir } = tempFile(`/**
* @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"
* }
*/`);

const [helloWorld] = setupDocs(dir, './.docs');

expect({
type: helloWorld.type,
url: helloWorld.url,
success: JSON.stringify(helloWorld.success),
error: JSON.stringify(helloWorld.error)
}).toMatchSnapshot('setupDocs');
});

it('should throw an error during testing', () => {
try {
apiDocMock({});
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const setupDocs = (dataPath = '', docsPath = '') => {
dest,
parsers: {
apimock: path.join(__dirname, './docs/configDocs.js')
}
},
silent: process.env.NODE_ENV === 'test'
};

const apiJsonFile = path.join(cwd, docsPath, 'api_data.json');
Expand Down

0 comments on commit f2b149b

Please sign in to comment.