diff --git a/bin/cli/__mocks__/utils.js b/bin/cli/__mocks__/utils.js index ebfc5fd..df0ac70 100644 --- a/bin/cli/__mocks__/utils.js +++ b/bin/cli/__mocks__/utils.js @@ -9,7 +9,7 @@ const config = { name: 'David' } }, - endpoint: 'https://httpbin.org/', + endpoint: 'https://example.org', version: 1, 'GET /anything': { alias: 'anything', @@ -26,6 +26,10 @@ utils.loadConfig = function() { return new Beau(config, {}); }; +utils.openConfigFile = function() { + return config; +}; + utils.baseFlags = original.baseFlags; module.exports = utils; diff --git a/bin/cli/__tests__/__snapshots__/list.spec.js.snap b/bin/cli/__tests__/__snapshots__/list.spec.js.snap index 8a25510..9a9eeec 100644 --- a/bin/cli/__tests__/__snapshots__/list.spec.js.snap +++ b/bin/cli/__tests__/__snapshots__/list.spec.js.snap @@ -2,9 +2,9 @@ exports[`List Command Should disable formatting when the flag is active. 1`] = ` Array [ - "GET anything https://httpbin.org/anything + "GET anything https://example.org/anything ", - "GET teapot https://httpbin.org/status/418 + "GET teapot https://example.org/status/418 ", ] `; @@ -13,9 +13,9 @@ exports[`List Command Should list available requests for a given file. 1`] = ` Array [ " HTTP Verb Alias Endpoint ", - " GET anything https://httpbin.org/anything + " GET anything https://example.org/anything ", - " GET teapot https://httpbin.org/status/418 + " GET teapot https://example.org/status/418 ", " ", diff --git a/bin/cli/__tests__/__snapshots__/request.spec.js.snap b/bin/cli/__tests__/__snapshots__/request.spec.js.snap new file mode 100644 index 0000000..ec4463f --- /dev/null +++ b/bin/cli/__tests__/__snapshots__/request.spec.js.snap @@ -0,0 +1,71 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Request Command Should output an unformatted version 1`] = ` +Array [ + "200 +", + "https://example.org/anything +", + "[] +", + "\\"{\\\\\\"hello\\\\\\": \\\\\\"world\\\\\\"}\\" +", +] +`; + +exports[`Request Command Should output nothing 1`] = `Array []`; + +exports[`Request Command Should output the response as json 1`] = ` +Array [ + "{\\"status\\":200,\\"headers\\":[],\\"body\\":\\"{\\\\\\"hello\\\\\\": \\\\\\"world\\\\\\"}\\"} +", +] +`; + +exports[`Request Command Should output the response as json verboselly 1`] = ` +Array [ + "{\\"request\\":{\\"body\\":{\\"name\\":\\"David\\"},\\"endpoint\\":\\"https://example.org/anything\\"},\\"response\\":{\\"status\\":200,\\"headers\\":[],\\"body\\":\\"{\\\\\\"hello\\\\\\": \\\\\\"world\\\\\\"}\\"},\\"body\\":\\"{\\\\\\"hello\\\\\\": \\\\\\"world\\\\\\"}\\"} +", +] +`; + +exports[`Request Command Should request the given alias 1`] = ` +Array [ + "", + " Status Endpoint +", + " 200 https://example.org/anything +", + " +", + "\\"{\\"hello\\": \\"world\\"}\\" +", +] +`; + +exports[`Request Command Should show all information available when being verbose 1`] = ` +Array [ + "", + " Status Endpoint +", + " 200 https://example.org/anything +", + " +", + "{ + request: { + body: { + name: \\"David\\" + }, + endpoint: \\"https://example.org/anything\\" + }, + response: { + status: 200, + headers: [], + body: \\"{\\"hello\\": \\"world\\"}\\" + }, + body: \\"{\\"hello\\": \\"world\\"}\\" +} +", +] +`; diff --git a/bin/cli/__tests__/__snapshots__/validate.spec.js.snap b/bin/cli/__tests__/__snapshots__/validate.spec.js.snap new file mode 100644 index 0000000..e9faf47 --- /dev/null +++ b/bin/cli/__tests__/__snapshots__/validate.spec.js.snap @@ -0,0 +1,8 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Validate Command Validate the configuration file 1`] = ` +Array [ + "beau.yml is valid. +", +] +`; diff --git a/bin/cli/__tests__/list.spec.js b/bin/cli/__tests__/list.spec.js index 0d10426..e4cf0e2 100644 --- a/bin/cli/__tests__/list.spec.js +++ b/bin/cli/__tests__/list.spec.js @@ -1,5 +1,4 @@ const ListCommand = require('../commands/list'); -const stripAnsi = require('strip-ansi'); jest.mock('../utils'); @@ -11,7 +10,7 @@ describe('List Command', () => { jest .spyOn(process.stdout, 'write') .mockImplementation(val => - result.push(stripAnsi(val.toString('utf8'))) + result.push(require('strip-ansi')(val.toString('utf8'))) ); }); diff --git a/bin/cli/__tests__/request.spec.js b/bin/cli/__tests__/request.spec.js new file mode 100644 index 0000000..c61bddf --- /dev/null +++ b/bin/cli/__tests__/request.spec.js @@ -0,0 +1,55 @@ +const RequestCommand = require('../commands/request'); +const requestPromiseNativeMock = require('request-promise-native'); + +jest.mock('../utils'); + +describe('Request Command', () => { + let result; + + beforeEach(() => { + requestPromiseNativeMock.fail = false; + result = []; + jest + .spyOn(process.stdout, 'write') + .mockImplementation(val => + result.push(require('strip-ansi')(val.toString('utf8'))) + ); + }); + + afterEach(() => jest.restoreAllMocks()); + + it('Should request the given alias', async () => { + await RequestCommand.run(['anything']); + expect(result).toMatchSnapshot(); + }); + + it('Should show all information available when being verbose', async () => { + await RequestCommand.run(['anything', '--verbose']); + expect(result).toMatchSnapshot(); + }); + + it('Should output the response as json', async () => { + await RequestCommand.run(['anything', '--as-json']); + expect(result).toMatchSnapshot(); + }); + + it('Should output the response as json verboselly', async () => { + await RequestCommand.run(['anything', '--as-json', '--verbose']); + expect(result).toMatchSnapshot(); + }); + + it('Should output an unformatted version', async () => { + await RequestCommand.run(['anything', '--no-format']); + expect(result).toMatchSnapshot(); + }); + + it('Should output nothing', async () => { + await RequestCommand.run(['anything', '--quiet']); + expect(result).toMatchSnapshot(); + }); + + it('should thrown an error when the request fails', async () => { + requestPromiseNativeMock.fail = true; + await expect(RequestCommand.run(['anything'])).rejects.toThrow(Error); + }); +}); diff --git a/bin/cli/__tests__/validate.spec.js b/bin/cli/__tests__/validate.spec.js new file mode 100644 index 0000000..4cd4eee --- /dev/null +++ b/bin/cli/__tests__/validate.spec.js @@ -0,0 +1,23 @@ +const ValidateCommand = require('../commands/validate'); + +jest.mock('../utils'); + +describe('Validate Command', () => { + let result; + + beforeEach(() => { + result = []; + jest + .spyOn(process.stdout, 'write') + .mockImplementation(val => + result.push(require('strip-ansi')(val.toString('utf8'))) + ); + }); + + afterEach(() => jest.restoreAllMocks()); + + it('Validate the configuration file', async () => { + await ValidateCommand.run([]); + expect(result).toMatchSnapshot(); + }); +}); diff --git a/bin/cli/commands/request.js b/bin/cli/commands/request.js index e403073..09fdc56 100644 --- a/bin/cli/commands/request.js +++ b/bin/cli/commands/request.js @@ -28,7 +28,7 @@ class RequestCommand extends Command { new Line().output(); - jsome((verbose ? res : body) || null); + this.log(jsome.getColoredString((verbose ? res : body) || null)); } async run() {