Skip to content

Commit

Permalink
Add tests for orderBookSnapshot event
Browse files Browse the repository at this point in the history
  • Loading branch information
maxima-net committed Aug 16, 2022
1 parent d9f2bf4 commit 50cdbc0
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/clients/testCases/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export { default as validCancelOrderWithDirectionsTestCases } from './validCance
export { default as validCancelAllOrdersWithDirectionsTestCases } from './validCancelAllOrdersWithDirectionsTestCases';
export { default as validOrderBookWithDirectionsTestCases } from './validOrderBookWithDirectionsTestCases';
export { default as validWsTopOfBookUpdatedTestCases } from './validWsTopOfBookUpdatedTestCases';
export { default as validWsOrderBookSnapshotTestCases } from './validWsOrderBookSnapshotTestCases';
export { default as validWsOrderBookUpdatedTestCases } from './validWsOrderBookUpdatedTestCases';
122 changes: 122 additions & 0 deletions tests/clients/testCases/validWsOrderBookSnapshotTestCases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import BigNumber from 'bignumber.js';

import type { WebSocketOrderBookSnapshotResponseDto } from '../../../src/clients/dtos';
import type { OrderBook, } from '../../../src/exchange/index';

const validWsOrderBookSnapshotTestCases: ReadonlyArray<readonly [
message: string,
testValue: readonly [snapshotDtos: WebSocketOrderBookSnapshotResponseDto[], expectedOrderBooks: OrderBook[]]
]> = [
[
'Single snapshot',
[
[{
event: 'snapshot',
data: {
updateId: 57551,
symbol: 'ETH/BTC',
entries: [{
side: 'Buy',
price: 0.06688964,
qtyProfile: [4.0]
}, {
side: 'Sell',
price: 0.06754710,
qtyProfile: [1.5]
}]
}
}],
[{
updateId: 57551,
symbol: 'ETH/BTC',
quoteCurrency: 'ETH',
baseCurrency: 'BTC',
entries: [
{
side: 'Buy',
price: new BigNumber(0.06688964),
qtyProfile: [4.0]
},
{
side: 'Sell',
price: new BigNumber(0.06754710),
qtyProfile: [1.5]
}
]
}]
]
],
[
'Several snapshots',
[
[{
event: 'snapshot',
data: {
updateId: 57551,
symbol: 'ETH/BTC',
entries: [{
side: 'Buy',
price: 0.06688964,
qtyProfile: [4.0]
}, {
side: 'Sell',
price: 0.06754710,
qtyProfile: [1.5]
}]
}
}, {
event: 'snapshot',
data: {
updateId: 57552,
symbol: 'XTZ/ETH',
entries: [{
side: 'Buy',
price: 0.66666666,
qtyProfile: [5.5]
}, {
side: 'Sell',
price: 0.77777777,
qtyProfile: [3.0]
}]
}
}],
[{
updateId: 57551,
symbol: 'ETH/BTC',
quoteCurrency: 'ETH',
baseCurrency: 'BTC',
entries: [
{
side: 'Buy',
price: new BigNumber(0.06688964),
qtyProfile: [4.0]
},
{
side: 'Sell',
price: new BigNumber(0.06754710),
qtyProfile: [1.5]
}
]
}, {
updateId: 57552,
symbol: 'XTZ/ETH',
quoteCurrency: 'XTZ',
baseCurrency: 'ETH',
entries: [
{
side: 'Buy',
price: new BigNumber(0.66666666),
qtyProfile: [5.5]
},
{
side: 'Sell',
price: new BigNumber(0.77777777),
qtyProfile: [3.0]
}
]
}]
]
],
];

