Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
56 changes: 36 additions & 20 deletions packages/gator-permissions-snap/src/core/confirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class ConfirmationDialog {
const isConfirmationGranted = new Promise<boolean>((resolve, reject) => {
// cleanup can't be defined before the click handlers, so cannot be const
// eslint-disable-next-line prefer-const
let cleanup: () => Promise<void>;
let cleanup: (resolveInterface?: boolean) => Promise<void>;

const { unbind: unbindGrantButtonClick } = this.#userEventDispatcher.on({
elementName: ConfirmationDialog.#grantButton,
Expand All @@ -94,32 +94,40 @@ export class ConfirmationDialog {
},
});

cleanup = async () => {
// store hooks so we can close/reject programmatically on error
this.#unbindHandlers = () => {
unbindGrantButtonClick();
unbindCancelButtonClick();
};

// clear our stored unbind handler reference
this.#unbindHandlers = undefined;
cleanup = async (resolveInterface = true) => {
if (this.#unbindHandlers) {
try {
this.#unbindHandlers();
} catch {
// ignore
} finally {
// clear our stored unbind handler reference
this.#unbindHandlers = undefined;
}
}

try {
await this.#snaps.request({
method: 'snap_resolveInterface',
params: {
id: interfaceId,
value: {},
},
});
} catch (error) {
const reason = error as Error;
reject(reason);
if (resolveInterface) {
try {
await this.#snaps.request({
method: 'snap_resolveInterface',
params: {
id: interfaceId,
value: {},
},
});
} catch (error) {
const reason = error as Error;
reject(reason);
}
}
};

// store hooks so we can close/reject programmatically on error
this.#unbindHandlers = () => {
unbindGrantButtonClick();
unbindCancelButtonClick();
};
this.#decisionReject = reject;

// we don't await this, because we only want to present the dialog, and
Expand All @@ -131,6 +139,14 @@ export class ConfirmationDialog {
id: interfaceId,
},
})
.then(async (result) => {
// Should resolve with false when dialog is closed.
if (result === null) {
await cleanup(false);

resolve(false);
}
})
.catch((error) => {
const reason = error as Error;
reject(reason);
Expand Down
19 changes: 19 additions & 0 deletions packages/gator-permissions-snap/test/core/confirmation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ describe('ConfirmationDialog', () => {
expect(result).toStrictEqual({ isConfirmationGranted: false });
});

it('should resolve with false when dialog is closed', async () => {
// Simulate dialog closure
mockSnaps.request.mockResolvedValueOnce(null);

const awaitingUserDecision =
confirmationDialog.displayConfirmationDialogAndAwaitUserDecision();

expect(mockUserEventDispatcher.on).toHaveBeenCalledTimes(2);
expect(mockUnbindFunctions).toHaveLength(2);

const result = await awaitingUserDecision;

mockUnbindFunctions.forEach((mockUnbindFn) => {
expect(mockUnbindFn).toHaveBeenCalledTimes(1);
});

expect(result).toStrictEqual({ isConfirmationGranted: false });
});

it('should clean up event listeners after decision', async () => {
const awaitingUserDecision =
confirmationDialog.displayConfirmationDialogAndAwaitUserDecision();
Expand Down
Loading