-
Notifications
You must be signed in to change notification settings - Fork 542
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING: Refactor RPC method params and add tests (#922)
* Refactor RPC method params and add tests Fix jest conf after rebase Add manageState tests and refactor it Update jest config after rebase Add refactoring for invoke snap method Add some overall refactoring Refactor update state validation process and add more tests * Fix coverage config after rebase * Refactor storage limit param
- Loading branch information
Showing
9 changed files
with
640 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
packages/rpc-methods/src/permitted/invokeSnapSugar.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { | ||
JsonRpcEngineEndCallback, | ||
JsonRpcEngineNextCallback, | ||
JsonRpcRequest, | ||
} from '@metamask/types'; | ||
import { ethErrors } from 'eth-rpc-errors'; | ||
import { getValidatedParams, invokeSnapSugar } from './invokeSnapSugar'; | ||
|
||
describe('wallet_invokeSnap', () => { | ||
describe('invokeSnapSugar', () => { | ||
it('invokes snap with next()', () => { | ||
const req: JsonRpcRequest<unknown> = { | ||
id: 'some-id', | ||
jsonrpc: '2.0', | ||
method: 'wallet_invokeSnap', | ||
params: { | ||
snapId: 'npm:@metamask/example-snap', | ||
request: { method: 'hello' }, | ||
}, | ||
}; | ||
const _res: unknown = {}; | ||
const next: JsonRpcEngineNextCallback = jest | ||
.fn() | ||
.mockResolvedValueOnce(true); | ||
const end: JsonRpcEngineEndCallback = jest.fn(); | ||
|
||
invokeSnapSugar(req, _res, next, end); | ||
|
||
expect(next).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('ends with an error if params are invalid', () => { | ||
const req: JsonRpcRequest<unknown> = { | ||
id: 'some-id', | ||
jsonrpc: '2.0', | ||
method: 'wallet_invokeSnap', | ||
params: { | ||
snapId: undefined, | ||
request: [], | ||
}, | ||
}; | ||
const _res: unknown = {}; | ||
const next: JsonRpcEngineNextCallback = jest | ||
.fn() | ||
.mockResolvedValueOnce(true); | ||
const end: JsonRpcEngineEndCallback = jest.fn(); | ||
|
||
invokeSnapSugar(req, _res, next, end); | ||
|
||
expect(end).toHaveBeenCalledWith( | ||
ethErrors.rpc.invalidParams({ | ||
message: 'Must specify a valid snap ID.', | ||
}), | ||
); | ||
expect(next).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('getValidatedParams', () => { | ||
it('throws an error if the params is not an object', () => { | ||
expect(() => getValidatedParams([])).toThrow( | ||
'Expected params to be a single object.', | ||
); | ||
}); | ||
|
||
it('throws an error if the snap ID is missing from params object', () => { | ||
expect(() => | ||
getValidatedParams({ snapId: undefined, request: {} }), | ||
).toThrow('Must specify a valid snap ID.'); | ||
}); | ||
|
||
it('throws an error if the request is not a plain object', () => { | ||
expect(() => | ||
getValidatedParams({ snapId: 'snap-id', request: [] }), | ||
).toThrow('Expected request to be a single object.'); | ||
}); | ||
|
||
it('returns valid parameters', () => { | ||
expect( | ||
getValidatedParams({ | ||
snapId: 'npm:@metamask/example-snap', | ||
request: { method: 'hello' }, | ||
}), | ||
).toStrictEqual({ | ||
snapId: 'npm:@metamask/example-snap', | ||
request: { method: 'hello' }, | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.