Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: beacon-wallet open handles in tests #3015

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', () => {
const wallet = new BeaconWallet({ name: 'testWallet' });
Expand Down
Loading