Skip to content

Commit

Permalink
added mock for beacon wallet tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dsawali committed Aug 26, 2024
1 parent 5d61f88 commit 943d5fd
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
47 changes: 47 additions & 0 deletions packages/taquito-beacon-wallet/test/__mocks__/broadcast-channel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
interface MessageListener {
(message: any): void;
}

class BroadcastChannel {
name: string;
listeners: MessageListener[];

constructor(name: string) {
this.name = name;
this.listeners = [];
}

postMessage(message: any) {
// Mock implementation of postMessage
this.listeners.forEach((listener) => listener(message));
}

addEventListener(event: string, listener: MessageListener) {
if (event === 'message') {
this.listeners.push(listener);
}
}

removeEventListener(event: string, listener: MessageListener) {
if (event === 'message') {
this.listeners = this.listeners.filter((l) => l !== listener);
}
}

close() {
// Mock implementation of close
this.listeners = [];
}
}

function createLeaderElection(_channel: BroadcastChannel) {
// Mock implementation of createLeaderElection
return {
awaitLeadership: jest.fn(),
hasLeader: jest.fn().mockReturnValue(true),
isLeader: jest.fn().mockReturnValue(true),
die: jest.fn(),
};
}

export { BroadcastChannel, createLeaderElection };
19 changes: 19 additions & 0 deletions packages/taquito-beacon-wallet/test/taquito-beacon-wallet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ global.localStorage = new LocalStorageMock();
global.indexedDB = indexedDB;
global.window = { addEventListener: jest.fn() } as any;

jest.mock('@airgap/beacon-transport-postmessage', () => {
jest.useFakeTimers()
const originalModule = jest.requireActual('@airgap/beacon-transport-postmessage');
jest.runAllTimers()

return {
...originalModule,
PostMessageTransport: jest.fn().mockImplementation(() => {
return {
connect: jest.fn(),
startOpenChannelListener: jest.fn(),
getPairingRequestInfo: jest.fn(),
listen: jest.fn(),
};
}),
getAvailableExtensions: jest.fn(),
};
});

describe('Beacon Wallet tests', () => {
it('Verify that BeaconWallet is instantiable', () => {
expect(new BeaconWallet({ name: 'testWallet' })).toBeInstanceOf(BeaconWallet);
Expand Down

0 comments on commit 943d5fd

Please sign in to comment.