export default validWsOrderBookSnapshotTestCases;
37 changes: 37 additions & 0 deletions tests/clients/webSocketAtomexClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { AtomexNetwork } from '../../src/common/index';
import { InMemoryOrderBookProvider } from '../../src/exchange/index';
import { TestCurrenciesProvider, TestAuthorizationManager, TestExchangeSymbolsProvider, wait } from '../testHelpers/index';
import {
validWsOrderBookSnapshotTestCases,
validWsOrderBookUpdatedTestCases, validWsOrderUpdatedTestCases,
validWsSwapUpdatedTestCases, validWsTopOfBookUpdatedTestCases
} from './testCases/index';
Expand Down Expand Up @@ -120,10 +121,12 @@ describe('WebSocket Atomex Client', () => {
const onOrderUpdatedCallback = jest.fn();
const onSwapUpdatedCallback = jest.fn();
const onTopOfBookUpdatedCallback = jest.fn();
const onOrderBookSnapshotCallback = jest.fn();
const onOrderBookUpdatedCallback = jest.fn();
client.events.orderUpdated.addListener(onOrderUpdatedCallback);
client.events.swapUpdated.addListener(onSwapUpdatedCallback);
client.events.topOfBookUpdated.addListener(onTopOfBookUpdatedCallback);
client.events.orderBookSnapshot.addListener(onOrderBookSnapshotCallback);
client.events.orderBookUpdated.addListener(onOrderBookUpdatedCallback);
authorizationManager.emitAuthorizedEvent(testAuthToken);
await exchangeWsServer.connected;
Expand All @@ -132,6 +135,7 @@ describe('WebSocket Atomex Client', () => {

expect(onSwapUpdatedCallback).toHaveBeenCalledTimes(0);
expect(onTopOfBookUpdatedCallback).toHaveBeenCalledTimes(0);
expect(onOrderBookSnapshotCallback).toHaveBeenCalledTimes(0);
expect(onOrderBookUpdatedCallback).toHaveBeenCalledTimes(0);
expect(onOrderUpdatedCallback).toHaveBeenCalledTimes(1);
expect(onOrderUpdatedCallback).toHaveBeenCalledWith(expectedOrder);
Expand All @@ -141,10 +145,12 @@ describe('WebSocket Atomex Client', () => {
const onOrderUpdatedCallback = jest.fn();
const onSwapUpdatedCallback = jest.fn();
const onTopOfBookUpdatedCallback = jest.fn();
const onOrderBookSnapshotCallback = jest.fn();
const onOrderBookUpdatedCallback = jest.fn();
client.events.orderUpdated.addListener(onOrderUpdatedCallback);
client.events.swapUpdated.addListener(onSwapUpdatedCallback);
client.events.topOfBookUpdated.addListener(onTopOfBookUpdatedCallback);
client.events.orderBookSnapshot.addListener(onOrderBookSnapshotCallback);
client.events.orderBookUpdated.addListener(onOrderBookUpdatedCallback);
authorizationManager.emitAuthorizedEvent(testAuthToken);

Expand All @@ -154,6 +160,7 @@ describe('WebSocket Atomex Client', () => {

expect(onOrderUpdatedCallback).toHaveBeenCalledTimes(0);
expect(onTopOfBookUpdatedCallback).toHaveBeenCalledTimes(0);
expect(onOrderBookSnapshotCallback).toHaveBeenCalledTimes(0);
expect(onOrderBookUpdatedCallback).toHaveBeenCalledTimes(0);
expect(onSwapUpdatedCallback).toHaveBeenCalledTimes(1);
expect(onSwapUpdatedCallback).toHaveBeenCalledWith(expectedSwap);
Expand All @@ -163,10 +170,12 @@ describe('WebSocket Atomex Client', () => {
const onOrderUpdatedCallback = jest.fn();
const onSwapUpdatedCallback = jest.fn();
const onTopOfBookUpdatedCallback = jest.fn();
const onOrderBookSnapshotCallback = jest.fn();
const onOrderBookUpdatedCallback = jest.fn();
client.events.orderUpdated.addListener(onOrderUpdatedCallback);
client.events.swapUpdated.addListener(onSwapUpdatedCallback);
client.events.topOfBookUpdated.addListener(onTopOfBookUpdatedCallback);
client.events.orderBookSnapshot.addListener(onOrderBookSnapshotCallback);
client.events.orderBookUpdated.addListener(onOrderBookUpdatedCallback);
authorizationManager.emitAuthorizedEvent(testAuthToken);

Expand All @@ -176,11 +185,39 @@ describe('WebSocket Atomex Client', () => {

expect(onOrderUpdatedCallback).toHaveBeenCalledTimes(0);
expect(onSwapUpdatedCallback).toHaveBeenCalledTimes(0);
expect(onOrderBookSnapshotCallback).toHaveBeenCalledTimes(0);
expect(onOrderBookUpdatedCallback).toHaveBeenCalledTimes(0);
expect(onTopOfBookUpdatedCallback).toHaveBeenCalledTimes(1);
expect(onTopOfBookUpdatedCallback).toHaveBeenCalledWith(expectedQuotes);
});

test.each(validWsOrderBookSnapshotTestCases)('emits orderBookSnapshot event with correct data (%s)', async (_, [snapshotDtos, expectedOrderBooks]) => {
const onOrderUpdatedCallback = jest.fn();
const onSwapUpdatedCallback = jest.fn();
const onTopOfBookUpdatedCallback = jest.fn();
const onOrderBookSnapshotCallback = jest.fn();
const onOrderBookUpdatedCallback = jest.fn();
client.events.orderUpdated.addListener(onOrderUpdatedCallback);
client.events.swapUpdated.addListener(onSwapUpdatedCallback);
client.events.topOfBookUpdated.addListener(onTopOfBookUpdatedCallback);
client.events.orderBookUpdated.addListener(onOrderBookUpdatedCallback);
client.events.orderBookSnapshot.addListener(onOrderBookSnapshotCallback);
authorizationManager.emitAuthorizedEvent(testAuthToken);

await exchangeWsServer.connected;

snapshotDtos.forEach(snapshot => exchangeWsServer.send(snapshot));

expect(onSwapUpdatedCallback).toHaveBeenCalledTimes(0);
expect(onOrderUpdatedCallback).toHaveBeenCalledTimes(0);
expect(onTopOfBookUpdatedCallback).toHaveBeenCalledTimes(0);
expect(onOrderBookUpdatedCallback).toHaveBeenCalledTimes(0);
expect(onOrderBookSnapshotCallback).toHaveBeenCalledTimes(snapshotDtos.length);
for (let i = 0; i < expectedOrderBooks.length; i++) {
expect(onOrderBookSnapshotCallback).toHaveBeenNthCalledWith(i + 1, expectedOrderBooks[i]);
}
});

test.each(validWsOrderBookUpdatedTestCases)('emits orderBookUpdated event with correct data (%s)', async (_, [snapshotDtos, entryDtos, expectedOrderBooks]) => {
const onOrderUpdatedCallback = jest.fn();
const onSwapUpdatedCallback = jest.fn();
Expand Down

0 comments on commit 50cdbc0

Please sign in to comment.