Skip to content

Commit

Permalink
chore: uses await instead of .then and adds coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherferreira9 committed Jan 8, 2025
1 parent 3a2d161 commit 8d1b9b3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { TrackingEvents } from '@metamask/sdk-communication-layer';
import { MetaMaskSDK } from '../../../sdk';
import { SDKProvider } from '../../../provider/SDKProvider';
import { getBrowserExtension } from '../../../utils/get-browser-extension';
import { EXTENSION_EVENTS, RPC_METHODS } from '../../../config';
import { setupExtensionPreferences } from './setupExtensionPreferences';

jest.mock('../../../utils/get-browser-extension');
Expand Down Expand Up @@ -81,4 +83,36 @@ describe('setupExtensionPreferences', () => {
event: TrackingEvents.SDK_USE_EXTENSION,
});
});

it('should terminate instance if permissions are revoked', async () => {
const mockTerminate = jest.fn().mockResolvedValue(undefined);
const mockRequest = jest.fn().mockResolvedValue([]);

instance.getProvider = jest.fn().mockReturnValue({
request: mockRequest,
}) as unknown as () => SDKProvider;

instance.terminate = mockTerminate;
instance.extensionActive = true; // Ensure extension is active

const mockBrowserExtension = {
on: jest.fn((event: string, callback: (accounts: string[]) => void) => {
if (event === EXTENSION_EVENTS.ACCOUNTS_CHANGED) {
// Directly call the callback to simulate the event
callback([]);
}
}),
};

(getBrowserExtension as jest.Mock).mockReturnValue(mockBrowserExtension);

await setupExtensionPreferences(instance);

expect(mockRequest).toHaveBeenCalledWith({
method: RPC_METHODS.WALLET_GETPERMISSIONS,
params: [],
});

expect(mockTerminate).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export async function setupExtensionPreferences(instance: MetaMaskSDK) {

metamaskBrowserExtension.on(
EXTENSION_EVENTS.ACCOUNTS_CHANGED,
(accounts) => {
async (accounts) => {
logger(
`[MetaMaskSDK: setupExtensionPreferences()] PROPAGATE accountsChanged accounts=${accounts}`,
);
Expand All @@ -84,37 +84,28 @@ export async function setupExtensionPreferences(instance: MetaMaskSDK) {
}

if (isExtensionActive && (accounts as string[])?.length === 0) {
instance
const getPermissionsResponse = await instance
.getProvider()
?.request({
method: RPC_METHODS.WALLET_GETPERMISSIONS,
params: [],
})
.then((getPermissionsResponse) => {
const permissions = getPermissionsResponse as {
caveats: { type: string; value: string[] }[];
parentCapability: string;
}[];
logger(
`[MetaMaskSDK: setupExtensionPreferences()] permissions`,
permissions,
);
});

const permissions = getPermissionsResponse as {
caveats: { type: string; value: string[] }[];
parentCapability: string;
}[];

if (permissions.length === 0) {
instance.terminate().catch((error) => {
logger(
`[MetaMaskSDK: setupExtensionPreferences()] error terminating on permissions revoked`,
error,
);
});
}
})
.catch((error) => {
if (permissions.length === 0) {
try {
await instance.terminate();
} catch (error) {
logger(

Check warning on line 103 in packages/sdk/src/services/MetaMaskSDK/InitializerManager/setupExtensionPreferences.ts

View check run for this annotation

Codecov / codecov/patch

packages/sdk/src/services/MetaMaskSDK/InitializerManager/setupExtensionPreferences.ts#L103

Added line #L103 was not covered by tests
`[MetaMaskSDK: setupExtensionPreferences()] error getting permissions`,
`[MetaMaskSDK: setupExtensionPreferences()] error terminating on permissions revoked`,
error,
);
});
}
}
}
},
);
Expand Down

0 comments on commit 8d1b9b3

Please sign in to comment.