Skip to content

Commit

Permalink
add coinbase wallet connector tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-jake committed Jun 17, 2022
1 parent 459fdd6 commit 1f8c01a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
85 changes: 85 additions & 0 deletions packages/react-celo/__tests__/connectors/coinbase-wallet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { CeloContract } from '@celo/contractkit/lib/base';
import crypto from 'crypto';
import { generateTestingUtils } from 'eth-testing';

import { CoinbaseWalletConnector } from '../../src/connectors';
import { Alfajores, Baklava, localStorageKeys } from '../../src/constants';
import { getTypedStorageKey } from '../../src/utils/local-storage';

const ACCOUNT = '0xf61B443A155b07D2b2cAeA2d99715dC84E839EEf';

const testingUtils = generateTestingUtils({
providerType: 'default',
verbose: false,
});

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
jest.mock('@coinbase/wallet-sdk', () => ({
...jest.requireActual('@coinbase/wallet-sdk'),
CoinbaseWalletSDK: jest.fn(() => ({
makeWeb3Provider: () => testingUtils.getProvider(),
})),
}));

describe('CoinbaseWalletConnector', () => {
beforeAll(() => {
// @ts-expect-error global override
global.crypto = crypto;
Object.defineProperty(global.self, 'crypto', {
value: {
getRandomValues: (arr: number[]) => crypto.randomBytes(arr.length),
},
});

testingUtils.mockNotConnectedWallet();
testingUtils.mockAccounts([ACCOUNT]);
testingUtils.mockRequestAccounts([ACCOUNT]);
testingUtils.lowLevel.mockRequest('wallet_switchEthereumChain', {});
});

afterEach(() => {
// Clear all mocks between tests
testingUtils.clearAllMocks();
});

it('initialises', async () => {
const connector = new CoinbaseWalletConnector(
Alfajores,
CeloContract.GoldToken
);
await connector.initialise();
expect(connector.account).toEqual(ACCOUNT);
expect(connector.initialised).toBe(true);
});

describe('when network change', () => {
let connector: CoinbaseWalletConnector;
beforeEach(() => {
testingUtils.mockConnectedWallet([ACCOUNT]);
connector = new CoinbaseWalletConnector(
Alfajores,
CeloContract.GoldToken
);
});

it('sets network in local storage and in connection', async () => {
await connector.updateKitWithNetwork(Baklava);
expect(getTypedStorageKey(localStorageKeys.lastUsedNetwork)).toEqual(
Baklava.name
);
});

const callback = jest.fn();

it('reacts to network being changed from CoinbaseWallet side', async () => {
connector.onNetworkChange(callback);

// Seems to only work when init is called after the callback is set
await connector.initialise();

testingUtils.mockChainChanged('0x1');

expect(callback).toHaveBeenLastCalledWith(1);
});
});
});
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6133,7 +6133,7 @@ eventemitter3@4.0.4:
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.4.tgz#b5463ace635a083d018bdc7c917b4c5f10a85384"
integrity sha512-rlaVLnVxtxvoyLsQQFBx53YmXHDxRIzzTLbdfxqi4yocpSjAxXwkU0cScM5JgSKMqEhrZpnvQ2D9gjylR0AimQ==

eventemitter3@^4.0.4:
eventemitter3@^4.0.4, eventemitter3@^4.0.7:
version "4.0.7"
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.7.tgz#2de9b68f6528d5644ef5c59526a1b4a07306169f"
integrity sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==
Expand Down

0 comments on commit 1f8c01a

Please sign in to comment.