Skip to content

Commit

Permalink
Add unit tests for new added events
Browse files Browse the repository at this point in the history
  • Loading branch information
OGPoyraz committed Jan 13, 2025
1 parent ae14430 commit e0611fa
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions packages/approval-controller/src/ApprovalController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
AddApprovalOptions,
ApprovalControllerActions,
ApprovalControllerEvents,
ApprovalControllerMessenger,
ErrorOptions,
StartFlowOptions,
SuccessOptions,
Expand Down Expand Up @@ -243,15 +244,17 @@ function getRestrictedMessenger() {
describe('approval controller', () => {
let approvalController: ApprovalController;
let showApprovalRequest: jest.Mock;
let messenger: ApprovalControllerMessenger;

beforeEach(() => {
nanoidMock.mockReturnValue('TestId');
jest.spyOn(global.console, 'info').mockImplementation(() => undefined);

messenger = getRestrictedMessenger();
showApprovalRequest = jest.fn();

approvalController = new ApprovalController({
messenger: getRestrictedMessenger(),
messenger,
showApprovalRequest,
});
});
Expand Down Expand Up @@ -815,6 +818,9 @@ describe('approval controller', () => {

describe('accept', () => {
it('resolves approval promise', async () => {
const acceptedEvent = jest.fn();
messenger.subscribe('ApprovalController:accepted', acceptedEvent);

const approvalPromise = approvalController.add({
id: 'foo',
origin: 'bar.baz',
Expand All @@ -826,6 +832,15 @@ describe('approval controller', () => {

const result = await approvalPromise;
expect(result).toBe('success');

expect(acceptedEvent.mock.calls[0][0]).toEqual({
approval: expect.objectContaining({
id: 'foo',
origin: 'bar.baz',
type: 'myType',
time: expect.any(Number),
}),
});
});

it('resolves multiple approval promises out of order', async () => {
Expand Down Expand Up @@ -1067,13 +1082,26 @@ describe('approval controller', () => {

describe('reject', () => {
it('rejects approval promise', 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', new Error('failure'));
await expect(approvalPromise).rejects.toThrow('failure');
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('rejects multiple approval promises out of order', async () => {
Expand Down

0 comments on commit e0611fa

Please sign in to comment.