From 87940db3ffa7d67abc652589667a7e05695f926b Mon Sep 17 00:00:00 2001 From: SamvelRaja Sakthivel Date: Wed, 2 Aug 2023 17:32:47 +0530 Subject: [PATCH] Added unit tests --- packages/snap/jest.config.js | 12 ++++++++++ packages/snap/package.json | 6 ++++- packages/snap/snap.manifest.json | 2 +- packages/snap/src/index.ts | 7 ++++-- packages/snap/src/mocks/input.mock.ts | 33 ++++++++++++++++++++++++++ packages/snap/src/mocks/output.mock.ts | 13 ++++++++++ packages/snap/src/mocks/snap.mock.ts | 16 +++++++++++++ packages/snap/src/tests/index.test.ts | 17 +++++++++++++ 8 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 packages/snap/jest.config.js create mode 100644 packages/snap/src/mocks/input.mock.ts create mode 100644 packages/snap/src/mocks/output.mock.ts create mode 100644 packages/snap/src/mocks/snap.mock.ts create mode 100644 packages/snap/src/tests/index.test.ts diff --git a/packages/snap/jest.config.js b/packages/snap/jest.config.js new file mode 100644 index 0000000..ad85eb4 --- /dev/null +++ b/packages/snap/jest.config.js @@ -0,0 +1,12 @@ +module.exports = { + "roots": [ + "/src" + ], + "testMatch": [ + "**/tests/**/*.+(ts|tsx|js)", + "**/?(*.)+(spec|test).+(ts|tsx|js)" + ], + "transform": { + "^.+\\.(ts|tsx)$": "ts-jest" + }, +} \ No newline at end of file diff --git a/packages/snap/package.json b/packages/snap/package.json index ec9b667..5272b02 100644 --- a/packages/snap/package.json +++ b/packages/snap/package.json @@ -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", @@ -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", @@ -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": "yarn@3.2.1", diff --git a/packages/snap/snap.manifest.json b/packages/snap/snap.manifest.json index b3e5021..8161428 100644 --- a/packages/snap/snap.manifest.json +++ b/packages/snap/snap.manifest.json @@ -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", diff --git a/packages/snap/src/index.ts b/packages/snap/src/index.ts index cd0fde6..62baed2 100644 --- a/packages/snap/src/index.ts +++ b/packages/snap/src/index.ts @@ -23,7 +23,7 @@ interface RequestParams { * @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, }) => { @@ -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; \ No newline at end of file diff --git a/packages/snap/src/mocks/input.mock.ts b/packages/snap/src/mocks/input.mock.ts new file mode 100644 index 0000000..0e563c9 --- /dev/null +++ b/packages/snap/src/mocks/input.mock.ts @@ -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> + } + }, + failure: { + getKey: { + origin: 'test_mock', + request: { + id: "some_unique_id", + jsonrpc: "2.0", + method: 'getKey', + params: { + chainId: 'cosmos', + }, + } as unknown as JsonRpcRequest> + } + } +} + +export default output; \ No newline at end of file diff --git a/packages/snap/src/mocks/output.mock.ts b/packages/snap/src/mocks/output.mock.ts new file mode 100644 index 0000000..1d52cfd --- /dev/null +++ b/packages/snap/src/mocks/output.mock.ts @@ -0,0 +1,13 @@ +const output = { + success: { + getKey: { + address: 'cosmos127jexdh5jx5tdqxt6z6dggfr5c6ffcwuamstpe', + algo: 'secp256k1', + bech32Address: 'cosmos127jexdh5jx5tdqxt6z6dggfr5c6ffcwuamstpe', + isNanoLedger: false, + name: 'Cosmos' + } + } +} + +export default output; \ No newline at end of file diff --git a/packages/snap/src/mocks/snap.mock.ts b/packages/snap/src/mocks/snap.mock.ts new file mode 100644 index 0000000..473edde --- /dev/null +++ b/packages/snap/src/mocks/snap.mock.ts @@ -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; \ No newline at end of file diff --git a/packages/snap/src/tests/index.test.ts b/packages/snap/src/tests/index.test.ts new file mode 100644 index 0000000..9166c73 --- /dev/null +++ b/packages/snap/src/tests/index.test.ts @@ -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) +}); \ No newline at end of file