Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SamvelRaja Sakthivel authored and SamvelRaja Sakthivel committed Aug 2, 2023
1 parent fa94d0d commit 87940db
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 4 deletions.
12 changes: 12 additions & 0 deletions packages/snap/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
"roots": [
"<rootDir>/src"
],
"testMatch": [
"**/tests/**/*.+(ts|tsx|js)",
"**/?(*.)+(spec|test).+(ts|tsx|js)"
],
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
},
}
6 changes: 5 additions & 1 deletion packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' --ignore-path .gitignore",
"serve": "mm-snap serve",
"start": "mm-snap watch"
"start": "mm-snap watch",
"test": "jest --coverage"
},
"dependencies": {
"@cosmjs/stargate": "^0.30.1",
Expand All @@ -47,6 +48,7 @@
"@metamask/eslint-config-nodejs": "^10.0.0",
"@metamask/eslint-config-typescript": "^10.0.0",
"@metamask/snaps-cli": "^0.32.2",
"@types/jest": "^29.5.3",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
"eslint": "^8.21.0",
Expand All @@ -56,12 +58,14 @@
"eslint-plugin-jsdoc": "^39.2.9",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.6.2",
"patch-package": "^7.0.0",
"postinstall-postinstall": "^2.1.0",
"prettier": "^2.2.1",
"prettier-plugin-packagejson": "^2.2.11",
"rimraf": "^3.0.2",
"through2": "^4.0.2",
"ts-jest": "^29.1.1",
"typescript": "^4.7.4"
},
"packageManager": "[email protected]",
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/leapwallet/cosmos-metamask-snap.git"
},
"source": {
"shasum": "N7FF/rqOb8EudU8zDolOwRBso/ShuzrUT6mE/x+K0Xw=",
"shasum": "C1WeriAbceTZ6AIoijURGy4ke3uhJbYz5L2U2itp620=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
7 changes: 5 additions & 2 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface RequestParams<T> {
* @returns The result of `snap_dialog`.
* @throws If the request method is not valid for this snap.
*/
export const onRpcRequest: OnRpcRequestHandler = async ({
const onRpcRequest: OnRpcRequestHandler = async ({
origin,
request,
}) => {
Expand Down Expand Up @@ -122,8 +122,11 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
pubkey: new Uint8Array(Object.values(accounts[0].pubkey)),
};
}

default:
throw new Error('Method not found.');
}
};


export default onRpcRequest;
33 changes: 33 additions & 0 deletions packages/snap/src/mocks/input.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Json, JsonRpcRequest } from "@metamask/snaps-types";


const output = {
success: {
getKey: {
origin: 'test_mock',
request: {
id: "some_unique_id",
jsonrpc: "2.0",
method: 'getKey',
params: {
chainId: 'cosmoshub-4',
},
} as unknown as JsonRpcRequest<Json[] | Record<string, Json>>
}
},
failure: {
getKey: {
origin: 'test_mock',
request: {
id: "some_unique_id",
jsonrpc: "2.0",
method: 'getKey',
params: {
chainId: 'cosmos',
},
} as unknown as JsonRpcRequest<Json[] | Record<string, Json>>
}
}
}

export default output;
13 changes: 13 additions & 0 deletions packages/snap/src/mocks/output.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const output = {
success: {
getKey: {
address: 'cosmos127jexdh5jx5tdqxt6z6dggfr5c6ffcwuamstpe',
algo: 'secp256k1',
bech32Address: 'cosmos127jexdh5jx5tdqxt6z6dggfr5c6ffcwuamstpe',
isNanoLedger: false,
name: 'Cosmos'
}
}
}

export default output;
16 changes: 16 additions & 0 deletions packages/snap/src/mocks/snap.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@



const snapMock:any = {
success: {
request: (params: any) => {
if (
params.method === "snap_getBip44Entropy"
) {
return {"depth":2,"masterFingerprint":3507465413,"parentFingerprint":3845878817,"index":2147483766,"privateKey":"0x4072b5221f1526a0654c5ea97791476d70f1c09fa9e71e8812c3b9a097c829ad","publicKey":"0x04f9cc5a47a6497bd9b0dd069f8d12b50a3c2198354f0630371ba5bc34b8e67939dc541e975c8ea7c0e77630f92af9f862bd6203458735525d8715de7d7c590608","chainCode":"0xd2beb6b5262f8db0200551b45ebf22054ac7e31846fe64b953bffdfd4f1e1592","coin_type":118,"path":"m / bip32:44' / bip32:118'"};
}
}
}
};

export default snapMock;
17 changes: 17 additions & 0 deletions packages/snap/src/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import onRPCRequest from '../index';
import snapMock from '../mocks/snap.mock';
import input from '../mocks/input.mock';
import output from '../mocks/output.mock';
import { JsonRpcRequest, OnRpcRequestHandler } from '@metamask/snaps-types';

test('GetKey for Invalid Chain ID', async () => {
if(input.failure.getKey.request) {
await expect(onRPCRequest(input.failure.getKey)).rejects.toThrow('Invalid chainId');
}

});

test('GetKey for Valid Chain ID', async () => {
(global as any).snap = snapMock.success;
await expect(onRPCRequest(input.success.getKey)).resolves.toMatchObject(output.success.getKey)
});

0 comments on commit 87940db

Please sign in to comment.