Skip to content

Commit

Permalink
chore: websocket client unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ifaouibadi committed Jul 13, 2023
1 parent 1b6c50e commit 1267808
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/lib/WebSocketClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ class WebSocketClient {
this.subscribers[message.payload][uuid] = callback;
return () => {
delete this.subscribers[message.payload][uuid];
if (Object.keys(this.subscribers[message.payload]).length === 0) {
// should remove the message from the queue if there are no subscribers
this.subscribersQueue = this.subscribersQueue.filter(
(msg) => msg.payload !== message.payload,
);

// should unsubscribe from the channel if there are no subscribers
Object.keys(WEB_SOCKET_SOURCE).forEach((source) => {
this.wsClient.send(JSON.stringify({
...message,
op: WEB_SOCKET_UNSUBSCRIBE,
source,
}));
});
}
};
}

Expand Down
106 changes: 106 additions & 0 deletions tests/unit/websocket-client.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import WebSocketClient from '../../src/lib/WebSocketClient';
import {
NETWORK_TESTNET,
WEB_SOCKET_SOURCE,
WEB_SOCKET_CHANNELS,
} from '../../src/popup/utils';

const testAddress = 'ak_2fxchiLvnj9VADMAXHBiKPsaCEsTFehAspcmWJ3ZzF3pFK1hB5';

const WEB_SOCKET_SOURCE_COUNT = Object.keys(WEB_SOCKET_SOURCE).length;

describe('WebSocketClient', () => {
beforeAll(async () => {
WebSocketClient.connect(NETWORK_TESTNET.websocketUrl);
await new Promise((resolve) => {
WebSocketClient.wsClient.onopen = () => {
resolve();
};
});
});

afterEach(() => {
jest.clearAllMocks();
});

describe('Can connect to ', () => {
it('should connect and set isWsConnected to true', () => {
expect(WebSocketClient.isWsConnected).toBe(true);
});
});

describe('Can handle transactions subscription', () => {
let transactionChannelUnsubscribe;
it('should subscribe for transactions', () => {
const mockSend = jest.spyOn(WebSocketClient.wsClient, 'send');
transactionChannelUnsubscribe = WebSocketClient.subscribeForTransactionsUpdates(
jest.fn(),
);
expect(mockSend).toHaveBeenCalledTimes(WEB_SOCKET_SOURCE_COUNT);
expect(Object.keys(WebSocketClient.subscribers[WEB_SOCKET_CHANNELS.Transactions]))
.toHaveLength(1);
expect(WebSocketClient.subscribersQueue)
.toHaveLength(1);
});

it('should unsubscribe from transactions', () => {
const mockSend = jest.spyOn(WebSocketClient.wsClient, 'send');
transactionChannelUnsubscribe();
expect(mockSend).toHaveBeenCalledTimes(WEB_SOCKET_SOURCE_COUNT);
expect(Object.keys(WebSocketClient.subscribers[WEB_SOCKET_CHANNELS.Transactions]))
.toHaveLength(0);
expect(WebSocketClient.subscribersQueue)
.toHaveLength(0);
});
});

describe('Can handle micro blocks subscription', () => {
let microBlocksChannelUnsubscribe;
it('should subscribe for micro blocks', () => {
const mockSend = jest.spyOn(WebSocketClient.wsClient, 'send');
microBlocksChannelUnsubscribe = WebSocketClient.subscribeForMicroBlocksUpdates(
jest.fn(),
);
expect(mockSend).toHaveBeenCalledTimes(WEB_SOCKET_SOURCE_COUNT);
expect(Object.keys(WebSocketClient.subscribers[WEB_SOCKET_CHANNELS.MicroBlocks]))
.toHaveLength(1);
expect(WebSocketClient.subscribersQueue)
.toHaveLength(1);
});

it('should unsubscribe from micro blocks', () => {
const mockSend = jest.spyOn(WebSocketClient.wsClient, 'send');
microBlocksChannelUnsubscribe();
expect(mockSend).toHaveBeenCalledTimes(WEB_SOCKET_SOURCE_COUNT);
expect(Object.keys(WebSocketClient.subscribers[WEB_SOCKET_CHANNELS.MicroBlocks]))
.toHaveLength(0);
expect(WebSocketClient.subscribersQueue)
.toHaveLength(0);
});
});

describe('Can handle account transactions subscription', () => {
let transactionChannelUnsubscribe;
it('should subscribe for account transactions', () => {
const mockSend = jest.spyOn(WebSocketClient.wsClient, 'send');
transactionChannelUnsubscribe = WebSocketClient.subscribeForAccountUpdates(
testAddress, jest.fn(),
);
expect(mockSend).toHaveBeenCalledTimes(WEB_SOCKET_SOURCE_COUNT);
expect(Object.keys(WebSocketClient.subscribers[WEB_SOCKET_CHANNELS.Object]))
.toHaveLength(1);
expect(WebSocketClient.subscribersQueue)
.toHaveLength(1);
});

it('should unsubscribe from account transactions', () => {
const mockSend = jest.spyOn(WebSocketClient.wsClient, 'send');
transactionChannelUnsubscribe();
expect(mockSend).toHaveBeenCalledTimes(WEB_SOCKET_SOURCE_COUNT);
expect(Object.keys(WebSocketClient.subscribers[WEB_SOCKET_CHANNELS.Object]))
.toHaveLength(0);
expect(WebSocketClient.subscribersQueue)
.toHaveLength(0);
});
});
});

0 comments on commit 1267808

Please sign in to comment.