Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bangtoven committed Nov 13, 2023
1 parent fd07d5a commit f88c1bf
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 31 deletions.
46 changes: 23 additions & 23 deletions packages/wallet-sdk/src/connection/WalletLinkConnection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@ import { ScopedLocalStorage } from '../lib/ScopedLocalStorage';
import { Session } from '../relay/Session';
import { APP_VERSION_KEY, WALLET_USER_NAME_KEY } from '../relay/WalletSDKRelayAbstract';
import { SessionConfig } from './SessionConfig';
import { WalletLinkConnection } from './WalletLinkConnection';
import { WalletLinkConnection, WalletLinkConnectionUpdateListener } from './WalletLinkConnection';
import { WalletLinkConnectionCipher } from './WalletLinkConnectionCipher';

const decryptMock = jest.fn().mockImplementation((text) => Promise.resolve(`decrypted ${text}`));

jest.spyOn(WalletLinkConnectionCipher.prototype, 'decrypt').mockImplementation(decryptMock);

describe('WalletLinkConnection', () => {
const session = new Session(new ScopedLocalStorage('test'));

const listener = {
linkedUpdated: jest.fn(),
connectedUpdated: jest.fn(),
handleResponseMessage: jest.fn(),
chainUpdated: jest.fn(),
accountUpdated: jest.fn(),
metadataUpdated: jest.fn(),
resetAndReload: jest.fn(),
};
const cipher = {
encrypt: jest.fn().mockImplementation((text) => Promise.resolve(`encrypted ${text}`)),
decrypt: jest.fn().mockImplementation((text) => Promise.resolve(`decrypted ${text}`)),
};
let connection: WalletLinkConnection;
let listener: WalletLinkConnectionUpdateListener;

beforeEach(() => {
jest.clearAllMocks();

connection = new WalletLinkConnection(session, 'http://link-api-url', listener);
(connection as any).cipher = cipher;
connection = new WalletLinkConnection(session, 'http://link-api-url', {
linkedUpdated: jest.fn(),
connectedUpdated: jest.fn(),
handleResponseMessage: jest.fn(),
chainUpdated: jest.fn(),
accountUpdated: jest.fn(),
metadataUpdated: jest.fn(),
resetAndReload: jest.fn(),
});
listener = (connection as any).listener;
});

describe('incomingDataListener', () => {
Expand Down Expand Up @@ -65,10 +66,9 @@ describe('WalletLinkConnection', () => {

invoke_handleSessionMetadataUpdated({ WalletUsername: newUsername });

expect(cipher.decrypt).toHaveBeenCalledWith(newUsername);
expect(listener_metadataUpdatedSpy).toHaveBeenCalledWith(
WALLET_USER_NAME_KEY,
await cipher.decrypt(newUsername)
await decryptMock(newUsername)
);
});

Expand All @@ -81,7 +81,7 @@ describe('WalletLinkConnection', () => {

expect(listener_metadataUpdatedSpy).toHaveBeenCalledWith(
APP_VERSION_KEY,
await cipher.decrypt(newAppVersion)
await decryptMock(newAppVersion)
);
});

Expand All @@ -100,7 +100,7 @@ describe('WalletLinkConnection', () => {

invoke_handleSessionMetadataUpdated({ EthereumAddress: newAccount });

expect(listener_accountUpdatedSpy).toHaveBeenCalledWith(await cipher.decrypt(newAccount));
expect(listener_accountUpdatedSpy).toHaveBeenCalledWith(await decryptMock(newAccount));
});

describe('chain updates', () => {
Expand All @@ -113,7 +113,7 @@ describe('WalletLinkConnection', () => {
invoke_handleSessionMetadataUpdated(chainIdUpdate);
invoke_handleSessionMetadataUpdated(jsonRpcUrlUpdate);

await cipher.decrypt(chainIdUpdate.ChainId);
await decryptMock(chainIdUpdate.ChainId);

expect(listener_chainUpdatedSpy).not.toHaveBeenCalled();
});
Expand All @@ -129,8 +129,8 @@ describe('WalletLinkConnection', () => {
invoke_handleSessionMetadataUpdated(update);

expect(listener_chainUpdatedSpy).toHaveBeenCalledWith(
await cipher.decrypt(update.ChainId),
await cipher.decrypt(update.JsonRpcUrl)
await decryptMock(update.ChainId),
await decryptMock(update.JsonRpcUrl)
);
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/wallet-sdk/src/connection/WalletLinkConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ export class WalletLinkConnection {
const message = JSON.parse(decryptedData);

if (!isWeb3ResponseMessage(message)) return;

this.listener?.handleResponseMessage(message);
} catch {
this.diagnostic?.log(EVENTS.GENERAL_ERROR, {
Expand Down
63 changes: 56 additions & 7 deletions packages/wallet-sdk/src/relay/WalletLinkRelay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@
import { ServerMessageEvent } from '../connection/ServerMessage';
import { SessionConfig } from '../connection/SessionConfig';
import { WalletLinkConnection } from '../connection/WalletLinkConnection';
import { WalletLinkConnectionCipher } from '../connection/WalletLinkConnectionCipher';
import { WalletLinkWebSocket } from '../connection/WalletLinkWebSocket';
import { ScopedLocalStorage } from '../lib/ScopedLocalStorage';
import { WalletLinkRelay, WalletLinkRelayOptions } from './WalletLinkRelay';
import { WALLET_USER_NAME_KEY } from './WalletSDKRelayAbstract';
import { WalletSDKRelayEventManager } from './WalletSDKRelayEventManager';

// mock isWeb3ResponseMessage to return true
jest.mock('./Web3ResponseMessage', () => ({
isWeb3ResponseMessage: jest.fn().mockReturnValue(true),
}));

const decryptMock = jest.fn().mockImplementation((text) => Promise.resolve(`"decrypted ${text}"`));

jest.spyOn(WalletLinkConnectionCipher.prototype, 'decrypt').mockImplementation(decryptMock);

describe('WalletLinkRelay', () => {
const options: WalletLinkRelayOptions = {
linkAPIUrl: 'http://link-api-url',
Expand Down Expand Up @@ -49,11 +60,16 @@ describe('WalletLinkRelay', () => {

const relay = new WalletLinkRelay(options);

const handleIncomingEventSpy = jest.spyOn(relay, <any>'handleIncomingEvent');
const handleWeb3ResponseMessageSpy = jest.spyOn(
(relay as any).listener,
'handleResponseMessage'
);

(relay as any).connection.ws.incomingDataListener?.(serverMessageEvent);

expect(handleIncomingEventSpy).toHaveBeenCalledWith(serverMessageEvent);
expect(handleWeb3ResponseMessageSpy).toHaveBeenCalledWith(
JSON.parse(await decryptMock(serverMessageEvent.data))
);
});

it('should set isLinked with LinkedListener', async () => {
Expand All @@ -70,6 +86,30 @@ describe('WalletLinkRelay', () => {
});

describe('setSessionConfigListener', () => {
it('should update metadata with new WalletUsername', async () => {
const sessionConfig: SessionConfig = {
webhookId: 'webhookId',
webhookUrl: 'webhookUrl',
metadata: {
WalletUsername: 'username',
},
};

const relay = new WalletLinkRelay(options);

const metadataUpdatedSpy = jest.spyOn((relay as any).listener, 'metadataUpdated');

(relay as any).connection.ws.incomingDataListener?.({
...sessionConfig,
type: 'SessionConfigUpdated',
});

expect(metadataUpdatedSpy).toHaveBeenCalledWith(
WALLET_USER_NAME_KEY,
await decryptMock(sessionConfig.metadata.WalletUsername)
);
});

it('should update chainId and jsonRpcUrl only when distinct', async () => {
const callback = jest.fn();
const relay = new WalletLinkRelay(options);
Expand All @@ -89,8 +129,10 @@ describe('WalletLinkRelay', () => {
...sessionConfig,
type: 'GetSessionConfigOK',
});
await new Promise((resolve) => setTimeout(resolve, 0));
expect(callback).toHaveBeenCalledWith('ChainId', 'JsonRpcUrl');
expect(callback).toHaveBeenCalledWith(
await decryptMock(sessionConfig.metadata.ChainId),
await decryptMock(sessionConfig.metadata.JsonRpcUrl)
);

// same chain id and json rpc url
(relay as any).connection.ws.incomingDataListener?.({
Expand All @@ -100,16 +142,23 @@ describe('WalletLinkRelay', () => {
expect(callback).toHaveBeenCalledTimes(1); // distinctUntilChanged

// different chain id and json rpc url
(relay as any).connection.ws.incomingDataListener?.({
const newSessionConfig = {
...sessionConfig,
metadata: {
ChainId: 'ChainId2',
JsonRpcUrl: 'JsonRpcUrl2',
},
};

(relay as any).connection.ws.incomingDataListener?.({
...newSessionConfig,
type: 'SessionConfigUpdated',
});
await new Promise((resolve) => setTimeout(resolve, 0));
expect(callback).toHaveBeenCalledWith('ChainId2', 'JsonRpcUrl2');

expect(callback).toHaveBeenCalledWith(
await decryptMock(newSessionConfig.metadata.ChainId),
await decryptMock(newSessionConfig.metadata.JsonRpcUrl)
);
expect(callback).toHaveBeenCalledTimes(2);
});
});
Expand Down
3 changes: 2 additions & 1 deletion packages/wallet-sdk/src/relay/WalletLinkRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ export class WalletLinkRelay extends WalletSDKRelayAbstract {
}

private listener: WalletLinkConnectionUpdateListener = {
handleResponseMessage: this.handleWeb3ResponseMessage,

linkedUpdated: (linked: boolean) => {
this.isLinked = linked;
const cachedAddresses = this.storage.getItem(LOCAL_STORAGE_ADDRESSES_KEY);
Expand Down Expand Up @@ -220,7 +222,6 @@ export class WalletLinkRelay extends WalletSDKRelayAbstract {
ui.setConnected(connected);
}
},
handleResponseMessage: this.handleWeb3ResponseMessage,
resetAndReload: this.resetAndReload,
};

Expand Down

0 comments on commit f88c1bf

Please sign in to comment.