Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test coverage #726

Merged
merged 15 commits into from
Apr 6, 2023
132 changes: 132 additions & 0 deletions packages/coin98-wallet/src/lib/coin98-wallet.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/* eslint-disable @nrwl/nx/enforce-module-boundaries */
import { mock } from "jest-mock-extended";
import { mockWallet } from "../../../core/src/lib/testUtils";

import type { MockWalletDependencies } from "../../../core/src/lib/testUtils";
import type { InjectedWallet } from "../../../core/src/lib/wallet";
import { setupCoin98Wallet } from "./coin98-wallet";
import type { Signer } from "near-api-js/lib/signer";

const accountId = "amirsaran.testnet";
const publicKey = "GF7tLvSzcxX4EtrMFtGvGTb2yUj2DhL8hWzc97BwUkyC";

const mockCoin98WalletOnWindow = () => {
window.coin98 = {
near: {
account: "",
signer: mock<Signer>({
createKey: jest.fn(),
signMessage: jest.fn().mockReturnValue({
signature: Buffer.from([
86, 38, 222, 143, 115, 251, 107, 14, 115, 59, 92, 98, 66, 174, 173,
124, 209, 189, 191, 180, 89, 25, 125, 254, 97, 240, 178, 98, 65, 70,
238, 108, 105, 122, 165, 249, 193, 70, 118, 194, 126, 218, 117, 100,
250, 124, 202, 161, 173, 12, 232, 146, 105, 194, 138, 35, 207, 53,
84, 218, 45, 220, 10, 4,
]),
publicKey,
}),
getPublicKey: jest.fn().mockReturnValue(publicKey),
}),
connect: jest.fn(async () => {
window.coin98.near.account = accountId;
return "";
}),
disconnect: jest.fn(),
},
};

return window.coin98;
};

const createCoin98Wallet = async (deps: MockWalletDependencies = {}) => {
const injectedCoin98Wallet = mockCoin98WalletOnWindow();
const { wallet } = await mockWallet<InjectedWallet>(
setupCoin98Wallet(),
deps
);

return {
wallet,
injectedCoin98Wallet,
};
};

afterEach(() => {
jest.resetModules();
});

describe("signIn", () => {
it("sign into coin98 wallet", async () => {
const { wallet, injectedCoin98Wallet } = await createCoin98Wallet();

await wallet.signIn({ contractId: "test.testnet" });

expect(injectedCoin98Wallet.near.connect).toHaveBeenCalled();
});
});

describe("signOut", () => {
it("sign out of coin98 wallet", async () => {
const { wallet, injectedCoin98Wallet } = await createCoin98Wallet();

await wallet.signIn({ contractId: "test.testnet" });
await wallet.signOut();

expect(injectedCoin98Wallet.near.disconnect).toHaveBeenCalled();
});
});

describe("getAccounts", () => {
it("returns array of accounts", async () => {
const { wallet, injectedCoin98Wallet } = await createCoin98Wallet();

await wallet.signIn({ contractId: "test.testnet" });
const result = await wallet.getAccounts();

expect(injectedCoin98Wallet.near.signer.getPublicKey).toHaveBeenCalled();
expect(result).toEqual([{ accountId, publicKey }]);
});
});

describe("signAndSendTransaction", () => {
it("sign transaction in coin98", async () => {
const { wallet, injectedCoin98Wallet } = await createCoin98Wallet();

await wallet.signIn({ contractId: "test.testnet" });
await wallet.signAndSendTransaction({
signerId: accountId,
receiverId: "test.testnet",
actions: [],
});

expect(injectedCoin98Wallet.near.signer.signMessage).toHaveBeenCalled();
});
});

describe("signAndSendTransactions", () => {
it("sign transactions in coin98", async () => {
const { wallet, injectedCoin98Wallet } = await createCoin98Wallet();

const transactions = [
{
signerId: accountId,
receiverId: "test.testnet",
actions: [],
},
{
signerId: accountId,
receiverId: "test.testnet",
actions: [],
},
];

await wallet.signIn({ contractId: "test.testnet" });
const result = await wallet.signAndSendTransactions({
transactions,
});

expect(injectedCoin98Wallet.near.signer.signMessage).toHaveBeenCalled();
expect(result.length).toEqual(transactions.length);
});
});
14 changes: 7 additions & 7 deletions packages/coin98-wallet/src/lib/coin98-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ const Coin98Wallet: WalletBehaviourFactory<InjectedWallet> = async ({
return [];
}

const publicKey = await _state.wallet.near.signer.getPublicKey(
accountId,
options.network.networkId
);

return [
{
accountId: _state.wallet.near.account,
publicKey: (
await _state.wallet.near.signer.getPublicKey(
accountId,
options.network.networkId
)
).toString(),
accountId,
publicKey: publicKey ? publicKey.toString() : undefined,
},
];
};
Expand Down
5 changes: 3 additions & 2 deletions packages/coin98-wallet/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
"noFallthroughCasesInSwitch": true,
"resolveJsonModule": true
}
}
}
146 changes: 146 additions & 0 deletions packages/finer-wallet/src/lib/finer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/* eslint-disable @nrwl/nx/enforce-module-boundaries */
import { mock } from "jest-mock-extended";
import { mockWallet } from "../../../core/src/lib/testUtils";

import type { MockWalletDependencies } from "../../../core/src/lib/testUtils";
import type { InjectedWallet } from "../../../core/src/lib/wallet";
import type { AccessKey, SignOutResponse } from "./injected-wallet";
import type { FinalExecutionOutcome } from "near-api-js/lib/providers";
import { setupFinerWallet } from "./finer";

