From d41a251a5987904aa1e4cba0c193178f094aa98b Mon Sep 17 00:00:00 2001 From: Taranveer Virk Date: Mon, 9 Apr 2018 20:15:31 -0400 Subject: [PATCH] test(openapi-v3-types): improve test coverage connected to #181 --- packages/openapi-v3-types/package.json | 6 +- .../test/unit/openapi-v3-spec-types.unit.ts | 102 ++++++++++++++++++ .../test/unit/type-guards.unit.ts | 43 ++++++++ packages/testlab/package.json | 1 - packages/testlab/src/validate-api-spec.ts | 4 +- 5 files changed, 152 insertions(+), 4 deletions(-) create mode 100644 packages/openapi-v3-types/test/unit/openapi-v3-spec-types.unit.ts create mode 100644 packages/openapi-v3-types/test/unit/type-guards.unit.ts diff --git a/packages/openapi-v3-types/package.json b/packages/openapi-v3-types/package.json index 6dd26ee53ff0..b33db0da9723 100644 --- a/packages/openapi-v3-types/package.json +++ b/packages/openapi-v3-types/package.json @@ -10,6 +10,7 @@ }, "devDependencies": { "@loopback/build": "^0.4.2", + "@loopback/testlab": "^0.5.3", "@types/node": "^8.10.4" }, "scripts": { @@ -17,7 +18,10 @@ "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.", diff --git a/packages/openapi-v3-types/test/unit/openapi-v3-spec-types.unit.ts b/packages/openapi-v3-types/test/unit/openapi-v3-spec-types.unit.ts new file mode 100644 index 000000000000..55fb62807152 --- /dev/null +++ b/packages/openapi-v3-types/test/unit/openapi-v3-spec-types.unit.ts @@ -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'; + } + }); +}); diff --git a/packages/openapi-v3-types/test/unit/type-guards.unit.ts b/packages/openapi-v3-types/test/unit/type-guards.unit.ts new file mode 100644 index 000000000000..f5284048d0fb --- /dev/null +++ b/packages/openapi-v3-types/test/unit/type-guards.unit.ts @@ -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'; + } +}); diff --git a/packages/testlab/package.json b/packages/testlab/package.json index aa7109947b00..1fc9342f23c8 100644 --- a/packages/testlab/package.json +++ b/packages/testlab/package.json @@ -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", diff --git a/packages/testlab/src/validate-api-spec.ts b/packages/testlab/src/validate-api-spec.ts index f646066dfe56..67029a01764a 100644 --- a/packages/testlab/src/validate-api-spec.ts +++ b/packages/testlab/src/validate-api-spec.ts @@ -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 { +// tslint:disable-next-line:no-any +export async function validateApiSpec(spec: any): Promise { const opts = {}; try {