Skip to content

Commit 02ef6fa

Browse files
committed
Simplifying the custom target client API a bit.
1 parent 0cecdd3 commit 02ef6fa

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,16 @@ module.exports.addTarget = function (target) {
235235
targets[target.info.key] = target
236236
}
237237

238-
module.exports.addTargetClient = function (target, client, plugin) {
238+
module.exports.addTargetClient = function (target, client) {
239239
if (!targets.hasOwnProperty(target)) {
240240
throw new Error(`Sorry, but no ${target} target exists to add clients to.`)
241+
} else if (!('info' in client)) {
242+
throw new Error('The supplied custom target client must contain an `info` object.')
243+
} else if (!('key' in client.info) || !('title' in client.info)) {
244+
throw new Error('The supplied custom target client must have an `info` object with a `key` and `title` property.')
241245
}
242246

243-
targets[target][client] = plugin
247+
targets[target][client.info.key] = client
244248
}
245249

246250
module.exports.availableTargets = function () {

test/targets.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,32 @@ describe('Custom targets', function () {
153153

154154
it("should throw if the client's target does not exist", function () {
155155
(function () {
156-
HTTPSnippet.addTargetClient('node.js', 'axios', customClient)
156+
HTTPSnippet.addTargetClient('node.js', customClient)
157+
}).should.throw(Error)
158+
})
159+
160+
it('should throw if the client does has no info object', function () {
161+
(function () {
162+
HTTPSnippet.addTargetClient('node', {})
163+
}).should.throw(Error)
164+
})
165+
166+
it('should throw if the target does not have a properly constructed info object', function () {
167+
(function () {
168+
HTTPSnippet.addTargetClient('node', {info: {key: ''}})
157169
}).should.throw(Error)
158170
})
159171

160172
it('should add and convert for a new custom client target', function () {
161-
HTTPSnippet.addTargetClient('node', 'axios', customClient)
173+
HTTPSnippet.addTargetClient('node', customClient)
162174

163175
const target = HTTPSnippet.availableTargets().find(function (target) { return target.key === 'node' })
164-
const client = target.clients.find(function (client) { return client.key === 'axios' })
176+
const client = target.clients.find(function (client) { return client.key === customClient.info.key })
165177
client.should.be.an.Object()
166178

167179
Object.keys(fixtures.requests).filter(clearInfo).forEach(function (request) {
168180
// Re-using the `request` module fixtures and framework since we copied it to create a custom client target.
169-
itShouldGenerateOutput(request, 'node/request/', 'node', 'axios')
181+
itShouldGenerateOutput(request, 'node/request/', 'node', customClient.info.key)
170182
})
171183
})
172184
})

0 commit comments

Comments
 (0)