-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(openapi-v3-types): improve test coverage
connected to #181
- Loading branch information
Showing
5 changed files
with
152 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
packages/openapi-v3-types/test/unit/openapi-v3-spec-types.unit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters