Skip to content

Commit

Permalink
chore: improve open API and add tests, ref leather-io/issues#5800
Browse files Browse the repository at this point in the history
  • Loading branch information
pete-watters committed Sep 3, 2024
1 parent 6b9566a commit 9d52d9e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 27 deletions.
27 changes: 9 additions & 18 deletions src/background/messaging/rpc-methods/open.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
import { RpcErrorCode } from '@btckit/types';

import { RouteUrls } from '@shared/route-urls';
import { OpenRequest } from '@shared/rpc/methods/open';
import { makeRpcErrorResponse } from '@shared/rpc/rpc-methods';
import { makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';

import {
listenForPopupClose,
makeSearchParamsWithDefaults,
triggerRequestWindowOpen,
} from '../messaging-utils';
import { makeSearchParamsWithDefaults, triggerRequestWindowOpen } from '../messaging-utils';
import { trackRpcRequestSuccess } from '../rpc-message-handler';

export async function rpcOpen(message: OpenRequest, port: chrome.runtime.Port) {
const { urlParams, tabId } = makeSearchParamsWithDefaults(port, [['requestId', message.id]]);
const { id } = await triggerRequestWindowOpen(RouteUrls.Home, urlParams);

await triggerRequestWindowOpen(RouteUrls.Home, urlParams);
void trackRpcRequestSuccess({ endpoint: message.method });

listenForPopupClose({
chrome.tabs.sendMessage(
tabId,
id,
response: makeRpcErrorResponse('open', {
makeRpcSuccessResponse('open', {
id: message.id,
error: {
code: RpcErrorCode.USER_REJECTION,
message: 'User rejected request to open wallet',
},
}),
});
result: { message: 'Success' },
})
);
}
11 changes: 2 additions & 9 deletions src/shared/rpc/methods/open.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { DefineRpcMethod, RpcRequest, RpcResponse } from '@btckit/types';
import * as yup from 'yup';

const rpcOpenParamsSchema = yup.object().shape({
url: yup.string(),
});
export type OpenRequest = RpcRequest<'open'>;

type OpenRequestParams = yup.InferType<typeof rpcOpenParamsSchema>;

export type OpenRequest = RpcRequest<'open', OpenRequestParams>;

type OpenResponse = RpcResponse<Response>;
type OpenResponse = RpcResponse<{ message: string }>;

export type Open = DefineRpcMethod<OpenRequest, OpenResponse>;
62 changes: 62 additions & 0 deletions tests/specs/rpc-open/open.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { BrowserContext, Page } from '@playwright/test';
import { TEST_PASSWORD } from '@tests/mocks/constants';
import { HomePageSelectors } from '@tests/selectors/home.selectors';

import { test } from '../../fixtures/fixtures';

const successResponse = {
jsonrpc: '2.0',
result: {
message: 'Success',
},
};

async function interceptRequestPopup(context: BrowserContext) {
return context.waitForEvent('page');
}

async function assertWalletHomeOpens(popup: Page) {
await popup.getByTestId(HomePageSelectors.HomePageContainer).waitFor();
const button = popup.getByTestId(HomePageSelectors.FundAccountBtn);
await test.expect(button).toBeVisible();
}
async function initiateOpen(page: Page) {
return page.evaluate(async () => (window as any).LeatherProvider?.request('open'));
}

test.describe('Rpc: Open', () => {
test.beforeEach(async ({ extensionId, globalPage, onboardingPage }) => {
await globalPage.setupAndUseApiCalls(extensionId);

await onboardingPage.signInWithTestAccount(extensionId);
});

test('the wallet opens successfully', async ({ page, context }) => {
await page.goto('localhost:3000');
const openPromise = await initiateOpen(page);

const popup = await interceptRequestPopup(context);
await assertWalletHomeOpens(popup);

const result = await openPromise;
if (!result) throw new Error('Expected result');
const { id, ...payloadWithoutId } = result;

await test.expect(payloadWithoutId).toEqual(successResponse);
});

test('it forces user to unlock wallet when wallet is locked', async ({
homePage,
page,
context,
}) => {
await homePage.lock();
await page.goto('localhost:3000');
const openPromise = initiateOpen(page);
const popup = await interceptRequestPopup(context);
await popup.getByRole('textbox').fill(TEST_PASSWORD);
await popup.getByRole('button', { name: 'Continue' }).click();
await assertWalletHomeOpens(popup);
await test.expect(openPromise).resolves.toMatchObject(successResponse);
});
});

0 comments on commit 9d52d9e

Please sign in to comment.