handleMetaMaskPolling.spec.ts
1 import noop from 'lodash/noop'; 2 3 import handleMetaMaskPolling, { getActualChainId } from './handleMetaMaskPolling'; 4 import * as configNetworksSelectors from './config/networks/selectors'; 5 import { walletSelectors } from './wallet'; 6 7 jest.mock('./config/networks/selectors'); 8 jest.mock('./wallet'); 9 10 describe('getActualChainId', () => { 11 it('should reject with an error if web3 does not exist', async done => { 12 try { 13 await getActualChainId(); 14 } catch (e) { 15 expect(e).toBe('Web3 not found.'); 16 done(); 17 } 18 }); 19 20 it('should reject with an error web3 if fails its network check', async done => { 21 (global as any).web3 = { 22 version: { 23 getNetwork: jest.fn(callback => callback('Network check failed')) 24 } 25 }; 26 27 try { 28 await getActualChainId(); 29 } catch (e) { 30 expect(e).toBe('Network check failed'); 31 done(); 32 } 33 }); 34 35 it('should return a chainId when everything is fine', async done => { 36 (global as any).web3 = { 37 version: { 38 getNetwork: jest.fn(callback => callback(null, '1')) 39 } 40 }; 41 42 const network = await getActualChainId(); 43 44 expect(network).toBe('1'); 45 done(); 46 }); 47 }); 48 49 describe('handleMetaMaskPolling', () => { 50 it('should do nothing when there is no wallet instance', async done => { 51 (global as any).web3 = { 52 version: { 53 getNetwork: jest.fn(callback => callback(null, '1')) 54 } 55 }; 56 (walletSelectors as any).getWalletInst.mockReturnValue(null); 57 (configNetworksSelectors as any).getNetworkByChainId.mockReturnValue('ETH'); 58 59 const store = { 60 getState: noop, 61 dispatch: noop 62 }; 63 const result = await handleMetaMaskPolling(store as any); 64 65 expect(result).toBe(false); 66 done(); 67 }); 68 69 it('should reload the page if the network has changed', async done => { 70 (global as any).web3 = { 71 version: { 72 getNetwork: jest.fn(callback => callback(null, '1')) 73 } 74 }; 75 (walletSelectors as any).getWalletInst.mockReturnValue({ 76 network: 'ETC' 77 }); 78 (configNetworksSelectors as any).getNetworkByChainId.mockReturnValue('ETH'); 79 80 const store = { 81 getState: noop 82 }; 83 const result = await handleMetaMaskPolling(store as any); 84 85 expect(result).toBe(true); 86 done(); 87 }); 88 89 it('should reload the page if `getActualChainId` rejects', async done => { 90 (global as any).web3 = { 91 version: { 92 getNetwork: jest.fn(callback => callback('Network check failed')) 93 } 94 }; 95 (walletSelectors as any).getWalletInst.mockReturnValue({ 96 network: 'ETH' 97 }); 98 (configNetworksSelectors as any).getNetworkByChainId.mockReturnValue('ETH'); 99 100 const store = { 101 getState: noop 102 }; 103 const result = await handleMetaMaskPolling(store as any); 104 105 expect(result).toBe(true); 106 done(); 107 }); 108 });