const accountId = "test-account.testnet";
const transactions = [
{
signerId: accountId,
receiverId: "test.testnet",
actions: [],
},
{
signerId: accountId,
receiverId: "test.testnet",
actions: [],
},
];

const mockFinerOnWindow = () => {
window.finer = {
near: {
isSender: false,
isFiner: true,
getAccountId: jest.fn().mockReturnValue(""),
getRpc: jest.fn(),
account: jest.fn().mockReturnValue({
connection: {
signer: {
getPublicKey: jest.fn().mockReturnValue(""),
},
},
}),
requestSignIn: jest.fn(async () => {
window.finer.near!.getAccountId = jest.fn().mockReturnValue(accountId);
return {
accessKey: mock<AccessKey>(),
error: "",
};
}),
signOut: jest.fn().mockReturnValue(mock<SignOutResponse>()),
isSignedIn: jest.fn().mockReturnValue(true),
remove: jest.fn(),
on: jest.fn(),
sendMoney: jest.fn(),
signAndSendTransaction: jest.fn().mockReturnValue(
Promise.resolve({
error: undefined,
response: mock<FinalExecutionOutcome>(),
})
),
requestSignTransactions: jest.fn().mockReturnValue(
Promise.resolve({
error: undefined,
response: mock<Array<FinalExecutionOutcome>>(
new Array(transactions.length).fill({})
),
})
),
signMessage: jest.fn(),
},
};

return window.finer;
};

const createFinerWallet = async (deps: MockWalletDependencies = {}) => {
const injectedFiner = mockFinerOnWindow();
const { wallet } = await mockWallet<InjectedWallet>(setupFinerWallet(), deps);

return {
wallet,
injectedFiner,
};
};

afterEach(() => {
jest.resetModules();
});

describe("signIn", () => {
it("sign into finer wallet", async () => {
const { wallet, injectedFiner } = await createFinerWallet();

const accounts = await wallet.signIn({ contractId: "test.testnet" });

expect(injectedFiner.near?.requestSignIn).toHaveBeenCalled();
expect(accounts).toEqual([{ accountId, publicKey: undefined }]);
});
});

describe("signOut", () => {
it("sign out of finer wallet", async () => {
const { wallet, injectedFiner } = await createFinerWallet();

await wallet.signIn({ contractId: "test.testnet" });
await wallet.signOut();

expect(injectedFiner.near?.signOut).toHaveBeenCalled();
});
});

describe("getAccounts", () => {
it("returns array of accounts", async () => {
const { wallet, injectedFiner } = await createFinerWallet();

await wallet.signIn({ contractId: "test.testnet" });
const result = await wallet.getAccounts();

expect(injectedFiner.near?.getAccountId).toHaveBeenCalled();
expect(result).toEqual([{ accountId, publicKey: undefined }]);
});
});

describe("signAndSendTransaction", () => {
it("sign transaction in finer", async () => {
const { wallet, injectedFiner } = await createFinerWallet();

await wallet.signIn({ contractId: "test.testnet" });
await wallet.signAndSendTransaction({
signerId: accountId,
receiverId: "test.testnet",
actions: [],
});

expect(injectedFiner.near?.signAndSendTransaction).toHaveBeenCalled();
});
});
describe("signAndSendTransactions", () => {
it("sign transactions in finer", async () => {
const { wallet, injectedFiner } = await createFinerWallet();

await wallet.signIn({ contractId: "test.testnet" });
const result = await wallet.signAndSendTransactions({
transactions,
});

expect(injectedFiner.near?.requestSignTransactions).toHaveBeenCalled();
expect(result.length).toEqual(transactions.length);
});
});
4 changes: 2 additions & 2 deletions packages/finer-wallet/src/lib/injected-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import type { Account, providers } from "near-api-js";

interface AccessKey {
export interface AccessKey {
publicKey: string;
secretKey: string;
}
Expand All @@ -12,7 +12,7 @@ interface RequestSignInResponse {
error: string | { type: string };
}

type SignOutResponse = true | { error: string | { type: string } };
export type SignOutResponse = true | { error: string | { type: string } };

interface RpcInfo {
explorerUrl: string;
Expand Down
5 changes: 3 additions & 2 deletions packages/finer-wallet/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
"noFallthroughCasesInSwitch": true,
"resolveJsonModule": true
},
"files": [],
"include": [],
Expand All @@ -16,4 +17,4 @@
"path": "./tsconfig.spec.json"
}
]
}
}
16 changes: 16 additions & 0 deletions packages/ledger/src/lib/ledger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,19 @@ describe("signAndSendTransactions", () => {
expect(result.length).toEqual(transactions.length);
});
});

describe("getPublicKey", () => {
it("returns public key", async () => {
const accountId = "amirsaran.testnet";
const derivationPath = "44'/397'/0'/0'/1'";
const { wallet, publicKey } = await createLedgerWallet();
await wallet.signIn({
accounts: [{ derivationPath, publicKey, accountId }],
contractId: "guest-book.testnet",
});
const publicKeyResponse = await wallet.getPublicKey(derivationPath);
expect(publicKeyResponse).toBe(
"GF7tLvSzcxX4EtrMFtGvGTb2yUj2DhL8hWzc97BwUkyC"
);
});
});
4 changes: 2 additions & 2 deletions packages/math-wallet/src/lib/injected-math-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface LoginParams {
publicKey?: string;
}

interface MathAccount {
export interface MathAccount {
name: string;
accountId: string;
publicKey: string;
Expand All @@ -26,7 +26,7 @@ interface MathNetwork {
extra: string;
}

type MathSigner = Signer & {
export type MathSigner = Signer & {
account: MathAccount | null;
network: MathNetwork;
};
Expand Down
Loading