Skip to content

Commit 337ff33

Browse files
committed
Add new tests for fields
1 parent ed2fdfa commit 337ff33

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

test/unit/definitionGenerator.spec.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,101 @@ describe('DefinitionGenerator', () => {
174174
expect(definitionGenerator.openAPI.info).to.be.an('object')
175175
expect(v4.test(definitionGenerator.openAPI.info.version)).to.be.true
176176
});
177+
178+
it('should assign a contact Object when a contact object is included', function() {
179+
mockServerless.service.custom.documentation.contact = {
180+
name: 'John',
181+
url: 'http://example.com',
182+
email: 'john@example.com'
183+
}
184+
const definitionGenerator = new DefinitionGenerator(mockServerless)
185+
definitionGenerator.createInfo()
186+
187+
expect(definitionGenerator.openAPI).to.be.an('object')
188+
expect(definitionGenerator.openAPI.info).to.be.an('object')
189+
expect(definitionGenerator.openAPI.info).to.have.property('contact')
190+
expect(definitionGenerator.openAPI.info.contact).to.be.an('object')
191+
expect(definitionGenerator.openAPI.info.contact.name).to.be.an('string')
192+
});
193+
194+
it('should only assign a contact url if one is provided', function() {
195+
mockServerless.service.custom.documentation.contact = {
196+
name: 'John',
197+
email: 'john@example.com'
198+
}
199+
const definitionGenerator = new DefinitionGenerator(mockServerless)
200+
definitionGenerator.createInfo()
201+
202+
expect(definitionGenerator.openAPI).to.be.an('object')
203+
expect(definitionGenerator.openAPI.info).to.be.an('object')
204+
expect(definitionGenerator.openAPI.info).to.have.property('contact')
205+
expect(definitionGenerator.openAPI.info.contact).to.be.an('object')
206+
expect(definitionGenerator.openAPI.info.contact.name).to.be.an('string')
207+
expect(definitionGenerator.openAPI.info.contact).to.not.have.property('url')
208+
});
209+
210+
it('should assign a license Object when a license object is included with a name', function() {
211+
mockServerless.service.custom.documentation.license = {
212+
name: 'Apache 2.0',
213+
url: 'https://www.apache.org/licenses/LICENSE-2.0.html',
214+
}
215+
const definitionGenerator = new DefinitionGenerator(mockServerless)
216+
definitionGenerator.createInfo()
217+
218+
expect(definitionGenerator.openAPI).to.be.an('object')
219+
expect(definitionGenerator.openAPI.info).to.be.an('object')
220+
expect(definitionGenerator.openAPI.info).to.have.property('license')
221+
expect(definitionGenerator.openAPI.info.license).to.be.an('object')
222+
expect(definitionGenerator.openAPI.info.license.name).to.be.an('string')
223+
});
224+
225+
it('should not assign a license Object when a license object is included without a name', function() {
226+
mockServerless.service.custom.documentation.license = {
227+
url: 'https://www.apache.org/licenses/LICENSE-2.0.html',
228+
}
229+
const definitionGenerator = new DefinitionGenerator(mockServerless)
230+
definitionGenerator.createInfo()
231+
232+
expect(definitionGenerator.openAPI).to.be.an('object')
233+
expect(definitionGenerator.openAPI.info).to.be.an('object')
234+
expect(definitionGenerator.openAPI.info).to.not.have.property('license')
235+
});
236+
237+
it('should only assign a contact url if one is provided', function() {
238+
mockServerless.service.custom.documentation.license = {
239+
name: 'John',
240+
}
241+
const definitionGenerator = new DefinitionGenerator(mockServerless)
242+
definitionGenerator.createInfo()
243+
244+
expect(definitionGenerator.openAPI).to.be.an('object')
245+
expect(definitionGenerator.openAPI.info).to.be.an('object')
246+
expect(definitionGenerator.openAPI.info).to.have.property('license')
247+
expect(definitionGenerator.openAPI.info.license).to.be.an('object')
248+
expect(definitionGenerator.openAPI.info.license.name).to.be.an('string')
249+
expect(definitionGenerator.openAPI.info.license).to.not.have.property('url')
250+
});
251+
252+
it('should assign specification extension fields when included', function() {
253+
mockServerless.service.custom.documentation['x-field'] = 'john'
254+
const definitionGenerator = new DefinitionGenerator(mockServerless)
255+
definitionGenerator.createInfo()
256+
257+
expect(definitionGenerator.openAPI).to.be.an('object')
258+
expect(definitionGenerator.openAPI.info).to.be.an('object')
259+
expect(definitionGenerator.openAPI.info).to.have.property('x-field')
260+
expect(definitionGenerator.openAPI.info['x-field']).to.be.equal('john')
261+
});
262+
263+
it('should ignore fields that do not conform to specifiction extension', function() {
264+
mockServerless.service.custom.documentation.otherField = 'john'
265+
const definitionGenerator = new DefinitionGenerator(mockServerless)
266+
definitionGenerator.createInfo()
267+
268+
expect(definitionGenerator.openAPI).to.be.an('object')
269+
expect(definitionGenerator.openAPI.info).to.be.an('object')
270+
expect(definitionGenerator.openAPI.info).to.not.have.property('otherField')
271+
});
177272
});
178273

179274
describe('createTags', () => {

0 commit comments

Comments
 (0)