|
1 | | -/* global describe, it */ |
| 1 | +/* global describe, it, beforeEach */ |
2 | 2 |
|
3 | 3 | 'use strict' |
4 | 4 |
|
@@ -85,15 +85,105 @@ var itShouldGenerateOutput = function (request, path, target, client) { |
85 | 85 | } |
86 | 86 |
|
87 | 87 | describe('Available Targets', function () { |
88 | | - var targets = HTTPSnippet.availableTargets() |
89 | | - |
90 | | - targets.forEach(function (target) { |
| 88 | + HTTPSnippet.availableTargets().forEach(function (target) { |
91 | 89 | it('available-targets.json should include ' + target.title, function () { |
92 | 90 | fixtures['available-targets'].should.containEql(target) |
93 | 91 | }) |
94 | 92 | }) |
95 | 93 | }) |
96 | 94 |
|
| 95 | +describe('Custom targets', function () { |
| 96 | + describe('Adding a custom target', function () { |
| 97 | + it('should throw if the target does has no info object', function () { |
| 98 | + (function () { |
| 99 | + HTTPSnippet.addTarget({}) |
| 100 | + }).should.throw(Error) |
| 101 | + }) |
| 102 | + |
| 103 | + it('should throw if the target does not have a properly constructed info object', function () { |
| 104 | + (function () { |
| 105 | + HTTPSnippet.addTarget({info: {key: ''}}) |
| 106 | + }).should.throw(Error) |
| 107 | + }) |
| 108 | + |
| 109 | + it('should throw if the target already exists', function () { |
| 110 | + (function () { |
| 111 | + HTTPSnippet.addTarget(targets.node) |
| 112 | + }).should.throw(Error) |
| 113 | + }) |
| 114 | + |
| 115 | + it('should throw if the target has no client', function () { |
| 116 | + (function () { |
| 117 | + HTTPSnippet.addTarget({ |
| 118 | + info: targets.node.info |
| 119 | + }) |
| 120 | + }).should.throw(Error) |
| 121 | + }) |
| 122 | + |
| 123 | + it('should add and convert for a new custom target', function () { |
| 124 | + const customTarget = require('./fixtures/customTarget') |
| 125 | + |
| 126 | + HTTPSnippet.addTarget(customTarget) |
| 127 | + const target = HTTPSnippet.availableTargets().find(function (target) { return target.key === customTarget.info.key }) |
| 128 | + const client = target.clients.find(function (client) { return client.key === customTarget.info.default }) |
| 129 | + client.should.be.an.Object() |
| 130 | + |
| 131 | + Object.keys(fixtures.requests).filter(clearInfo).forEach(function (request) { |
| 132 | + // Re-using the `request` module fixtures and framework since we copied it to create a custom client. |
| 133 | + itShouldGenerateOutput(request, 'node/request/', customTarget.info.key, customTarget.info.default) |
| 134 | + }) |
| 135 | + }) |
| 136 | + }) |
| 137 | + |
| 138 | + describe('Adding a custom client target', function () { |
| 139 | + let customClient |
| 140 | + |
| 141 | + beforeEach(function () { |
| 142 | + // Re-using the existing request client instead of mocking out something completely new. |
| 143 | + customClient = { |
| 144 | + ...targets.node.request, |
| 145 | + info: { |
| 146 | + key: 'axios', |
| 147 | + title: 'Axios', |
| 148 | + link: 'https://www.npmjs.com/package/axios', |
| 149 | + description: 'Promise based HTTP client for the browser and node.js' |
| 150 | + } |
| 151 | + } |
| 152 | + }) |
| 153 | + |
| 154 | + it("should throw if the client's target does not exist", function () { |
| 155 | + (function () { |
| 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: ''}}) |
| 169 | + }).should.throw(Error) |
| 170 | + }) |
| 171 | + |
| 172 | + it('should add and convert for a new custom client target', function () { |
| 173 | + HTTPSnippet.addTargetClient('node', customClient) |
| 174 | + |
| 175 | + const target = HTTPSnippet.availableTargets().find(function (target) { return target.key === 'node' }) |
| 176 | + const client = target.clients.find(function (client) { return client.key === customClient.info.key }) |
| 177 | + client.should.be.an.Object() |
| 178 | + |
| 179 | + Object.keys(fixtures.requests).filter(clearInfo).forEach(function (request) { |
| 180 | + // Re-using the `request` module fixtures and framework since we copied it to create a custom client target. |
| 181 | + itShouldGenerateOutput(request, 'node/request/', 'node', customClient.info.key) |
| 182 | + }) |
| 183 | + }) |
| 184 | + }) |
| 185 | +}) |
| 186 | + |
97 | 187 | // test all the things! |
98 | 188 | describe('Targets', function () { |
99 | 189 | Object.keys(targets).forEach(function (target) { |
|
0 commit comments