aboutsummaryrefslogtreecommitdiffstats
path: root/packages/connect/test
diff options
context:
space:
mode:
authorBrandon Millman <brandon.millman@gmail.com>2018-05-30 03:54:53 +0800
committerBrandon Millman <brandon.millman@gmail.com>2018-07-12 01:25:54 +0800
commitaf395eccda95f12ba14f40f9261443d24e48f455 (patch)
tree0fee2323cdba4efefcfebf7152aa0d5552ef3258 /packages/connect/test
parentc403dcdabf4b98ea0ecde1ab0e7e9dafa97d797f (diff)
downloaddexon-sol-tools-af395eccda95f12ba14f40f9261443d24e48f455.tar
dexon-sol-tools-af395eccda95f12ba14f40f9261443d24e48f455.tar.gz
dexon-sol-tools-af395eccda95f12ba14f40f9261443d24e48f455.tar.bz2
dexon-sol-tools-af395eccda95f12ba14f40f9261443d24e48f455.tar.lz
dexon-sol-tools-af395eccda95f12ba14f40f9261443d24e48f455.tar.xz
dexon-sol-tools-af395eccda95f12ba14f40f9261443d24e48f455.tar.zst
dexon-sol-tools-af395eccda95f12ba14f40f9261443d24e48f455.zip
Update orderbook channel and factory tests
Diffstat (limited to 'packages/connect/test')
-rw-r--r--packages/connect/test/orderbook_channel_factory_test.ts34
-rw-r--r--packages/connect/test/ws_orderbook_channel_test.ts26
2 files changed, 41 insertions, 19 deletions
diff --git a/packages/connect/test/orderbook_channel_factory_test.ts b/packages/connect/test/orderbook_channel_factory_test.ts
index fd84332cc..d2140bfa6 100644
--- a/packages/connect/test/orderbook_channel_factory_test.ts
+++ b/packages/connect/test/orderbook_channel_factory_test.ts
@@ -9,18 +9,38 @@ import { orderbookChannelFactory } from '../src/orderbook_channel_factory';
chai.config.includeStack = true;
chai.use(dirtyChai);
const expect = chai.expect;
+const emptyOrderbookChannelHandler = {
+ onSnapshot: () => {
+ _.noop();
+ },
+ onUpdate: () => {
+ _.noop();
+ },
+ onError: () => {
+ _.noop();
+ },
+ onClose: () => {
+ _.noop();
+ },
+};
describe('orderbookChannelFactory', () => {
const websocketUrl = 'ws://localhost:8080';
-
describe('#createWebSocketOrderbookChannelAsync', () => {
it('throws when input is not a url', () => {
- const badInput = 54;
- const badSubscribeCall = orderbookChannelFactory.createWebSocketOrderbookChannelAsync.bind(
- orderbookChannelFactory,
- badInput,
- );
- expect(orderbookChannelFactory.createWebSocketOrderbookChannelAsync(badInput as any)).to.be.rejected();
+ const badUrlInput = 54;
+ expect(
+ orderbookChannelFactory.createWebSocketOrderbookChannelAsync(
+ badUrlInput as any,
+ emptyOrderbookChannelHandler,
+ ),
+ ).to.be.rejected();
+ });
+ it('throws when handler has the incorrect members', () => {
+ const badHandlerInput = {};
+ expect(
+ orderbookChannelFactory.createWebSocketOrderbookChannelAsync(websocketUrl, badHandlerInput as any),
+ ).to.be.rejected();
});
});
});
diff --git a/packages/connect/test/ws_orderbook_channel_test.ts b/packages/connect/test/ws_orderbook_channel_test.ts
index f4ad67ba3..fed4f2217 100644
--- a/packages/connect/test/ws_orderbook_channel_test.ts
+++ b/packages/connect/test/ws_orderbook_channel_test.ts
@@ -2,6 +2,7 @@ import * as chai from 'chai';
import * as dirtyChai from 'dirty-chai';
import * as _ from 'lodash';
import 'mocha';
+import * as Sinon from 'sinon';
import * as WebSocket from 'websocket';
import { WebSocketOrderbookChannel } from '../src/ws_orderbook_channel';
@@ -26,8 +27,10 @@ const emptyOrderbookChannelHandler = {
describe('WebSocketOrderbookChannel', () => {
const websocketUrl = 'ws://localhost:8080';
- const client = new WebSocket.w3cwebsocket(websocketUrl);
- const orderbookChannel = new WebSocketOrderbookChannel(client, emptyOrderbookChannelHandler);
+ const openClient = new WebSocket.w3cwebsocket(websocketUrl);
+ Sinon.stub(openClient, 'readyState').get(() => WebSocket.w3cwebsocket.OPEN);
+ Sinon.stub(openClient, 'send').callsFake(_.noop);
+ const openOrderbookChannel = new WebSocketOrderbookChannel(openClient, emptyOrderbookChannelHandler);
const subscriptionOpts = {
baseTokenAddress: '0x323b5d4c32345ced77393b3530b1eed0f346429d',
quoteTokenAddress: '0xef7fff64389b814a946f3e92105513705ca6b990',
@@ -36,22 +39,21 @@ describe('WebSocketOrderbookChannel', () => {
};
describe('#subscribe', () => {
it('throws when subscriptionOpts does not conform to schema', () => {
- const badSubscribeCall = orderbookChannel.subscribe.bind(
- orderbookChannel,
- {},
- emptyOrderbookChannelHandler,
- );
+ const badSubscribeCall = openOrderbookChannel.subscribe.bind(openOrderbookChannel, {});
expect(badSubscribeCall).throws(
'Expected subscriptionOpts to conform to schema /RelayerApiOrderbookChannelSubscribePayload\nEncountered: {}\nValidation errors: instance requires property "baseTokenAddress", instance requires property "quoteTokenAddress"',
);
});
it('does not throw when inputs are of correct types', () => {
- const goodSubscribeCall = orderbookChannel.subscribe.bind(
- orderbookChannel,
- subscriptionOpts,
- emptyOrderbookChannelHandler,
- );
+ const goodSubscribeCall = openOrderbookChannel.subscribe.bind(openOrderbookChannel, subscriptionOpts);
expect(goodSubscribeCall).to.not.throw();
});
+ it('throws when client is closed', () => {
+ const closedClient = new WebSocket.w3cwebsocket(websocketUrl);
+ Sinon.stub(closedClient, 'readyState').get(() => WebSocket.w3cwebsocket.CLOSED);
+ const closedOrderbookChannel = new WebSocketOrderbookChannel(closedClient, emptyOrderbookChannelHandler);
+ const badSubscribeCall = closedOrderbookChannel.subscribe.bind(closedOrderbookChannel, subscriptionOpts);
+ expect(badSubscribeCall).throws('WebSocket connection is closed');
+ });
});
});