JSON format modernisation and _shields_test removal (#3272)

* Modernised JSON format and removed _shields_test style

* Added logoWidth and labelColor fields to JSON response

* Reinstated and updated comment

* Extended expectBadge to accept Joi schemas for all fields
This commit is contained in:
Pierre-Yves B
2019-04-07 18:57:55 +01:00
committed by GitHub
parent 93333509c6
commit 25f8541e5b
75 changed files with 365 additions and 382 deletions

View File

@@ -81,7 +81,13 @@ describe('The request handler', function() {
it('should return the expected response', async function() {
const res = await fetch(`${baseUrl}/testing/123.json`)
expect(res.ok).to.be.true
expect(await res.json()).to.deep.equal({ name: 'testing', value: '123' })
expect(await res.json()).to.deep.equal({
name: 'testing',
value: '123',
label: 'testing',
message: '123',
color: 'lightgrey',
})
})
})
@@ -96,7 +102,13 @@ describe('The request handler', function() {
it('should return the expected response', async function() {
const res = await fetch(`${baseUrl}/testing/123.json`)
expect(res.ok).to.be.true
expect(await res.json()).to.deep.equal({ name: 'testing', value: '123' })
expect(await res.json()).to.deep.equal({
name: 'testing',
value: '123',
label: 'testing',
message: '123',
color: 'lightgrey',
})
})
})
@@ -121,6 +133,9 @@ describe('The request handler', function() {
expect(await res.json()).to.deep.equal({
name: 'testing',
value: '123',
label: 'testing',
message: '123',
color: 'lightgrey',
})
})
@@ -134,6 +149,9 @@ describe('The request handler', function() {
expect(await res.json()).to.deep.equal({
name: 'testing',
value: 'Maximum response size exceeded',
label: 'testing',
message: 'Maximum response size exceeded',
color: 'lightgrey',
})
})

View File

@@ -1,6 +1,7 @@
'use strict'
const Joi = require('joi')
const { expect } = require('chai')
// based on https://github.com/paulmelnikow/icedfrisby-nock/blob/master/icedfrisby-nock.js
// can be used to wrap the original "icedfrisby-nock" with additional functionality:
@@ -31,18 +32,25 @@ const factory = superclass =>
return this
}
expectBadge({ label, message, color }) {
let expectedBadge
if (typeof color === 'undefined') {
expectedBadge = { name: label, value: message }
} else {
expectedBadge = { name: label, value: message, color }
}
expectBadge({ label, message, logoWidth, labelColor, color }) {
return this.afterJSON(json => {
this.constructor._expectField(json, 'label', label)
this.constructor._expectField(json, 'message', message)
this.constructor._expectField(json, 'logoWidth', logoWidth)
this.constructor._expectField(json, 'labelColor', labelColor)
this.constructor._expectField(json, 'color', color)
})
}
if (typeof message === 'string') {
return this.expectJSON(expectedBadge)
} else {
return this.expectJSONTypes(Joi.object().keys(expectedBadge))
static _expectField(json, name, expected) {
if (typeof expected === 'string') {
expect(json[name], `${name} mismatch`).to.equal(expected)
} else if (typeof expected === 'object') {
Joi.validate(json[name], expected, err => {
if (err) {
throw err
}
})
}
}
}