From 445749e5e85aa38ed7208659d937a384a35840b7 Mon Sep 17 00:00:00 2001 From: OGPoyraz Date: Thu, 16 Jan 2025 08:25:02 +0100 Subject: [PATCH] Fix suggestions --- .../src/ApprovalController.test.ts | 55 ++++++++++++++----- .../src/ApprovalController.ts | 8 +-- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/packages/approval-controller/src/ApprovalController.test.ts b/packages/approval-controller/src/ApprovalController.test.ts index 9dcb3510ce..9518deec3e 100644 --- a/packages/approval-controller/src/ApprovalController.test.ts +++ b/packages/approval-controller/src/ApprovalController.test.ts @@ -826,15 +826,29 @@ describe('approval controller', () => { origin: 'bar.baz', type: 'myType', }); - // TODO: Either fix this lint violation or explain why it's necessary to ignore. - // eslint-disable-next-line @typescript-eslint/no-floating-promises + approvalController.accept('foo', 'success'); const result = await approvalPromise; expect(result).toBe('success'); + }); + + it('emits "accepted" event', async () => { + const acceptedEvent = jest.fn(); + messenger.subscribe('ApprovalController:accepted', acceptedEvent); + + const approvalPromise = approvalController.add({ + id: 'foo', + origin: 'bar.baz', + type: 'myType', + }); + + approvalController.accept('foo', 'success'); + + await approvalPromise; expect(acceptedEvent.mock.calls[0][0]).toEqual({ - approval: expect.objectContaining({ + request: expect.objectContaining({ id: 'foo', origin: 'bar.baz', type: 'myType', @@ -855,15 +869,11 @@ describe('approval controller', () => { type: 'myType2', }); - // TODO: Either fix this lint violation or explain why it's necessary to ignore. - // eslint-disable-next-line @typescript-eslint/no-floating-promises approvalController.accept('foo2', 'success2'); let result = await approvalPromise2; expect(result).toBe('success2'); - // TODO: Either fix this lint violation or explain why it's necessary to ignore. - // eslint-disable-next-line @typescript-eslint/no-floating-promises approvalController.accept('foo1', 'success1'); result = await approvalPromise1; @@ -1093,15 +1103,32 @@ describe('approval controller', () => { }); approvalController.reject('foo', rejectedError); await expect(approvalPromise).rejects.toThrow(rejectedError); + }); - expect(rejectedEvent.mock.calls[0][0]).toEqual({ - approval: expect.objectContaining({ - id: 'foo', - origin: 'bar.baz', - type: TYPE, - }), - error: rejectedError, + it('emits "rejected" event', async () => { + const rejectedEvent = jest.fn(); + messenger.subscribe('ApprovalController:rejected', rejectedEvent); + + const rejectedError = new Error('failure'); + const approvalPromise = approvalController.add({ + id: 'foo', + origin: 'bar.baz', + type: TYPE, }); + approvalController.reject('foo', rejectedError); + + try { + await approvalPromise; + } catch (error) { + expect(rejectedEvent.mock.calls[0][0]).toEqual({ + request: expect.objectContaining({ + id: 'foo', + origin: 'bar.baz', + type: TYPE, + }), + error: rejectedError, + }); + } }); it('rejects multiple approval promises out of order', async () => { diff --git a/packages/approval-controller/src/ApprovalController.ts b/packages/approval-controller/src/ApprovalController.ts index 888fde6ee3..5c1e5498a0 100644 --- a/packages/approval-controller/src/ApprovalController.ts +++ b/packages/approval-controller/src/ApprovalController.ts @@ -332,7 +332,7 @@ export type ApprovalRejectedEvent = { type: `${typeof controllerName}:rejected`; payload: [ { - approval: ApprovalRequest; + request: ApprovalRequest; error: Error; }, ]; @@ -342,7 +342,7 @@ export type ApprovalAcceptedEvent = { type: `${typeof controllerName}:accepted`; payload: [ { - approval: ApprovalRequest; + request: ApprovalRequest; }, ]; }; @@ -752,7 +752,7 @@ export class ApprovalController extends BaseController< this.#delete(id); } this.messagingSystem.publish(`${controllerName}:accepted`, { - approval, + request: approval, }); }); } @@ -772,7 +772,7 @@ export class ApprovalController extends BaseController< this.#delete(id); callbacks.reject(error); this.messagingSystem.publish(`${controllerName}:rejected`, { - approval: rejectedApproval, + request: rejectedApproval, error: error as Error, }); }