| Conditions | 1 | 
| Paths | 1 | 
| Total Lines | 203 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* jshint -W101, -W098 */ | ||
| 40 | describe('webhooks api', function() { | ||
| 41 | var createdWebhooks = []; | ||
| 42 |     var cleanup = function(done) { | ||
| 43 |         client.allWebhooks({page:1, limit:500}, function(err, response) { | ||
| 44 | //delete each webhook | ||
| 45 | //var allWebhooks = response.data; | ||
| 46 |             if (!createdWebhooks.length) { | ||
| 47 | done(); | ||
| 48 | } | ||
| 49 |             createdWebhooks.forEach(function(identifier) { | ||
| 50 |                 client.deleteWebhook(identifier, function(err, response) { | ||
| 51 | createdWebhooks.splice(identifier, 1); | ||
| 52 |                     if (createdWebhooks.length === 0) { | ||
| 53 | done(); | ||
| 54 | } | ||
| 55 | }); | ||
| 56 | }); | ||
| 57 | }); | ||
| 58 | }; | ||
| 59 |     before(function(done) { | ||
| 60 | // runs before all tests in this block..cleanup any existing data that could conflict with the tests | ||
| 61 | cleanup(done); | ||
| 62 | }); | ||
| 63 |     after(function(done) { | ||
| 64 | //cleanup after all tests | ||
| 65 | cleanup(done); | ||
| 66 | }); | ||
| 67 | |||
| 68 | //create a custom (yet "random") identifier such to avoid conflicts when running multiple tests simultaneously | ||
| 69 |     var myIdentifier = crypto.randomBytes(24).toString('hex'); | ||
| 70 | |||
| 71 | // test cases | ||
| 72 |     it('create new webhook with custom identifier', function(done) { | ||
| 73 |         client.setupWebhook("https://www.blocktrail.com/webhook-test", myIdentifier, function(err, webhook) { | ||
| 74 | assert.ifError(err); | ||
| 75 | assert.equal(webhook.url, "https://www.blocktrail.com/webhook-test"); | ||
| 76 | assert.equal(webhook.identifier, myIdentifier); | ||
| 77 | createdWebhooks.push(webhook.identifier); | ||
| 78 | done(); | ||
| 79 | }); | ||
| 80 | }); | ||
| 81 | |||
| 82 |     it('create new webhook with random identifier', function(done) { | ||
| 83 |         client.setupWebhook("https://www.blocktrail.com/webhook-test", function(err, webhook) { | ||
| 84 | assert.ifError(err); | ||
| 85 | assert.equal(webhook.url, "https://www.blocktrail.com/webhook-test"); | ||
| 86 | assert.ok(webhook.identifier); | ||
| 87 | createdWebhooks.push(webhook.identifier); | ||
| 88 | done(); | ||
| 89 | }); | ||
| 90 | }); | ||
| 91 | |||
| 92 |     it('get all user webhooks', function(done) { | ||
| 93 |         client.allWebhooks(null, function(err, response) { | ||
| 94 | assert.ifError(err); | ||
| 95 |             assert.ok('data' in response, "'data' key not in response"); | ||
| 96 |             assert.ok('total' in response, "'total' key not in response"); | ||
| 97 | assert.ok(parseInt(response['total']) >= 2, "'total' does not match expected value"); | ||
| 98 | assert.ok(response['data'].length >= 2, "Count of webhooks returned is not equal to 2"); | ||
| 99 | |||
| 100 |             assert.ok('url' in response['data'][0], "'url' key not in first webhook of response"); | ||
| 101 |             assert.ok('url' in response['data'][1], "'url' key not in second webhook of response"); | ||
| 102 | done(); | ||
| 103 | }); | ||
| 104 | }); | ||
| 105 | |||
| 106 |     it('get a single webhook', function(done) { | ||
| 107 |         client.getWebhook(createdWebhooks[0], function(err, response) { | ||
| 108 | assert.ifError(err); | ||
| 109 |             assert.ok('url' in response, "'url' key not in response"); | ||
| 110 |             assert.ok('identifier' in response, "'identifier' key not in response"); | ||
| 111 | assert.equal(response['url'], "https://www.blocktrail.com/webhook-test", "'url' does not match expected value"); | ||
| 112 | assert.equal(response['identifier'], myIdentifier, "'identifier' does not match expected value"); | ||
| 113 | done(); | ||
| 114 | }); | ||
| 115 | }); | ||
| 116 | |||
| 117 |     it('delete a webhook', function(done) { | ||
| 118 |         client.deleteWebhook(createdWebhooks[0], function(err, response) { | ||
| 119 | assert.ifError(err); | ||
| 120 | assert.ok(response); | ||
| 121 | done(); | ||
| 122 | }); | ||
| 123 | }); | ||
| 124 | |||
| 125 |     it('update a webhook', function(done) { | ||
| 126 |         var newIdentifier = crypto.randomBytes(24).toString('hex'); | ||
| 127 | var newUrl = "https://www.blocktrail.com/new-webhook-url"; | ||
| 128 |         client.updateWebhook(createdWebhooks[1], {identifier: newIdentifier, url: newUrl}, function(err, response) { | ||
| 129 | assert.ifError(err); | ||
| 130 |             assert.ok('url' in response, "'url' key not in response"); | ||
| 131 |             assert.ok('identifier' in response, "'identifier' key not in response"); | ||
| 132 | assert.equal(response['url'], newUrl, "'url' does not match expected value"); | ||
| 133 | assert.equal(response['identifier'], newIdentifier, "'identifier' does not match expected value"); | ||
| 134 | |||
| 135 | createdWebhooks[1] = newIdentifier; | ||
| 136 | done(); | ||
| 137 | }); | ||
| 138 | }); | ||
| 139 | |||
| 140 |     it('subscribe to address-transaction events', function(done) { | ||
| 141 | var address = "1dice8EMZmqKvrGE4Qc9bUFf9PX3xaYDp"; | ||
| 142 |         client.subscribeAddressTransactions(createdWebhooks[1], address, 2, function(err, response) { | ||
| 143 | assert.ifError(err); | ||
| 144 |             assert.ok('event_type' in response, "'event_type' key not in response"); | ||
| 145 |             assert.ok('address' in response, "'address' key not in response"); | ||
| 146 |             assert.ok('confirmations' in response, "'confirmations' key not in response"); | ||
| 147 | assert.equal(response['event_type'], "address-transactions", "'event_type' does not match expected value"); | ||
| 148 | assert.equal(response['address'], address, "'address' does not match expected value"); | ||
| 149 | assert.equal(response['confirmations'], 2, "'confirmations' does not match expected value"); | ||
| 150 | done(); | ||
| 151 | }); | ||
| 152 | }); | ||
| 153 | |||
| 154 |     it('subscribe to transaction event', function(done) { | ||
| 155 | var transaction = "a0a87b1577d606b349cfded85c842bdc53b99bcd49614229a71804b46b1c27cc"; | ||
| 156 |         client.subscribeTransaction(createdWebhooks[1], transaction, 2, function(err, response) { | ||
| 157 | assert.ifError(err); | ||
| 158 |             assert.ok('event_type' in response, "'event_type' key not in response"); | ||
| 159 |             assert.ok('address' in response, "'address' key not in response"); | ||
| 160 |             assert.ok('confirmations' in response, "'confirmations' key not in response"); | ||
| 161 | assert.equal(response['event_type'], "transaction", "'event_type' does not match expected value"); | ||
| 162 | assert.equal(response['transaction'], transaction, "'transaction' does not match expected value"); | ||
| 163 | assert.equal(response['confirmations'], 2, "'confirmations' does not match expected value"); | ||
| 164 | done(); | ||
| 165 | }); | ||
| 166 | }); | ||
| 167 | |||
| 168 |     it('subscribe to new block events', function(done) { | ||
| 169 |         client.subscribeNewBlocks(createdWebhooks[1], function(err, response) { | ||
| 170 | assert.ifError(err); | ||
| 171 |             assert.ok('event_type' in response, "'event_type' key not in response"); | ||
| 172 |             assert.ok('address' in response, "'address' key not in response"); | ||
| 173 |             assert.ok('confirmations' in response, "'confirmations' key not in response"); | ||
| 174 | assert.equal(response['event_type'], "block", "'event_type' does not match expected value"); | ||
| 175 | assert.equal(response['address'], null, "'address' does not match expected value"); | ||
| 176 | assert.equal(response['confirmations'], null, "'confirmations' does not match expected value"); | ||
| 177 | done(); | ||
| 178 | }); | ||
| 179 | }); | ||
| 180 | |||
| 181 |     it('batch subscribe to address-transaction events', function(done) { | ||
| 182 | var batchData = [ | ||
| 183 |             { | ||
| 184 | 'event_type': 'address-transactions', | ||
| 185 | 'address': '18FA8Tn54Hu8fjn7kkfAygPoGEJLHMbHzo', | ||
| 186 | 'confirmations': 1 | ||
| 187 | }, | ||
| 188 |             { | ||
| 189 | 'address': '1LUCKYwD6V9JHVXAFEEjyQSD4Dj5GLXmte', | ||
| 190 | 'confirmations': 1 | ||
| 191 | }, | ||
| 192 |             { | ||
| 193 | 'address': '1qMBuZnrmGoAc2MWyTnSgoLuWReDHNYyF' | ||
| 194 | } | ||
| 195 | ]; | ||
| 196 |         client.batchSubscribeAddressTransactions(createdWebhooks[1], batchData, function(err, response) { | ||
| 197 | assert.ifError(err); | ||
| 198 | assert.ok(response); | ||
| 199 | done(); | ||
| 200 | }); | ||
| 201 | }); | ||
| 202 | |||
| 203 |     it('get webhook event subscriptions', function(done) { | ||
| 204 |         client.getWebhookEvents(createdWebhooks[1], function(err, response) { | ||
| 205 | assert.ifError(err); | ||
| 206 |             assert.ok('data' in response, "'data' key not in response"); | ||
| 207 |             assert.ok('total' in response, "'total' key not in response"); | ||
| 208 | assert.equal(parseInt(response['total']), 6, "'total' does not match expected value"); | ||
| 209 | assert.equal(response['data'].length, 6, "Count of event subscriptions returned is not equal to 2"); | ||
| 210 | |||
| 211 |             assert.ok('event_type' in response['data'][0], "'event_type' key not in first event subscription of response"); | ||
| 212 | |||
| 213 | done(); | ||
| 214 | }); | ||
| 215 | }); | ||
| 216 | |||
| 217 |     it('unsubscribe from address-transaction events', function(done) { | ||
| 218 | var address = "1dice8EMZmqKvrGE4Qc9bUFf9PX3xaYDp"; | ||
| 219 |         client.unsubscribeAddressTransactions(createdWebhooks[1], address, function(err, response) { | ||
| 220 | assert.ifError(err); | ||
| 221 | assert.ok(response); | ||
| 222 | done(); | ||
| 223 | }); | ||
| 224 | }); | ||
| 225 | |||
| 226 |     it('unsubscribe from new transaction events', function(done) { | ||
| 227 | var transaction = "a0a87b1577d606b349cfded85c842bdc53b99bcd49614229a71804b46b1c27cc"; | ||
| 228 |         client.unsubscribeTransaction(createdWebhooks[1], transaction, function(err, response) { | ||
| 229 | assert.ifError(err); | ||
| 230 | assert.ok(response); | ||
| 231 | done(); | ||
| 232 | }); | ||
| 233 | }); | ||
| 234 | |||
| 235 |     it('unsubscribe from new block events', function(done) { | ||
| 236 |         client.unsubscribeNewBlocks(createdWebhooks[1], function(err, response) { | ||
| 237 | assert.ifError(err); | ||
| 238 | assert.ok(response); | ||
| 239 | done(); | ||
| 240 | }); | ||
| 241 | }); | ||
| 242 | }); | ||
| 243 | |||
| 310 | 
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.