Skip to content

Commit

Permalink
test(openapi-v3-types): improve test coverage
Browse files Browse the repository at this point in the history
connected to #181
  • Loading branch information
virkt25 committed Apr 11, 2018
1 parent 3db07eb commit d41a251
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/openapi-v3-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
},
"devDependencies": {
"@loopback/build": "^0.4.2",
"@loopback/testlab": "^0.5.3",
"@types/node": "^8.10.4"
},
"scripts": {
"build": "lb-tsc es2017",
"build:apidocs": "lb-apidocs",
"clean": "lb-clean loopback-openapi-v3-types*.tgz dist package api-docs",
"prepublishOnly": "npm run build && npm run build:apidocs",
"verify": "npm pack && tar xf loopback-openapi-v3-types*.tgz && tree package && npm run clean"
"verify": "npm pack && tar xf loopback-openapi-v3-types*.tgz && tree package && npm run clean",
"pretest": "npm run build",
"test": "lb-mocha \"DIST/test/**/*.js\"",
"unit": "npm run build && lb-mocha \"DIST/test/unit/**/*.js\""
},
"author": "IBM",
"copyright.owner": "IBM Corp.",
Expand Down
102 changes: 102 additions & 0 deletions packages/openapi-v3-types/test/unit/openapi-v3-spec-types.unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/openapi-v3-types
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {expect} from '@loopback/testlab';
import {
ExampleObject,
ReferenceObject,
DiscriminatorObject,
XMLObject,
ExternalDocumentationObject,
ISpecificationExtension,
createEmptyApiSpec,
OpenAPIObject,
} from '../..';

describe('openapi-v3-types unit tests', () => {
describe('createEmptyApiSpec', () => {
let emptySpec: OpenAPIObject;

beforeEach(createEmptySpec);

it('sets version 3 for an empty spec', () => {
expect(emptySpec.openapi).to.equal('3.0.0');
});

it('sets the spec info object', () => {
expect(emptySpec.info).to.be.an.Object();
expect(emptySpec.info.title).to.equal('LoopBack Application');
expect(emptySpec.info.version).to.equal('1.0.0');
});

it('creates an empty paths object', () => {
expect(emptySpec.paths).to.be.an.Object();
expect(emptySpec.paths).to.be.empty();
});

it('creates a default servers array', () => {
expect(emptySpec.servers).to.be.an.Array();
expect(emptySpec.servers).to.have.lengthOf(1);
const server = emptySpec.servers ? emptySpec.servers[0] : undefined;
expect(server).to.not.be.Undefined();
const serverUrl = server ? server.url : '';
expect(serverUrl).to.equal('/');
});

function createEmptySpec() {
emptySpec = createEmptyApiSpec();
}
});

describe('interfaces', () => {
/**
* The classes below are declared as tests for the Interfaces. The TS Compiler
* will complain if an interface changes with a way inconsistent with the
* original OAS 3 definition. (Though some interfaces allow for extensibility).
*/

// tslint:disable-next-line:no-unused-variable
class TestObject implements ExampleObject {
summary: 'test object';
description: 'test object';
value: 1;
externalValue: '1';
randomProperty: 'extension value';
}

// tslint:disable-next-line:no-unused-variable
class ReferenceTestObject implements ReferenceObject {
$ref: '#def/reference-object';
}

// tslint:disable-next-line:no-unused-variable
class DiscriminatorTestObject implements DiscriminatorObject {
propertyName: 'test';
mapping: {
hello: 'world';
};
}

// tslint:disable-next-line:no-unused-variable
class XMLTestObject implements XMLObject {
name: 'test';
namespace: 'test';
prefix: 'test';
attribute: false;
wrapped: false;
}

// tslint:disable-next-line:no-unused-variable
class TestExternalDocumentationObject
implements ExternalDocumentationObject {
url: 'https://test.com/test.html';
}

// tslint:disable-next-line:no-unused-variable
class TestISpecificationExtension implements ISpecificationExtension {
test: 'test';
}
});
});
43 changes: 43 additions & 0 deletions packages/openapi-v3-types/test/unit/type-guards.unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/openapi-v3-types
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {expect} from '@loopback/testlab';
import {
SchemaObject,
isSchemaObject,
ReferenceObject,
isReferenceObject,
} from '../../src';

describe('type-guards unit tests', () => {
describe('isSchemaObject()', () => {
it('returns true for a schema object', () => {
const schemaObject = new TestSchemaObject();
expect(isSchemaObject(schemaObject)).to.be.True();
});

it('returns false for a reference object', () => {
const referenceObject = new TestReferenceObject();
expect(isSchemaObject(referenceObject)).to.be.False();
});
});

describe('isReferenceObject()', () => {
it('returns true for a reference object', () => {
const referenceObject = new TestReferenceObject();
expect(isReferenceObject(referenceObject)).to.be.True();
});

it('returns false for a schema object', () => {
const schemaObject = new TestSchemaObject();
expect(isReferenceObject(schemaObject)).to.be.False();
});
});

class TestSchemaObject implements SchemaObject {}
class TestReferenceObject implements ReferenceObject {
$ref = 'test';
}
});
1 change: 0 additions & 1 deletion packages/testlab/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"copyright.owner": "IBM Corp.",
"license": "MIT",
"dependencies": {
"@loopback/openapi-v3-types": "^0.3.3",
"@types/fs-extra": "^5.0.1",
"@types/shot": "^3.4.0",
"@types/sinon": "^4.3.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/testlab/src/validate-api-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {OpenApiSpec} from '@loopback/openapi-v3-types';
const validator = require('swagger2openapi/validate.js');
import {promisify} from 'util';

const validateAsync = promisify(validator.validate);

export async function validateApiSpec(spec: OpenApiSpec): Promise<void> {
// tslint:disable-next-line:no-any
export async function validateApiSpec(spec: any): Promise<void> {
const opts = {};

try {
Expand Down

0 comments on commit d41a251

Please sign in to comment.