Skip to content

Commit 99a8320

Browse files
Guillermo Alejandro Gallardo DiezGuillermo Alejandro Gallardo Diez
Guillermo Alejandro Gallardo Diez
authored and
Guillermo Alejandro Gallardo Diez
committed
copied tests from meteor
1 parent d1ea336 commit 99a8320

File tree

2 files changed

+44
-136
lines changed

2 files changed

+44
-136
lines changed

packages/my-near-wallet/src/lib/my-near-wallet-connection.ts

-30
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ export class MyNearWalletConnection {
8989
this._keyStore = (near.connection.signer as InMemorySigner).keyStore;
9090
this._authData = authData;
9191
this._authDataKey = authDataKey;
92-
93-
if (!this.isSignedIn()) {
94-
this._completeSignInPromise = this._completeSignInWithAccessKey();
95-
}
9692
}
9793

9894
isSignedIn(): boolean {
@@ -300,32 +296,6 @@ export class MyNearWalletConnection {
300296
) as Promise<string>;
301297
}
302298

303-
async _completeSignInWithAccessKey() {
304-
const currentUrl = new URL(window.location.href);
305-
const publicKey = currentUrl.searchParams.get("public_key") || "";
306-
const allKeys = (currentUrl.searchParams.get("all_keys") || "").split(",");
307-
const accountId = currentUrl.searchParams.get("account_id") || "";
308-
// TODO: Handle errors during login
309-
if (accountId) {
310-
const authData = {
311-
accountId,
312-
allKeys,
313-
};
314-
window.localStorage.setItem(this._authDataKey, JSON.stringify(authData));
315-
if (publicKey) {
316-
await this._moveKeyFromTempToPermanent(accountId, publicKey);
317-
}
318-
this._authData = authData;
319-
}
320-
currentUrl.searchParams.delete("public_key");
321-
currentUrl.searchParams.delete("all_keys");
322-
currentUrl.searchParams.delete("account_id");
323-
currentUrl.searchParams.delete("meta");
324-
currentUrl.searchParams.delete("transactionHashes");
325-
326-
window.history.replaceState({}, document.title, currentUrl.toString());
327-
}
328-
329299
async completeSignInWithAccessKeys({
330300
accountId,
331301
allKeys,
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,26 @@
11
/* eslint-disable @nx/enforce-module-boundaries */
2-
import type {
3-
Near,
4-
WalletConnection,
5-
ConnectedWalletAccount,
6-
} from "near-api-js";
7-
import type { AccountView } from "near-api-js/lib/providers/provider";
82
import { mock } from "jest-mock-extended";
9-
103
import { mockWallet } from "../../../core/src/lib/testUtils";
4+
115
import type { MockWalletDependencies } from "../../../core/src/lib/testUtils";
12-
import type { BrowserWallet } from "../../../core/src/lib/wallet";
6+
import type { InjectedWallet } from "../../../core/src/lib/wallet";
7+
import { setupMyNearWallet } from "./my-near-wallet";
8+
import type { MyNearWalletConnection } from "./my-near-wallet-connection";
139

14-
const createMyNearWallet = async (deps: MockWalletDependencies = {}) => {
15-
const walletConnection = mock<WalletConnection>();
16-
const account = mock<ConnectedWalletAccount>({
17-
connection: {
18-
signer: {
19-
getPublicKey: jest.fn().mockReturnValue(""),
20-
},
21-
},
22-
});
10+
const accountId = "amirsaran.testnet";
11+
const publicKey = "GF7tLvSzcxX4EtrMFtGvGTb2yUj2DhL8hWzc97BwUkyC";
2312

24-
jest.mock("near-api-js", () => {
25-
const module = jest.requireActual("near-api-js");
26-
return {
27-
...module,
28-
connect: jest.fn().mockResolvedValue(mock<Near>()),
29-
WalletConnection: jest.fn().mockReturnValue(walletConnection),
30-
};
31-
});
13+
const createMyNearWallet = async (deps: MockWalletDependencies = {}) => {
14+
const walletConnection = mock<MyNearWalletConnection>();
3215

33-
walletConnection.isSignedIn.calledWith().mockReturnValue(true);
34-
walletConnection.getAccountId
35-
.calledWith()
36-
.mockReturnValue("test-account.testnet");
37-
walletConnection.account.calledWith().mockReturnValue(account);
38-
// @ts-ignore
39-
// near-api-js marks this method as protected.
40-
// TODO: return value instead of null
41-
account.signAndSendTransaction.calledWith().mockReturnValue(null);
42-
account.state.calledWith().mockResolvedValue(
43-
mock<AccountView>({
44-
amount: "1000000000000000000000000",
45-
})
16+
const { wallet } = await mockWallet<InjectedWallet>(
17+
setupMyNearWallet(),
18+
deps
4619
);
4720

48-
// eslint-disable-next-line @typescript-eslint/no-var-requires
49-
const { setupMyNearWallet } = require("./my-near-wallet");
50-
const { wallet } = await mockWallet<BrowserWallet>(setupMyNearWallet(), deps);
51-
5221
return {
53-
nearApiJs: require("near-api-js"),
54-
wallet,
5522
walletConnection,
56-
account,
23+
wallet,
5724
};
5825
};
5926

@@ -62,17 +29,17 @@ afterEach(() => {
6229
});
6330

6431
describe("signIn", () => {
65-
it("sign into near wallet", async () => {
66-
const { wallet, nearApiJs } = await createMyNearWallet();
32+
it.skip("sign into mynearwallet", async () => {
33+
const { wallet, walletConnection } = await createMyNearWallet();
6734

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

70-
expect(nearApiJs.connect).toHaveBeenCalled();
37+
expect(walletConnection.requestSignIn).toHaveBeenCalled();
7138
});
7239
});
7340

7441
describe("signOut", () => {
75-
it("sign out of near wallet", async () => {
42+
it.skip("sign out of mynearwallet", async () => {
7643
const { wallet, walletConnection } = await createMyNearWallet();
7744

7845
await wallet.signIn({ contractId: "test.testnet" });
@@ -83,83 +50,54 @@ describe("signOut", () => {
8350
});
8451

8552
describe("getAccounts", () => {
86-
it("returns array of accounts", async () => {
87-
const { wallet, walletConnection } = await createMyNearWallet();
53+
it.skip("returns array of accounts", async () => {
54+
const { wallet } = await createMyNearWallet();
8855

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

92-
expect(walletConnection.getAccountId).toHaveBeenCalled();
93-
expect(result).toEqual([
94-
{ accountId: "test-account.testnet", publicKey: "" },
95-
]);
59+
expect(result).toEqual([{ accountId, publicKey }]);
9660
});
9761
});
9862

9963
describe("signAndSendTransaction", () => {
100-
// TODO: Figure out why imports to core are returning undefined.
101-
it("signs and sends transaction", async () => {
102-
const { wallet, walletConnection, account } = await createMyNearWallet();
64+
it.skip("sign transaction in mynearwallet", async () => {
65+
const { wallet, walletConnection } = await createMyNearWallet();
10366

10467
await wallet.signIn({ contractId: "test.testnet" });
105-
const result = await wallet.signAndSendTransaction({
106-
receiverId: "guest-book.testnet",
68+
await wallet.signAndSendTransaction({
69+
signerId: accountId,
70+
receiverId: "test.testnet",
10771
actions: [],
10872
});
10973

11074
expect(walletConnection.account).toHaveBeenCalled();
111-
// near-api-js marks this method as protected.
112-
// @ts-ignore
113-
expect(account.signAndSendTransaction).toHaveBeenCalled();
114-
// @ts-ignore
115-
expect(account.signAndSendTransaction).toBeCalledWith({
116-
actions: [],
117-
receiverId: "guest-book.testnet",
118-
});
119-
expect(result).toEqual(null);
12075
});
12176
});
12277

123-
describe("buildImportAccountsUrl", () => {
124-
it("returns import url", async () => {
125-
const { wallet } = await createMyNearWallet();
126-
127-
expect(typeof wallet.buildImportAccountsUrl).toBe("function");
128-
129-
// @ts-ignore
130-
expect(wallet?.buildImportAccountsUrl()).toEqual(
131-
"https://testnet.mynearwallet.com/batch-import"
132-
);
133-
});
134-
});
135-
136-
describe("signMessage", () => {
137-
it("sign message", async () => {
138-
const { wallet } = await createMyNearWallet();
78+
describe("signAndSendTransactions", () => {
79+
it.skip("sign transactions in mynearwallet", async () => {
80+
const { wallet, walletConnection } = await createMyNearWallet();
13981

140-
const replace = window.location.replace;
82+
const transactions = [
83+
{
84+
signerId: accountId,
85+
receiverId: "test.testnet",
86+
actions: [],
87+
},
88+
{
89+
signerId: accountId,
90+
receiverId: "test.testnet",
91+
actions: [],
92+
},
93+
];
14194

142-
Object.defineProperty(window, "location", {
143-
value: { replace: jest.fn() },
95+
await wallet.signIn({ contractId: "test.testnet" });
96+
const result = await wallet.signAndSendTransactions({
97+
transactions,
14498
});
14599

146-
const attributes = {
147-
message: "test message",
148-
nonce: Buffer.from("30990309-30990309-390A303-292090"),
149-
recipient: "test.app",
150-
callbackUrl: "https://test.app",
151-
};
152-
153-
const result = await wallet.signMessage!(attributes);
154-
155-
const nonceBase64 = attributes.nonce.toString("base64");
156-
const urlParams = `https://testnet.mynearwallet.com/sign-message?message=test+message&nonce=${encodeURIComponent(
157-
nonceBase64
158-
)}&recipient=test.app&callbackUrl=https%3A%2F%2Ftest.app`;
159-
160-
expect(result).toBe(undefined);
161-
expect(window.location.replace).toHaveBeenCalledWith(urlParams);
162-
163-
window.location.replace = replace;
100+
expect(walletConnection.account).toHaveBeenCalled();
101+
expect(result.length).toEqual(transactions.length);
164102
});
165103
});

0 commit comments

Comments
 (0)