Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bangtoven committed Sep 29, 2023
1 parent 0415478 commit 75edd5f
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions packages/wallet-sdk/src/connection/RxWebSocket.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import WS from 'jest-websocket-mock';
import { Observable } from 'rxjs';

import { ConnectionState, RxWebSocket } from './RxWebSocket';

Expand All @@ -17,27 +16,15 @@ describe('RxWebSocket', () => {

describe('is connected', () => {
test('@connect & @disconnect', async () => {
const connectionStateListener = jest.fn();
rxWS.setConnectionStateListener(connectionStateListener);

const client = rxWS.connect();

expect(client).toBeInstanceOf(Observable);
await client.toPromise();
await client;
await server.connected;

// @ts-expect-error test private methods
expect(rxWS.webSocket).toBeInstanceOf(WebSocket);
// @ts-expect-error test private methods
rxWS.connectionStateSubject
.subscribe({
next: (val) => {
// Connected state
expect(val).toEqual(ConnectionState.CONNECTED);
},
closed: (val: ConnectionState) => {
// Disconnected state
expect(val).toEqual(ConnectionState.DISCONNECTED);
},
})
.unsubscribe();
expect(connectionStateListener).toHaveBeenCalledWith(ConnectionState.CONNECTED);

// Sends data
const webSocketSendMock = jest
Expand All @@ -49,54 +36,67 @@ describe('RxWebSocket', () => {

// Disconnects
rxWS.disconnect();
expect(connectionStateListener).toHaveBeenCalledWith(ConnectionState.DISCONNECTED);
// @ts-expect-error test private methods
expect(rxWS.webSocket).toBe(null);
});

test('@connectionState$ & @incomingData$', () => {
expect(rxWS.connectionState$).toBeInstanceOf(Observable);
expect(rxWS.incomingData$).toBeInstanceOf(Observable);
});

test('@incomingJSONData$', () => {
expect(rxWS.incomingJSONData$).toBeInstanceOf(Observable);
});

describe('errors & event listeners', () => {
afterEach(() => rxWS.disconnect());

test('@connect throws error when connecting again', async () => {
const client = rxWS.connect();
await client.toPromise();
await client;

await expect(rxWS.connect().toPromise()).rejects.toThrow('webSocket object is not null');
await expect(rxWS.connect()).rejects.toThrow('webSocket object is not null');
});

test('@connect throws error & fails to set websocket instance', async () => {
const errorConnect = new RxWebSocket('');

await expect(errorConnect.connect().toPromise()).rejects.toThrow(
await expect(errorConnect.connect()).rejects.toThrow(
"Failed to construct 'WebSocket': 1 argument required, but only 0 present."
);
});

test('onclose event throws error', async () => {
const client = rxWS.connect();
await client.toPromise();
await client;
await server.connected;
server.error();

await expect(rxWS.connect().toPromise()).rejects.toThrow('websocket error 1000: ');
await expect(rxWS.connect()).rejects.toThrow('websocket error 1000: ');
});

test('onmessage event emits message', async () => {
const incomingDataListener = jest.fn();
rxWS.setIncomingDataListener(incomingDataListener);

const client = rxWS.connect();
await client;
await server.connected;

const message = {
type: 'ServerMessageType',
data: 'hello world',
};

server.send(JSON.stringify(message));
expect(incomingDataListener).toHaveBeenCalledWith(message);
});

test('onmessage event emits heartbeat message', async () => {
const incomingDataListener = jest.fn();
rxWS.setIncomingDataListener(incomingDataListener);

const client = rxWS.connect();
await client.toPromise();
await client;
await server.connected;

// @ts-expect-error test private methods
rxWS.incomingDataSubject.subscribe((val) => expect(val).toEqual('hello world'));
server.send('hello world');
server.send('h');
expect(incomingDataListener).toHaveBeenCalledWith({
type: 'Heartbeat',
});
});
});
});
Expand Down

0 comments on commit 75edd5f

Please sign in to comment.