Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add methods and tests for serverVariables #476

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/models/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const ChannelParameter = require('./channel-parameter');
const CorrelationId = require('./correlation-id');
const OperationTrait = require('./operation-trait');
const MessageTrait = require('./message-trait');
const ServerVariable = require('./server-variable');

const MixinSpecificationExtensions = require('../mixins/specification-extensions');

Expand Down Expand Up @@ -219,6 +220,31 @@ class Components extends Base {
messageTrait(name) {
return getMapValueOfType(this._json.messageTraits, name, MessageTrait);
}

/**
*
* @returns {Object<string, ServerVariable>}
*/
serverVariables() {
return createMapOfType(this._json.serverVariables, ServerVariable);
}

/**
*
* @returns {boolean}
*/
hasServerVariables() {
return !!this._json.serverVariables;
}

/**
*
* @param {string} name - Name of the server variable.
* @returns {ServerVariable}
*/
serverVariable(name) {
return getMapValueOfType(this._json.serverVariables, name, ServerVariable);
}
}

module.exports = mix(Components, MixinSpecificationExtensions);
52 changes: 52 additions & 0 deletions test/models/components_test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { expect } = require('chai');
const { it } = require('mocha');
danielkocot marked this conversation as resolved.
Show resolved Hide resolved

const Components = require('../../lib/models/components');

Expand Down Expand Up @@ -469,4 +470,55 @@ describe('Components', function() {
assertMixinSpecificationExtensionsInheritance(Components);
});
});

describe('#serverVariables()', function() {
it('should return a map of ServerVariable objects', function() {
const doc = { serverVariables: { test1: {test: 'test1'}, test2: {test: 'test2'} } };
const d = new Components(doc);
expect(typeof d.serverVariables()).to.be.equal('object');
expect(d.serverVariables().test1.constructor.name).to.equal('ServerVariable');
expect(d.serverVariables().test1.json()).to.equal(doc.serverVariables.test1);
expect(d.serverVariables().test2.constructor.name).to.equal('ServerVariable');
expect(d.serverVariables().test2.json()).to.equal(doc.serverVariables.test2);
});

it('should return an empty object if the components field has no defined serverVariables', function() {
const doc = {};
const d = new Components(doc);
expect(typeof d.serverVariables()).to.be.equal('object');
expect(d.serverVariables()).to.deep.equal({});
});
});

describe('#hasServerVariables()', function() {
it('should return a boolean indicating if the components field has serverVariables', function() {
const doc = { serverVariables: { test1: {test: 'test1'}, test2: {test: 'test2'} } };
const docNoSchemas = { servers: {} };
const d = new Components(doc);
const d2 = new Components(docNoSchemas);
expect(d.hasServerVariables()).to.equal(true);
expect(d2.hasServerVariables()).to.equal(false);
});
});

describe('#serverVariable()', function() {
it('should return a specific serverVariable object', function() {
const doc = { serverVariables: { test1: {test: 'test1'}, test2: {test: 'test2'} } };
const d = new Components(doc);
expect(d.serverVariable('test1').constructor.name).to.equal('ServerVariable');
expect(d.serverVariable('test1').json()).to.equal(doc.serverVariables.test1);
});

it('should return null if a serverVariable name is not provided', function() {
const doc = { serverVariables: { test1: {test: 'test1'}, test2: {test: 'test2'} } };
const d = new Components(doc);
expect(d.serverVariable()).to.equal(null);
});

it('should return null if a serverVariable name is not found', function() {
const doc = { serverVariables: { test1: {test: 'test1'}, test2: {test: 'test2'} } };
const d = new Components(doc);
expect(d.serverVariable('not found')).to.equal(null);
});
});
});