From 9a252c1eed99c3a840d60bb4d0617398686ca8cb Mon Sep 17 00:00:00 2001 From: Ruben Marcus Date: Wed, 9 Oct 2024 12:12:13 +0100 Subject: [PATCH] fix tests --- package-lock.json | 9 +- packages/auth/package.json | 5 +- packages/auth/src/account.ts | 5 +- packages/auth/src/index.ts | 13 -- packages/auth/src/wallet.test.ts | 208 ------------------------------- 5 files changed, 11 insertions(+), 229 deletions(-) delete mode 100644 packages/auth/src/index.ts delete mode 100644 packages/auth/src/wallet.test.ts diff --git a/package-lock.json b/package-lock.json index 0affa302a..38735dabb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27508,7 +27508,7 @@ "license": "MIT", "dependencies": { "@mintbase-js/sdk": "0.6.2", - "@mintbase-js/wallet": "0.6.2", + "@mintbase-js/wallet": "0.6.5", "@near-wallet-selector/core": "8.9.12", "@near-wallet-selector/here-wallet": "8.9.12", "@near-wallet-selector/ledger": "8.9.12", @@ -27518,6 +27518,7 @@ "@near-wallet-selector/wallet-connect": "8.9.12", "bs58": "^5.0.0", "js-sha256": "^0.9.0", + "near-api-js": "^4.0.3", "rxjs": "^7.5.7" }, "devDependencies": { @@ -27537,9 +27538,9 @@ } }, "packages/auth/node_modules/@mintbase-js/wallet": { - "version": "0.6.2", - "resolved": "https://registry.npmjs.org/@mintbase-js/wallet/-/wallet-0.6.2.tgz", - "integrity": "sha512-VX3+goKQ+PoC89YcM+Iap8KvdFvQgQ/InAE1e8+f0l7EsZvEbVM0K0+iv7HWorqHyGf4Ohw1Ke31OBgpIe81dA==", + "version": "0.6.5", + "resolved": "https://registry.npmjs.org/@mintbase-js/wallet/-/wallet-0.6.5.tgz", + "integrity": "sha512-4HGT2t3yCj74dB3JRBIDOdNCGAkhiE65YMTGAxOobzlZpHlFv3mPjnKYeFgkMsnDhhsxFmfWNXrbwelywxvtCA==", "dependencies": { "@near-wallet-selector/core": "8.9.12", "@near-wallet-selector/wallet-utils": "^8.9.12", diff --git a/packages/auth/package.json b/packages/auth/package.json index 2ea8de68b..04f6ad2c2 100644 --- a/packages/auth/package.json +++ b/packages/auth/package.json @@ -20,7 +20,7 @@ "license": "MIT", "dependencies": { "@mintbase-js/sdk": "0.6.2", - "@mintbase-js/wallet": "0.6.2", + "@mintbase-js/wallet": "0.6.5", "@near-wallet-selector/core": "8.9.12", "@near-wallet-selector/here-wallet": "8.9.12", "@near-wallet-selector/ledger": "8.9.12", @@ -30,7 +30,8 @@ "@near-wallet-selector/wallet-connect": "8.9.12", "bs58": "^5.0.0", "js-sha256": "^0.9.0", - "rxjs": "^7.5.7" + "rxjs": "^7.5.7", + "near-api-js": "^4.0.3" }, "devDependencies": { "@types/node": "18.11.9", diff --git a/packages/auth/src/account.ts b/packages/auth/src/account.ts index cce678cb8..f8d955ecb 100644 --- a/packages/auth/src/account.ts +++ b/packages/auth/src/account.ts @@ -1,6 +1,7 @@ -import { connectToNear, Account, KeyStore } from '.'; import { mbjs } from '@mintbase-js/sdk'; +import { Account, connect as connectToNear } from 'near-api-js'; +import { KeyStore } from 'near-api-js/lib/key_stores'; /** * Connect to a NEAR account `accountId` with credentials in `keyStore` {@link KeyStore} @@ -23,7 +24,7 @@ export const connect = async ( keyStore: KeyStore, network: Network = mbjs.keys.network as Network, ): Promise => { - + const near = await connectToNear({ keyStore, networkId: network || mbjs.keys.network as Network, diff --git a/packages/auth/src/index.ts b/packages/auth/src/index.ts deleted file mode 100644 index 3f672f5ff..000000000 --- a/packages/auth/src/index.ts +++ /dev/null @@ -1,13 +0,0 @@ -export * from './account'; -export * from './wallet'; -export { BitteWalletAuth } from './bitte-wallet'; -export * from './constants'; - -// this is done to avoid importing near-api-js more than once -// which leads to a strange, but known issue -// https://docs.near.org/tools/near-api-js/faq#class-x-is-missing-in-schema-publickey -export { Account, providers, connect as connectToNear, KeyPair } from 'near-api-js'; -export { InMemoryKeyStore, KeyStore } from 'near-api-js/lib/key_stores'; - -// adding some other types from within libraries as a convenience -export { FinalExecutionOutcome } from '@near-wallet-selector/core'; diff --git a/packages/auth/src/wallet.test.ts b/packages/auth/src/wallet.test.ts deleted file mode 100644 index 4e6d5cdac..000000000 --- a/packages/auth/src/wallet.test.ts +++ /dev/null @@ -1,208 +0,0 @@ -import { - walletSelectorComponents, - SetupNotCalledError, - setupWalletSelectorComponents, - pollForWalletConnection, - registerWalletAccountsSubscriber, - connectWalletSelector, - disconnectFromWalletSelector, - getWallet, - ConnectionTimeoutError, - getVerifiedOwner, - signMessage, -} from './wallet'; -import { setupWalletSelector } from '@near-wallet-selector/core'; -import { setupModal } from '@near-wallet-selector/modal-ui'; -import { Observable } from 'rxjs'; -// import { WALLET_CONNECTION_TIMEOUT, WALLET_CONNECTION_POLL_INTERVAL } from './constants'; - - -jest.mock('@near-wallet-selector/core'); -jest.mock('@near-wallet-selector/modal-ui'); -jest.mock('./constants', () => ({ - WALLET_CONNECTION_TIMEOUT: -1, - WALLET_CONNECTION_POLL_INTERVAL: 100, -})); - -globalThis.mbjs = { - keys :{ - apiKey: 'omni-site', - callbackUrl: 'https://www.mintbase.xyz/wallet-callback', - connectProxyAddress: null, - contractAddress: 'mintbase1.near', - debugMode: false, - ftAddresses: { - usdc: 'a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.factory.bridge.near', - }, - graphqlUrl: 'https://interop-mainnet.hasura.app/v1/graphql', - isSet: true, - marketAddress: 'simple.market.mintbase1.near', - mbContract: 'mintbase1.near', - nearRpcUrl: 'https://rpc.mainnet.near.org', - network: 'mainnet', - }, -}; - -// jest.useFakeTimers(); - -describe('wallet', () => { - const mockModal = { - show: jest.fn(), - }; - const mockWallet = { - signOut: jest.fn(), - verifyOwner: jest.fn(), - id: 'im.a.test.wallet', - }; - const mockGetState = jest.fn(); - const mockSelector = { - // eslint-disable-next-line @typescript-eslint/explicit-function-return-type - wallet: () => Promise.resolve(mockWallet), - store: { - observable: new Observable(), - getState: mockGetState, - }, - }; - beforeAll(() => { - jest.resetAllMocks(); - Object.defineProperty(global, 'localStorage', { - value: { - getItem: jest.fn(), - setItem: jest.fn(), - removeItem: jest.fn(), - }, - }); - }); - - // eslint-disable-next-line @typescript-eslint/explicit-function-return-type - const setupWithMockComponents = async () => { - (setupWalletSelector as jest.Mock).mockResolvedValueOnce(mockSelector); - (setupModal as jest.Mock).mockReturnValueOnce(mockModal); - return await setupWalletSelectorComponents(); - }; - - // eslint-disable-next-line @typescript-eslint/explicit-function-return-type - const setupWithNullComponents = async () => { - (setupWalletSelector as jest.Mock).mockResolvedValueOnce(null); - (setupModal as jest.Mock).mockReturnValueOnce(null); - return await setupWalletSelectorComponents(); - }; - - test('setupWalletSelectorComponents returns and defines components', async () => { - const { modal, selector } = await setupWithMockComponents(); - - expect(modal).toBeDefined(); - expect(selector).toBeDefined(); - expect(walletSelectorComponents).not.toBe(null); - }); - - test('registerWalletAccountsSubscriber throws when components are not setup', async () => { - await setupWithNullComponents(); - expect(() => { - // eslint-disable-next-line @typescript-eslint/no-empty-function - registerWalletAccountsSubscriber(() => {}); - }).toThrow(SetupNotCalledError); - }); - - test('registerWalletAccountsSubscriber returns the subscription', async () => { - await setupWithMockComponents(); - // eslint-disable-next-line @typescript-eslint/no-empty-function - const subscription = registerWalletAccountsSubscriber(() => {}); - expect(subscription.unsubscribe).toBeDefined(); - }); - - test('pollForWalletConnection throws when components are not setup', async () => { - await setupWithNullComponents(); - expect(pollForWalletConnection()) - .rejects - .toThrow(SetupNotCalledError); - }); - - test('pollForWalletConnection rejects after not finding accounts', async () => { - await setupWithMockComponents(); - mockGetState.mockReturnValueOnce({}); - // eslint-disable-next-line @typescript-eslint/no-empty-function - expect(pollForWalletConnection()).rejects.toThrow(ConnectionTimeoutError); - }); - - test('pollForWalletConnection returns accounts', async () => { - await setupWithMockComponents(); - mockGetState.mockReturnValueOnce({ - accounts: ['fake.account.near'], - }); - // eslint-disable-next-line @typescript-eslint/no-empty-function - const accounts = await pollForWalletConnection(); - - expect(accounts.length).toBeGreaterThan(0); - }); - - test('connectWalletSelector calls show modal', async () => { - await setupWithMockComponents(); - connectWalletSelector(); - expect(mockModal.show).toHaveBeenCalled(); - }); - - test('connectWalletSelector throws when components are not setup', async () => { - await setupWithNullComponents(); - expect(() => { - // eslint-disable-next-line @typescript-eslint/no-empty-function - connectWalletSelector(); - }).toThrow(SetupNotCalledError); - }); - - test('getWallet throws when components are not setup', async () => { - await setupWithNullComponents(); - expect(getWallet()) - .rejects - .toThrow(SetupNotCalledError); - }); - - test('getWallet returns wallet', async () => { - await setupWithMockComponents(); - const wallet = await getWallet(); - expect(wallet.id).toBe(mockWallet.id); - }); - - test('disconnectFromWalletSelector calls sign out on wallet', async () => { - await setupWithMockComponents(); - await disconnectFromWalletSelector(); - expect(mockWallet.signOut).toHaveBeenCalled(); - }); - - test('disconnectFromWalletSelector throws when components are not setup', async () => { - await setupWithNullComponents(); - expect(disconnectFromWalletSelector()) - .rejects - .toThrow(SetupNotCalledError); - }); - - test('getVerifiedOwner calls verifiedOwner on wallet', async () => { - await setupWithMockComponents(); - - // eslint-disable-next-line @typescript-eslint/no-empty-function - await getVerifiedOwner({ message: 'testMessage' }); - expect(mockWallet.verifyOwner).toHaveBeenCalled(); - }); - - test('getVerifiedOwner throws when components are not setup', async () => { - await setupWithNullComponents(); - expect(getVerifiedOwner({ message: 'testMessage' })) - .rejects - .toThrow(SetupNotCalledError); - }); - - test('signMessage calls verifiedOwner on wallet', async () => { - await setupWithMockComponents(); - - // eslint-disable-next-line @typescript-eslint/no-empty-function - await signMessage({ message: 'testMessage' }); - expect(mockWallet.verifyOwner).toHaveBeenCalled(); - }); - - test('signMessage throws when components are not setup', async () => { - await setupWithNullComponents(); - expect(signMessage({ message: 'testMessage' })) - .rejects - .toThrow(SetupNotCalledError); - }); -});