-
Notifications
You must be signed in to change notification settings - Fork 37
/
test.js
192 lines (164 loc) · 6.8 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
require('jest-extended')
require('jest-chain')
const CoinMarketCap = require('./')
require('dotenv').config()
const API_KEY = process.env.COINMARKETCAP_API_KEY
test('should be defined', () => {
expect(CoinMarketCap).toBeDefined()
})
test('should return new CoinMarketCap client', () => {
const client = new CoinMarketCap(API_KEY)
expect(client.getTickers).toBeDefined()
expect(client.getGlobal).toBeDefined()
expect(client.getQuotes).toBeDefined()
expect(client.getIdMap).toBeDefined()
expect(client.getMetadata).toBeDefined()
})
test('getTickers should have correct response structure and type', async () => {
const client = new CoinMarketCap(API_KEY)
const ticker = await client.getTickers()
expect(ticker).toContainAllKeys(['data', 'status'])
expect(ticker).toHaveProperty('status.timestamp')
expect(ticker).toHaveProperty('status.credit_count')
expect(ticker).toHaveProperty('status.error_code')
expect(ticker.data).toBeArray()
expect(ticker.status.timestamp).toBeString()
})
test('should get latest tickers', async () => {
const client = new CoinMarketCap(API_KEY)
const ticker1 = await client.getTickers()
const ticker2 = await client.getTickers({ limit: 10 })
expect(typeof ticker1).toBe('object')
expect(typeof ticker2).toBe('object')
expect(Object.keys(ticker2.data).length).toBe(10)
})
test('limit = 0 returns 5000 tickers', async () => {
const client = new CoinMarketCap(API_KEY)
const ticker = await client.getTickers({ limit: 0 })
expect(Object.keys(ticker.data).length).toBeGreaterThan(0)
})
test('can pass in an array of currencies to convert for getTickers', async () => {
const client = new CoinMarketCap(API_KEY)
const ticker = await client.getTickers({ convert: ['USD'] })
ticker.data.forEach(coin => expect(coin.quote).toContainAllKeys(['USD']))
})
test('passing in start and limit = 0 is not allowed in getTickers', async () => {
const client = new CoinMarketCap(API_KEY)
expect(() => client.getTickers({ start: 2, limit: 0 })).toThrow(Error)
})
test('should get latest global', async () => {
const client = new CoinMarketCap(API_KEY)
const global = await client.getGlobal()
expect(typeof global).toBe('object')
expect(global).toContainAllKeys(['data', 'status'])
expect(global).toHaveProperty('status.timestamp')
expect(global).toHaveProperty('status.error_code')
expect(global).toHaveProperty('data.active_cryptocurrencies')
expect(global).toHaveProperty('data.quote')
expect(global).toHaveProperty('data.active_market_pairs')
})
test('can pass in currencies in various ways to getGlobal', async () => {
const client = new CoinMarketCap(API_KEY)
const global1 = await client.getGlobal('gbp')
const global2 = await client.getGlobal(['gbp'])
const global3 = await client.getGlobal({ convert: 'gbp' })
const global4 = await client.getGlobal({ convert: ['gbp'] })
expect(global1.data.quote).toHaveProperty('GBP')
expect(global2.data.quote).toHaveProperty('GBP')
expect(global3.data.quote).toHaveProperty('GBP')
expect(global4.data.quote).toHaveProperty('GBP')
})
test('should get ID map', async () => {
const client = new CoinMarketCap(API_KEY)
const map = await client.getIdMap({ symbol: ['BTC', 'ETH'] })
expect(typeof map).toBe('object')
expect(map).toContainAllKeys(['data', 'status'])
expect(map).toHaveProperty('status.timestamp')
expect(map).toHaveProperty('status.error_code')
expect(Array.isArray(map.data)).toBeTruthy()
for (const info of map.data) {
expect(typeof info).toBe('object')
expect(info).toHaveProperty('id')
expect(info).toHaveProperty('name')
expect(info).toHaveProperty('symbol')
}
})
test('should get quotes', async () => {
const client = new CoinMarketCap(API_KEY)
const quotes = await client.getQuotes({ symbol: ['BTC', 'ETH'] })
expect(typeof quotes).toBe('object')
expect(quotes).toContainAllKeys(['data', 'status'])
expect(quotes).toHaveProperty('status.timestamp')
expect(quotes).toHaveProperty('status.error_code')
expect(typeof quotes.data).toBe('object')
for (const key of Object.keys(quotes.data)) {
const info = quotes.data[key]
expect(typeof info).toBe('object')
expect(info).toHaveProperty('id')
expect(info).toHaveProperty('name')
expect(info).toHaveProperty('symbol')
expect(key).toEqual(info.symbol)
expect(info).toHaveProperty('circulating_supply')
expect(info).toHaveProperty('total_supply')
expect(info).toHaveProperty('max_supply')
expect(info).toHaveProperty('cmc_rank')
expect(info).toHaveProperty('quote')
expect(info.quote).toHaveProperty('USD')
expect(info.quote.USD).toHaveProperty('price')
expect(info.quote.USD).toHaveProperty('volume_24h')
expect(info.quote.USD).toHaveProperty('percent_change_1h')
expect(info.quote.USD).toHaveProperty('percent_change_24h')
expect(info.quote.USD).toHaveProperty('percent_change_7d')
expect(info.quote.USD).toHaveProperty('market_cap')
expect(info.quote.USD).toHaveProperty('last_updated')
}
})
test('can pass in an array of currencies to convert for getQuotes', async () => {
const client = new CoinMarketCap(API_KEY)
const quotes = await client.getQuotes({ id: [1, 2], convert: ['USD'] })
Object.values(quotes.data).forEach(coin => expect(coin.quote).toContainAllKeys(['USD']))
})
test('can pass in an array of IDs to id for getQuotes', async () => {
const client = new CoinMarketCap(API_KEY)
const quotes = await client.getQuotes({ id: [1, 2] })
expect(quotes.data).toContainAllKeys(['1', '2'])
})
test('must pass in id or symbol to getQuotes', async () => {
const client = new CoinMarketCap(API_KEY)
expect(() => client.getQuotes()).toThrow(Error)
})
test('cannot pass in both id and symbol to getQuotes', async () => {
const client = new CoinMarketCap(API_KEY)
expect(() => client.getQuotes({ id: 2, symbol: 'BTC' })).toThrow(Error)
})
test('should get metadata', async () => {
const client = new CoinMarketCap(API_KEY)
const metadata = await client.getMetadata({ symbol: ['BTC', 'ETH'] })
expect(typeof metadata).toBe('object')
expect(metadata).toContainAllKeys(['data', 'status'])
expect(metadata).toHaveProperty('status.timestamp')
expect(metadata).toHaveProperty('status.error_code')
expect(typeof metadata.data).toBe('object')
for (const key of Object.keys(metadata.data)) {
const info = metadata.data[key]
expect(typeof info).toBe('object')
expect(info).toHaveProperty('id')
expect(info).toHaveProperty('name')
expect(info).toHaveProperty('symbol')
expect(key).toEqual(info.symbol)
expect(info).toHaveProperty('logo')
expect(info).toHaveProperty('category')
expect(info).toHaveProperty('urls')
expect(info.urls).toContainAllKeys([
'website',
'technical_doc',
'twitter',
'reddit',
'message_board',
'announcement',
'chat',
'explorer',
'source_code'
])
}
})