Skip to content

Commit

Permalink
Add missing hook and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz committed Dec 18, 2024
1 parent 7b60d82 commit c7c2c50
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/snaps-rpc-methods/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module.exports = deepmerge(baseConfig, {
],
coverageThreshold: {
global: {
branches: 92.95,
functions: 97.44,
branches: 92.98,
functions: 97.46,
lines: 97.93,
statements: 97.52,
},
Expand Down
69 changes: 69 additions & 0 deletions packages/snaps-simulation/src/simulation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,75 @@ describe('getPermittedHooks', () => {
const { runSaga, store } = createStore(getMockOptions());
const controllerMessenger = getRootControllerMessenger();

it('returns the `hasPermission` hook', async () => {
const { snapId, close } = await getMockServer({
manifest: getSnapManifest(),
});

const location = detectSnapLocation(snapId, {
allowLocal: true,
});

const snapFiles = await fetchSnap(snapId, location);

const { hasPermission } = getPermittedHooks(
snapId,
snapFiles,
controllerMessenger,
runSaga,
);

expect(hasPermission('snap_manageState')).toBe(true);

await close();
});

it('returns the `getUnlockPromise` hook', async () => {
const { snapId, close } = await getMockServer({
manifest: getSnapManifest(),
});

const location = detectSnapLocation(snapId, {
allowLocal: true,
});

const snapFiles = await fetchSnap(snapId, location);

const { getUnlockPromise } = getPermittedHooks(
snapId,
snapFiles,
controllerMessenger,
runSaga,
);

expect(await getUnlockPromise(true)).toBeUndefined();

await close();
});

it('returns the `getIsLocked` hook', async () => {
const { snapId, close } = await getMockServer({
manifest: getSnapManifest(),
});

const location = detectSnapLocation(snapId, {
allowLocal: true,
});

const snapFiles = await fetchSnap(snapId, location);

const { getIsLocked } = getPermittedHooks(
snapId,
snapFiles,
controllerMessenger,
runSaga,
);

expect(getIsLocked()).toBe(false);

await close();
});

it('returns the `getSnapFile` hook', async () => {
const value = JSON.stringify({ bar: 'baz' });
const { snapId, close } = await getMockServer({
Expand Down
8 changes: 8 additions & 0 deletions packages/snaps-simulation/src/simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ export type PermittedMiddlewareHooks = {
*/
getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;

/**
* A hook that returns whether the client is locked or not.
*
* @returns A boolean flag signaling whether the client is locked.
*/
getIsLocked: () => boolean;

/**
* A hook that returns the Snap's auxiliary file for the given path. This hook
* is bound to the Snap ID.
Expand Down Expand Up @@ -390,6 +397,7 @@ export function getPermittedHooks(
return {
hasPermission: () => true,
getUnlockPromise: asyncResolve(),
getIsLocked: () => false,

getSnapFile: async (path: string, encoding: AuxiliaryFileEncoding) =>
await getSnapFile(snapFiles.auxiliaryFiles, path, encoding),
Expand Down

0 comments on commit c7c2c50

Please sign in to comment.