From 3c55207293107f0c65640e21ea1f33f90eb08276 Mon Sep 17 00:00:00 2001 From: Bernardo Garces Chapero Date: Thu, 4 May 2023 09:56:31 +0100 Subject: [PATCH] better types --- src/ledger-iframe-bridge.test.ts | 27 +++++++++++++++++++-------- src/ledger-iframe-bridge.ts | 2 +- src/ledger-keyring.test.ts | 14 +------------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/ledger-iframe-bridge.test.ts b/src/ledger-iframe-bridge.test.ts index ebe67e37..d57c57a6 100644 --- a/src/ledger-iframe-bridge.test.ts +++ b/src/ledger-iframe-bridge.test.ts @@ -1,6 +1,9 @@ import { hasProperty } from '@metamask/utils'; -import { LedgerIframeBridge } from './ledger-iframe-bridge'; +import { + IFrameMessageAction, + LedgerIframeBridge, +} from './ledger-iframe-bridge'; import documentShim from '../test/document.shim'; import windowShim from '../test/window.shim'; @@ -113,14 +116,16 @@ describe('LedgerIframeBridge', function () { it('sends and processes a successful ledger-make-app message', async function () { stubKeyringIFramePostMessage(bridge, (message) => { expect(message).toStrictEqual({ - action: 'ledger-make-app', + action: IFrameMessageAction.LedgerMakeApp, messageId: 1, target: 'LEDGER-IFRAME', }); bridge.messageCallbacks[message.messageId]?.({ + action: IFrameMessageAction.LedgerMakeApp, + messageId: 1, success: true, - } as any); + }); }); const result = await bridge.attemptMakeApp(); @@ -136,15 +141,17 @@ describe('LedgerIframeBridge', function () { stubKeyringIFramePostMessage(bridge, (message) => { expect(message).toStrictEqual({ - action: 'ledger-make-app', + action: IFrameMessageAction.LedgerMakeApp, messageId: 1, target: 'LEDGER-IFRAME', }); bridge.messageCallbacks[message.messageId]?.({ + action: IFrameMessageAction.LedgerMakeApp, + messageId: 1, error: new Error(errorMessage), success: false, - } as any); + }); }); await expect(bridge.attemptMakeApp()).rejects.toThrow(errorMessage); @@ -162,15 +169,17 @@ describe('LedgerIframeBridge', function () { stubKeyringIFramePostMessage(bridge, (message) => { expect(message).toStrictEqual({ - action: 'ledger-update-transport', + action: IFrameMessageAction.LedgerUpdateTransport, params: { transportType }, messageId: 1, target: 'LEDGER-IFRAME', }); bridge.messageCallbacks[message.messageId]?.({ + action: IFrameMessageAction.LedgerUpdateTransport, + messageId: 1, success: true, - } as any); + }); }); const result = await bridge.updateTransportMethod(transportType); @@ -195,8 +204,10 @@ describe('LedgerIframeBridge', function () { }); bridge.messageCallbacks[message.messageId]?.({ + action: IFrameMessageAction.LedgerUpdateTransport, + messageId: 1, success: false, - } as any); + }); }); await expect(bridge.updateTransportMethod(transportType)).rejects.toThrow( diff --git a/src/ledger-iframe-bridge.ts b/src/ledger-iframe-bridge.ts index 854bc14b..259e9fb7 100644 --- a/src/ledger-iframe-bridge.ts +++ b/src/ledger-iframe-bridge.ts @@ -15,7 +15,7 @@ import { const LEDGER_IFRAME_ID = 'LEDGER-IFRAME'; -enum IFrameMessageAction { +export enum IFrameMessageAction { LedgerConnectionChange = 'ledger-connection-change', LedgerUnlock = 'ledger-unlock', LedgerMakeApp = 'ledger-make-app', diff --git a/src/ledger-keyring.test.ts b/src/ledger-keyring.test.ts index f6481698..4c15cd8f 100644 --- a/src/ledger-keyring.test.ts +++ b/src/ledger-keyring.test.ts @@ -93,19 +93,7 @@ describe('LedgerKeyring', function () { } beforeEach(async function () { - // eslint-disable-next-line @typescript-eslint/no-empty-function - const noop = () => {}; - bridge = { - init: noop, - destroy: noop, - attemptMakeApp: noop, - updateTransportMethod: noop, - getPublicKey: noop, - deviceSignTransaction: noop, - deviceSignMessage: noop, - deviceSignTypedData: noop, - } as any; - + bridge = new LedgerIframeBridge(); keyring = new LedgerKeyring({ bridge }); keyring.hdk = fakeHdKey; await keyring.deserialize();