From 4036a63b2cf88601c6f1a4215156373f992d4995 Mon Sep 17 00:00:00 2001 From: David Diaz Date: Tue, 22 May 2018 21:35:52 -0600 Subject: [PATCH] Update some old tests. Added missing cases and tests. Most of these are kind useless but I hope I won't have to touch them again. --- bin/cli/__mocks__/fs.js | 14 +++ bin/cli/__mocks__/utils.js | 10 ++- .../__snapshots__/utils.spec.js.snap | 90 +++++++++++++++++++ .../__snapshots__/validate.spec.js.snap | 2 +- bin/cli/__tests__/utils.spec.js | 30 +++++++ bin/cli/__tests__/validate.spec.js | 8 +- src/__tests__/plugins.spec.js | 1 - src/__tests__/request.spec.js | 1 - src/__tests__/schema.spec.js | 15 ++++ src/schema.js | 4 +- 10 files changed, 166 insertions(+), 9 deletions(-) create mode 100644 bin/cli/__mocks__/fs.js create mode 100644 bin/cli/__tests__/__snapshots__/utils.spec.js.snap create mode 100644 bin/cli/__tests__/utils.spec.js create mode 100644 src/__tests__/schema.spec.js diff --git a/bin/cli/__mocks__/fs.js b/bin/cli/__mocks__/fs.js new file mode 100644 index 0000000..ad5f097 --- /dev/null +++ b/bin/cli/__mocks__/fs.js @@ -0,0 +1,14 @@ +const fs = jest.genMockFromModule('fs'); + +fs.existsSync = filename => filename === 'beau.yml'; +fs.readFileSync = () => ` +version: 1 +endpoint: https://example.org/ + +GET /anything: + alias: anything + payload: + name: $env.params.name +`; + +module.exports = fs; diff --git a/bin/cli/__mocks__/utils.js b/bin/cli/__mocks__/utils.js index df0ac70..05def91 100644 --- a/bin/cli/__mocks__/utils.js +++ b/bin/cli/__mocks__/utils.js @@ -26,8 +26,14 @@ utils.loadConfig = function() { return new Beau(config, {}); }; -utils.openConfigFile = function() { - return config; +utils.openConfigFile = function(filename) { + if (filename === 'beau.yml') { + return config; + } + + if (filename === 'invalid-conf.yml') { + return { plugins: [{ hello: 1, world: 2 }] }; + } }; utils.baseFlags = original.baseFlags; diff --git a/bin/cli/__tests__/__snapshots__/utils.spec.js.snap b/bin/cli/__tests__/__snapshots__/utils.spec.js.snap new file mode 100644 index 0000000..fdf0190 --- /dev/null +++ b/bin/cli/__tests__/__snapshots__/utils.spec.js.snap @@ -0,0 +1,90 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`utils loadConfig should load load the config onto Beau 1`] = ` +Config { + "COOKIEJAR": false, + "DEFAULTS": Object {}, + "ENDPOINT": "https://example.org/", + "ENVIRONMENT": Object { + "_": Object {}, + }, + "HOSTS": Array [], + "PLUGINS": Plugins { + "context": Object { + "createReadStream": [Function], + }, + "registry": Object { + "dynamicValues": Array [ + Object { + "fn": [Function], + "name": "createReadStream", + }, + ], + "postRequestModifiers": Array [], + "preRequestModifiers": Array [], + }, + }, + "REQUESTS": Array [ + Object { + "ALIAS": "anything", + "COOKIEJAR": false, + "ENDPOINT": "https://example.org/", + "PAYLOAD": Object { + "name": "$env.params.name", + }, + "REQUEST": "GET /anything", + }, + ], + "VERSION": 1, + "configKeys": Array [ + "VERSION", + "ENDPOINT", + "PLUGINS", + "DEFAULTS", + "ENVIRONMENT", + "HOSTS", + "COOKIEJAR", + ], + "defaultConfigValues": Object { + "COOKIEJAR": false, + "DEFAULTS": Object {}, + "ENDPOINT": "", + "ENVIRONMENT": Object {}, + "HOSTS": Array [], + "PLUGINS": Array [], + "VERSION": 1, + }, + "doc": Object { + "GET /anything": Object { + "alias": "anything", + "payload": Object { + "name": "$env.params.name", + }, + }, + "endpoint": "https://example.org/", + "version": 1, + }, +} +`; + +exports[`utils loadConfig should load params onto the environment 1`] = ` +Object { + "_": Object { + "BYE": "MARS", + "HELLO": "WORLD", + }, +} +`; + +exports[`utils openConfigFile should read and parse the given configuration file. 1`] = ` +Object { + "GET /anything": Object { + "alias": "anything", + "payload": Object { + "name": "$env.params.name", + }, + }, + "endpoint": "https://example.org/", + "version": 1, +} +`; diff --git a/bin/cli/__tests__/__snapshots__/validate.spec.js.snap b/bin/cli/__tests__/__snapshots__/validate.spec.js.snap index e9faf47..f7f7fbb 100644 --- a/bin/cli/__tests__/__snapshots__/validate.spec.js.snap +++ b/bin/cli/__tests__/__snapshots__/validate.spec.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Validate Command Validate the configuration file 1`] = ` +exports[`Validate Command should validate the configuration file 1`] = ` Array [ "beau.yml is valid. ", diff --git a/bin/cli/__tests__/utils.spec.js b/bin/cli/__tests__/utils.spec.js new file mode 100644 index 0000000..ffcde27 --- /dev/null +++ b/bin/cli/__tests__/utils.spec.js @@ -0,0 +1,30 @@ +const utils = require('../utils.js'); + +jest.mock('fs'); + +describe('utils', () => { + describe('openConfigFile', () => { + it('should read and parse the given configuration file.', () => { + expect(utils.openConfigFile('beau.yml')).toMatchSnapshot(); + }); + + it('should thrown if given not given a file', () => { + expect(() => utils.openConfigFile('not-a-file.yml')).toThrow(); + }); + }); + + describe('loadConfig', () => { + it('should load load the config onto Beau', () => { + let beau = utils.loadConfig('beau.yml'); + expect(beau.config).toMatchSnapshot(); + }); + + it('should load params onto the environment', () => { + let beau = utils.loadConfig('beau.yml', [ + 'HELLO=WORLD', + 'BYE=MARS' + ]); + expect(beau.config.ENVIRONMENT).toMatchSnapshot(); + }); + }); +}); diff --git a/bin/cli/__tests__/validate.spec.js b/bin/cli/__tests__/validate.spec.js index 4cd4eee..c563130 100644 --- a/bin/cli/__tests__/validate.spec.js +++ b/bin/cli/__tests__/validate.spec.js @@ -16,8 +16,14 @@ describe('Validate Command', () => { afterEach(() => jest.restoreAllMocks()); - it('Validate the configuration file', async () => { + it('should validate the configuration file', async () => { await ValidateCommand.run([]); expect(result).toMatchSnapshot(); }); + + it('should show schema errors', async () => { + await expect( + ValidateCommand.run(['invalid-conf.yml']) + ).rejects.toThrow(); + }); }); diff --git a/src/__tests__/plugins.spec.js b/src/__tests__/plugins.spec.js index 313b0c0..15ecec1 100644 --- a/src/__tests__/plugins.spec.js +++ b/src/__tests__/plugins.spec.js @@ -1,4 +1,3 @@ -const yaml = require('js-yaml'); const Plugins = require('../plugins'); const Request = require('../request'); const RequestCache = require('../requestCache'); diff --git a/src/__tests__/request.spec.js b/src/__tests__/request.spec.js index 79eeac1..4c111aa 100644 --- a/src/__tests__/request.spec.js +++ b/src/__tests__/request.spec.js @@ -1,6 +1,5 @@ const Request = require('../request'); const RequestCache = require('../requestCache'); -const RequestList = require('../requestList'); const requestPromiseNativeMock = require('request-promise-native'); describe('Request', () => { diff --git a/src/__tests__/schema.spec.js b/src/__tests__/schema.spec.js new file mode 100644 index 0000000..c4e818b --- /dev/null +++ b/src/__tests__/schema.spec.js @@ -0,0 +1,15 @@ +const schema = require('../schema'); + +describe('Schema', () => { + it(`should validate an object against the schema`, async () => { + await expect( + schema.validate({ endpoint: 'http://example.com' }) + ).resolves.toHaveProperty('valid', true); + }); + + it(`should indicate the error when an schema is invalid`, async () => { + await expect( + schema.validate({ plugins: [{ hello: 1, world: 2 }] }) + ).resolves.toHaveProperty('valid', false); + }); +}); diff --git a/src/schema.js b/src/schema.js index 2ad67fd..7c1a3fc 100644 --- a/src/schema.js +++ b/src/schema.js @@ -62,9 +62,7 @@ const schema = Joi.object() const validate = async function(config) { try { - let results = await Joi.validate(config, schema, { - allowUnknown: true - }); + await Joi.validate(config, schema, { allowUnknown: true }); return { valid: true }; } catch ({ name, details }) { return {