-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathindex.js
130 lines (110 loc) · 3.57 KB
/
index.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
/* eslint-env mocha */
'use strict'
const assert = require('assert')
const BFX = require('../index')
const { RESTv1, RESTv2 } = require('bfx-api-node-rest')
const WSv1 = require('bfx-api-node-ws1')
const WSv2 = require('../lib/transports/ws2')
describe('BFX', () => {
it('should be loaded', () => {
assert.strictEqual(typeof BFX, 'function')
})
describe('constructor', () => {
it('throws on using the deprecated way to set options', () => {
assert.throws(() => new BFX(2, {}))
assert.throws(() => new BFX('dummy', 'dummy', 2))
})
})
describe('rest', () => {
it('throws an error if an invalid version is requested', () => {
const bfx = new BFX()
assert.throws(bfx.rest.bind(bfx, 0))
assert.throws(bfx.rest.bind(bfx, 3))
})
it('returns correct REST api by version', () => {
const bfx = new BFX()
const restDefault = bfx.rest()
const rest1 = bfx.rest(1)
const rest2 = bfx.rest(2)
assert(restDefault instanceof RESTv2)
assert(rest1 instanceof RESTv1)
assert(rest2 instanceof RESTv2)
})
it('passes API keys & transform flag to new transport', () => {
const bfx = new BFX({
apiKey: 'k',
apiSecret: 's',
transform: true,
rest: {
url: 'http://'
}
})
const rest1 = bfx.rest(1)
const rest2 = bfx.rest(2)
assert.strictEqual(rest1._apiKey, 'k')
assert.strictEqual(rest2._apiKey, 'k')
assert.strictEqual(rest1._apiSecret, 's')
assert.strictEqual(rest2._apiSecret, 's')
assert.strictEqual(rest1._url, 'http://')
assert.strictEqual(rest2._url, 'http://')
assert.strictEqual(rest2._transform, true)
})
it('passes extra options to new transport', () => {
const bfx = new BFX()
const rest2 = bfx.rest(2, { url: '/dev/null' })
assert.strictEqual(rest2._url, '/dev/null')
})
it('returns one instance if called twice for the same version', () => {
const bfx = new BFX()
const restA = bfx.rest(2)
const restB = bfx.rest(2)
assert(restA === restB)
})
})
describe('ws', () => {
it('throws an error if an invalid version is requested', () => {
const bfx = new BFX()
assert.throws(bfx.ws.bind(bfx, 0))
assert.throws(bfx.ws.bind(bfx, 3))
})
it('returns correct WebSocket api by version', () => {
const bfx = new BFX()
const wsDefault = bfx.ws()
const ws1 = bfx.ws(1)
const ws2 = bfx.ws(2)
assert(wsDefault instanceof WSv2)
assert(ws1 instanceof WSv1)
assert(ws2 instanceof WSv2)
})
it('passes API keys & transform flag to new transport', () => {
const bfx = new BFX({
apiKey: 'k',
apiSecret: 's',
transform: true,
ws: {
url: 'wss://'
}
})
const ws1 = bfx.ws(1)
const ws2 = bfx.ws(2)
assert.strictEqual(ws1._apiKey, 'k')
assert.strictEqual(ws2._authArgs.apiKey, 'k')
assert.strictEqual(ws1._apiSecret, 's')
assert.strictEqual(ws2._authArgs.apiSecret, 's')
assert.strictEqual(ws1._url, 'wss://')
assert.strictEqual(ws2._url, 'wss://')
assert.strictEqual(ws2._transform, true)
})
it('passes extra options to new transport', () => {
const bfx = new BFX()
const ws2 = bfx.ws(2, { url: '/dev/null' })
assert.strictEqual(ws2._url, '/dev/null')
})
it('returns one instance if called twice for the same version', () => {
const bfx = new BFX()
const wsA = bfx.ws(2)
const wsB = bfx.ws(2)
assert(wsA === wsB)
})
})
})