Skip to content

Commit 2fdea1a

Browse files
committed
Refactor tryConversion=false tests
- Add helper function - Use a const instead of inlining LTD in tests - Test all methods that support tryConversion=false
1 parent e34e81b commit 2fdea1a

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

test/cryptocompare.test.js

+28-15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ const test = require('tape')
55
global.fetch = require('node-fetch')
66
const cc = require('../')
77

8+
// NOT_USD_TRADABLE is a cryptocurrency which does not trade directly with USD.
9+
// This value is used in testing tryConversion. Currently, this is set to LTD.
10+
// If LTD trades directly with USD in the future, tests will fail.
11+
// In that case, change this value:
12+
const NOT_USD_TRADABLE = 'LTD'
13+
814
test('price()', t => {
915
t.plan(2)
1016
cc.price('BTC', ['USD', 'EUR']).then(prices => {
@@ -23,13 +29,7 @@ test('price() allows passing a string as the second parameter', t => {
2329
})
2430

2531
test("price()'s tryConversion=false works", t => {
26-
t.plan(1)
27-
cc.price('LTD', 'USD', false).then(prices => {
28-
t.end('Promise should not resolve')
29-
}).catch(e => {
30-
t.ok(e.match(/market does not exist/i), 'Converting LTD to USD fails')
31-
t.end()
32-
}).catch(t.end)
32+
testTryConversion(cc.price(NOT_USD_TRADABLE, 'USD', false), t)
3333
})
3434

3535
test('priceMulti()', t => {
@@ -51,6 +51,10 @@ test('priceMulti() allows passing strings instead of arrays', t => {
5151
}).catch(t.end)
5252
})
5353

54+
test("priceMulti()'s tryConversion=false works", t => {
55+
testTryConversion(cc.priceMulti(NOT_USD_TRADABLE, 'USD', false), t)
56+
})
57+
5458
test('priceFull()', t => {
5559
t.plan(5 * 2 * 2)
5660
cc.priceFull(['BTC', 'ETH'], ['USD', 'EUR']).then(prices => {
@@ -81,6 +85,10 @@ test('priceFull() allows passing strings instead of arrays', t => {
8185
}).catch(t.end)
8286
})
8387

88+
test("priceFull()'s tryConversion=false works", t => {
89+
testTryConversion(cc.priceFull(NOT_USD_TRADABLE, 'USD', false), t)
90+
})
91+
8492
test('priceHistorical()', t => {
8593
t.plan(3)
8694
// NOTE: Historical values are hard-coded into this test
@@ -94,15 +102,8 @@ test('priceHistorical()', t => {
94102
})
95103

96104
test("priceHistorical()'s tryConversion=false works", t => {
97-
t.plan(1)
98-
// NOTE: Historical values are hard-coded into this test
99105
const timestamp = new Date('2017-01-01')
100-
cc.priceHistorical('LTD', 'USD', timestamp, false).then(prices => {
101-
t.end('Promise should not resolve')
102-
}).catch(e => {
103-
t.ok(e.match(/market does not exist/i), 'Converting LTD to USD fails')
104-
t.end()
105-
}).catch(t.end)
106+
testTryConversion(cc.priceHistorical(NOT_USD_TRADABLE, 'USD', timestamp, false), t)
106107
})
107108

108109
test('error handling', t => {
@@ -113,3 +114,15 @@ test('error handling', t => {
113114
t.end()
114115
}).catch(t.end)
115116
})
117+
118+
// Helper Functions:
119+
120+
function testTryConversion (promise, t) {
121+
t.plan(1)
122+
promise.then(prices => {
123+
t.end('Promise should not resolve')
124+
}).catch(e => {
125+
t.ok(e.match(/market does not exist/i), `Converting ${NOT_USD_TRADABLE} to USD fails`)
126+
t.end()
127+
}).catch(t.end)
128+
}

0 commit comments

Comments
 (0)