Files
bybit-api/test/contract/ws.public.inverse.test.ts
2023-02-17 14:01:07 +00:00

77 lines
1.8 KiB
TypeScript

/* eslint-disable no-unused-vars */
/* eslint-disable @typescript-eslint/no-unused-vars */
import {
WSClientConfigurableOptions,
WS_KEY_MAP,
WebsocketClient,
} from '../../src';
import {
WS_OPEN_EVENT_PARTIAL,
getSilentLogger,
logAllEvents,
waitForSocketEvent,
} from '../ws.util';
describe('Public Contract Inverse Websocket Client', () => {
let wsClient: WebsocketClient;
const wsClientOptions: WSClientConfigurableOptions = {
market: 'contractInverse',
};
beforeAll(() => {
wsClient = new WebsocketClient(
wsClientOptions,
getSilentLogger('expectSuccessNoAuth')
);
wsClient.connectPublic();
});
afterAll(() => {
wsClient.closeAll(true);
});
it('should open a public ws connection', async () => {
const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
try {
expect(await wsOpenPromise).toMatchObject({
event: WS_OPEN_EVENT_PARTIAL,
wsKey: WS_KEY_MAP.contractInversePublic,
});
} catch (e) {
console.error('open: ', e);
expect(e).toBeFalsy();
}
});
it('should subscribe to public orderbook events', async () => {
const wsResponsePromise = waitForSocketEvent(wsClient, 'response');
const wsUpdatePromise = waitForSocketEvent(wsClient, 'update');
const wsTopic = 'orderbook.1.BTCUSD';
wsClient.subscribe(wsTopic);
try {
expect(await wsResponsePromise).toMatchObject({
success: true,
op: 'subscribe',
});
} catch (e) {
console.error('response: ', e);
// sub failed (or jest expect threw)
expect(e).toBeFalsy();
}
try {
expect(await wsUpdatePromise).toMatchObject({
topic: wsTopic,
data: {
a: expect.any(Array),
},
});
} catch (e) {
console.error(`Wait for "${wsTopic}" event exception: `, e);
}
});
});