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.
This commit is contained in:
David Diaz 2018-05-22 21:35:52 -06:00
parent 9444b9ca61
commit 4036a63b2c
10 changed files with 166 additions and 9 deletions

14
bin/cli/__mocks__/fs.js Normal file
View File

@ -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;

View File

@ -26,8 +26,14 @@ utils.loadConfig = function() {
return new Beau(config, {}); return new Beau(config, {});
}; };
utils.openConfigFile = function() { utils.openConfigFile = function(filename) {
return config; if (filename === 'beau.yml') {
return config;
}
if (filename === 'invalid-conf.yml') {
return { plugins: [{ hello: 1, world: 2 }] };
}
}; };
utils.baseFlags = original.baseFlags; utils.baseFlags = original.baseFlags;

View File

@ -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,
}
`;

View File

@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // 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 [ Array [
"beau.yml is valid. "beau.yml is valid.
", ",

View File

@ -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();
});
});
});

View File

@ -16,8 +16,14 @@ describe('Validate Command', () => {
afterEach(() => jest.restoreAllMocks()); afterEach(() => jest.restoreAllMocks());
it('Validate the configuration file', async () => { it('should validate the configuration file', async () => {
await ValidateCommand.run([]); await ValidateCommand.run([]);
expect(result).toMatchSnapshot(); expect(result).toMatchSnapshot();
}); });
it('should show schema errors', async () => {
await expect(
ValidateCommand.run(['invalid-conf.yml'])
).rejects.toThrow();
});
}); });

View File

@ -1,4 +1,3 @@
const yaml = require('js-yaml');
const Plugins = require('../plugins'); const Plugins = require('../plugins');
const Request = require('../request'); const Request = require('../request');
const RequestCache = require('../requestCache'); const RequestCache = require('../requestCache');

View File

@ -1,6 +1,5 @@
const Request = require('../request'); const Request = require('../request');
const RequestCache = require('../requestCache'); const RequestCache = require('../requestCache');
const RequestList = require('../requestList');
const requestPromiseNativeMock = require('request-promise-native'); const requestPromiseNativeMock = require('request-promise-native');
describe('Request', () => { describe('Request', () => {

View File

@ -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);
});
});

View File

@ -62,9 +62,7 @@ const schema = Joi.object()
const validate = async function(config) { const validate = async function(config) {
try { try {
let results = await Joi.validate(config, schema, { await Joi.validate(config, schema, { allowUnknown: true });
allowUnknown: true
});
return { valid: true }; return { valid: true };
} catch ({ name, details }) { } catch ({ name, details }) {
return { return {