Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
test redirectUser callback
Browse files Browse the repository at this point in the history
  • Loading branch information
owencraston committed Oct 10, 2023
1 parent a0e962e commit 526d214
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
27 changes: 17 additions & 10 deletions src/SnapKeyring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('SnapKeyring', () => {
await handleUserInput(true);
return Promise.resolve();
}),
redirectUser: jest.fn(async () => Promise.resolve()),
};

const snapId = 'local:snap.mock';
Expand Down Expand Up @@ -239,15 +240,10 @@ describe('SnapKeyring', () => {
});

it.each([
[
{ message: 'Go to dapp to continue.' },
'The snap requested a redirect: message="Go to dapp to continue.", url=""',
],
[
{ url: 'https://example.com/sign?tx=1234' },
'The snap requested a redirect: message="", url="https://example.com/sign?tx=1234"',
],
])('returns a redirect %s', async (redirect, message) => {
[{ message: 'Go to dapp to continue.' }],
[{ url: 'https://example.com/sign?tx=1234' }],
[{ url: 'https://example.com/sign?tx=12345', message: 'Go to dapp.' }],
])('returns a redirect %s', async (redirect) => {
const spy = jest.spyOn(console, 'log').mockImplementation();

mockSnapController.handleRequest.mockResolvedValue({
Expand All @@ -266,14 +262,25 @@ describe('SnapKeyring', () => {
params: { id: requestId },
});

const { url = '', message = '' } = redirect as {
url?: string;
message?: string;
};

// We need to await on the request promise because the request submission
// is async, so if we don't await, the test will exit before the promise
// gets resolved.
await expect(requestPromise).rejects.toThrow(
'Request rejected by user or snap.',
);

expect(console.log).toHaveBeenCalledWith(message);
// Check that `redirectUser` was called with the correct parameters
expect(mockCallbacks.redirectUser).toHaveBeenCalledWith(
snapId,
url,
message,
expect.any(String), // if you don’t know `method` value in test context
);
spy.mockRestore();
});

Expand Down
10 changes: 2 additions & 8 deletions src/SnapKeyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,14 +453,8 @@ export class SnapKeyring extends EventEmitter {
// In the future, this should be handled by the UI. For now, we just log
// the redirect information for debugging purposes.
if (response.redirect?.message || response.redirect?.url) {
const { message, url } = response.redirect;
if (message && url) {
await this.#callbacks.redirectUser(snapId, url, message, method);
} else {
console.warn(
`Snap '${snapId}' requested a redirect but the message or URL is missing.`,
);
}
const { message = '', url = '' } = response.redirect;
await this.#callbacks.redirectUser(snapId, url, message, method);
}

return promise.promise;
Expand Down

0 comments on commit 526d214

Please sign in to comment.