Skip to content

Commit

Permalink
Add tests for new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz committed Dec 10, 2024
1 parent f6e08f2 commit b620e54
Show file tree
Hide file tree
Showing 7 changed files with 863 additions and 9 deletions.
8 changes: 4 additions & 4 deletions packages/snaps-rpc-methods/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ module.exports = deepmerge(baseConfig, {
],
coverageThreshold: {
global: {
branches: 92.94,
functions: 97.26,
lines: 97.87,
statements: 97.39,
branches: 93.3,
functions: 97.42,
lines: 98.01,
statements: 97.58,
},
},
});
207 changes: 207 additions & 0 deletions packages/snaps-rpc-methods/src/permitted/clearState.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import { errorCodes } from '@metamask/rpc-errors';
import type { ClearStateResult } from '@metamask/snaps-sdk';
import { MOCK_SNAP_ID } from '@metamask/snaps-utils/test-utils';
import type { JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils';

import type { ClearStateParameters } from './clearState';
import { clearStateHandler } from './clearState';

describe('snap_clearState', () => {
describe('clearStateHandler', () => {
it('has the expected shape', () => {
expect(clearStateHandler).toMatchObject({
methodNames: ['snap_clearState'],
implementation: expect.any(Function),
hookNames: {
clearSnapState: true,
hasPermission: true,
},
});
});
});

describe('implementation', () => {
const createOriginMiddleware =
(origin: string) =>
(request: any, _response: unknown, next: () => void, _end: unknown) => {
request.origin = origin;
next();
};

it('returns the result from the `clearSnapState` hook', async () => {
const { implementation } = clearStateHandler;

const clearSnapState = jest.fn().mockReturnValue(null);
const hasPermission = jest.fn().mockReturnValue(true);

const hooks = {
clearSnapState,
hasPermission,
};

const engine = new JsonRpcEngine();

engine.push(createOriginMiddleware(MOCK_SNAP_ID));
engine.push((request, response, next, end) => {
const result = implementation(
request as JsonRpcRequest<ClearStateParameters>,
response as PendingJsonRpcResponse<ClearStateResult>,
next,
end,
hooks,
);

result?.catch(end);
});

const response = await engine.handle({
jsonrpc: '2.0',
id: 1,
method: 'snap_clearState',
params: {},
});

expect(clearSnapState).toHaveBeenCalledWith(MOCK_SNAP_ID, true);
expect(response).toStrictEqual({
jsonrpc: '2.0',
id: 1,
result: null,
});
});

it('clears unencrypted state if specified', async () => {
const { implementation } = clearStateHandler;

const clearSnapState = jest.fn().mockReturnValue(null);
const hasPermission = jest.fn().mockReturnValue(true);

const hooks = {
clearSnapState,
hasPermission,
};

const engine = new JsonRpcEngine();

engine.push(createOriginMiddleware(MOCK_SNAP_ID));
engine.push((request, response, next, end) => {
const result = implementation(
request as JsonRpcRequest<ClearStateParameters>,
response as PendingJsonRpcResponse<ClearStateResult>,
next,
end,
hooks,
);

result?.catch(end);
});

const response = await engine.handle({
jsonrpc: '2.0',
id: 1,
method: 'snap_clearState',
params: {
encrypted: false,
},
});

expect(clearSnapState).toHaveBeenCalledWith(MOCK_SNAP_ID, false);
expect(response).toStrictEqual({
jsonrpc: '2.0',
id: 1,
result: null,
});
});

it('throws if the requesting origin does not have the required permission', async () => {
const { implementation } = clearStateHandler;

const clearSnapState = jest.fn();
const hasPermission = jest.fn().mockReturnValue(false);

const hooks = {
clearSnapState,
hasPermission,
};

const engine = new JsonRpcEngine();

engine.push((request, response, next, end) => {
const result = implementation(
request as JsonRpcRequest<ClearStateParameters>,
response as PendingJsonRpcResponse<ClearStateResult>,
next,
end,
hooks,
);

result?.catch(end);
});

const response = await engine.handle({
jsonrpc: '2.0',
id: 1,
method: 'snap_clearState',
params: {},
});

expect(clearSnapState).not.toHaveBeenCalled();
expect(response).toStrictEqual({
jsonrpc: '2.0',
id: 1,
error: {
code: errorCodes.provider.unauthorized,
message:
'The requested account and/or method has not been authorized by the user.',
stack: expect.any(String),
},
});
});

it('throws if the parameters are invalid', async () => {
const { implementation } = clearStateHandler;

const clearSnapState = jest.fn();
const hasPermission = jest.fn().mockReturnValue(true);

const hooks = {
clearSnapState,
hasPermission,
};

const engine = new JsonRpcEngine();

engine.push((request, response, next, end) => {
const result = implementation(
request as JsonRpcRequest<ClearStateParameters>,
response as PendingJsonRpcResponse<ClearStateResult>,
next,
end,
hooks,
);

result?.catch(end);
});

const response = await engine.handle({
jsonrpc: '2.0',
id: 1,
method: 'snap_clearState',
params: {
encrypted: 'foo',
},
});

expect(response).toStrictEqual({
jsonrpc: '2.0',
id: 1,
error: {
code: errorCodes.rpc.invalidParams,
message:
'Invalid params: At path: encrypted -- Expected a value of type `boolean`, but received: `"foo"`.',
stack: expect.any(String),
},
});
});
});
});
3 changes: 2 additions & 1 deletion packages/snaps-rpc-methods/src/permitted/clearState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ function getValidatedParams(params?: unknown) {
});
}

throw error;
/* istanbul ignore next */
throw rpcErrors.internal();
}
}
Loading

0 comments on commit b620e54

Please sign in to comment.