From cb56de5f63e03334397f7012883d70c8d0f8914c Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Thu, 5 Jul 2018 11:39:19 +0200 Subject: [PATCH] Fix tests --- .../fether-react/src/stores/sendStore.spec.js | 146 ++---------------- 1 file changed, 15 insertions(+), 131 deletions(-) diff --git a/packages/fether-react/src/stores/sendStore.spec.js b/packages/fether-react/src/stores/sendStore.spec.js index 7f3c63db..c0d8113c 100644 --- a/packages/fether-react/src/stores/sendStore.spec.js +++ b/packages/fether-react/src/stores/sendStore.spec.js @@ -5,8 +5,6 @@ /* eslint-env jest */ -import abi from '@parity/shared/lib/contracts/abi/eip20'; -import BigNumber from 'bignumber.js'; import lightJs from '@parity/light.js'; // Mocked import parityStore from './parityStore'; @@ -21,11 +19,6 @@ jest.mock('@parity/light.js', () => ({ })) })), makeContract$: jest.fn(() => ({ - contractObject: { - instance: { - transfer: { estimateGas: jest.fn(() => Promise.resolve(123)) } - } - }, transfer$: jest.fn(() => ({ subscribe: jest.fn() })) })), post$: jest.fn(() => ({ @@ -38,9 +31,6 @@ jest.mock('@parity/light.js', () => ({ jest.mock('./parityStore', () => ({ api: { - eth: { - estimateGas: jest.fn(() => Promise.resolve(123)) - }, signer: { confirmRequest: jest.fn(() => Promise.resolve(true)) } @@ -60,6 +50,15 @@ const mockTx = { to: '0x123' }; +const mockErc20Token = { + address: 'foo', + decimals: 18 +}; + +const mockEthToken = { + address: 'ETH' +}; + let sendStore; // Will hold the newly created instance of SendStore in each test beforeEach(() => { sendStore = new SendStore(); @@ -113,149 +112,34 @@ describe('@computed confirmations', () => { }); }); -describe('@computed contract', () => { - test('should create a contract with correct token address if the current token Erc20', () => { - sendStore.setTokenAddress('foo'); - sendStore.contract; // eslint-disable-line - expect(lightJs.makeContract$).toHaveBeenCalledWith('foo', abi); - }); - - test('should return null if the current token is ETH', () => { - sendStore.setTokenAddress('ETH'); - expect(sendStore.contract).toBe(null); - }); -}); - -describe('method estimateGas', () => { - test('should reject and not estimate if no tx is set', () => { - sendStore.estimateGasForErc20 = jest.fn(); - sendStore.estimateGasForEth = jest.fn(); - expect(sendStore.estimateGas()).rejects.toHaveProperty( - 'message', - 'Tx not set in sendStore.' - ); - expect(sendStore.estimateGasForErc20).not.toHaveBeenCalled(); - expect(sendStore.estimateGasForEth).not.toHaveBeenCalled(); - }); - - test('should call estimateGasForErc20 if the current token is Erc20', () => { - sendStore.estimateGasForErc20 = jest.fn(() => 'estimateGasForErc20'); - sendStore.setTokenAddress('foo'); - sendStore.setTx(mockTx); - expect(sendStore.estimateGas()).toBe('estimateGasForErc20'); - expect(sendStore.estimateGasForErc20).toHaveBeenCalled(); - }); - - test('should call estimateGasForEth if the current token is ETH', () => { - sendStore.estimateGasForEth = jest.fn(() => 'estimateGasForEth'); - sendStore.setTokenAddress('ETH'); - sendStore.setTx(mockTx); - expect(sendStore.estimateGas()).toBe('estimateGasForEth'); - expect(sendStore.estimateGasForEth).toHaveBeenCalled(); - }); -}); - -describe('method estimateGasForErc20', () => { - beforeEach(() => { - sendStore.setTokenAddress('foo'); - }); - - test.skip('should call the transfer method on the contract', () => { - sendStore.estimateGasForErc20(mockTx); - expect( - sendStore.contract.contractObject.instance.transfer.estimateGas - ).toHaveBeenCalledWith(mockTx); - }); - - test('should memoize result', () => { - const a = sendStore.estimateGasForErc20(mockTx); - const b = sendStore.estimateGasForErc20(mockTx); - expect(a).toBe(b); - }); -}); - -describe('method estimateGasForEth', () => { - beforeEach(() => { - sendStore.setTokenAddress('ETH'); - }); - - test('should call api.eth.estimateGas', () => { - sendStore.estimateGasForEth(mockTx); - expect(parityStore.api.eth.estimateGas).toHaveBeenCalledWith(mockTx); - }); - - test('should memoize result', () => { - const a = sendStore.estimateGasForEth(mockTx); - const b = sendStore.estimateGasForEth(mockTx); - expect(a).toBe(b); - }); -}); - describe('method send', () => { beforeEach(() => { sendStore.setTx(mockTx); }); test.skip('should call transfer$ if the token is Erc20 and subscribe to it', () => { - sendStore.setTokenAddress('foo'); - sendStore.send(); - expect(sendStore.contract.transfer$).toHaveBeenCalledWith( - sendStore.txForErc20 - ); + sendStore.send(mockErc20Token); + expect(sendStore.contract.transfer$).toHaveBeenCalled(); }); test('should call post$ if the token is ETH and subscribe to it', () => { - sendStore.setTokenAddress('ETH'); - sendStore.send(); - expect(lightJs.post$).toHaveBeenCalledWith(sendStore.txForEth); + sendStore.send(mockEthToken); + expect(lightJs.post$).toHaveBeenCalled(); }); test('should update txStatus', () => { sendStore.setTxStatus = jest.fn(); - sendStore.setTokenAddress('ETH'); - sendStore.send(); + sendStore.send(mockEthToken); expect(sendStore.setTxStatus).toHaveBeenCalledWith({ estimating: true }); }); test('should call acceptRequest when txStatus is requested', () => { sendStore.acceptRequest = jest.fn(() => Promise.resolve(true)); - sendStore.setTokenAddress('ETH'); - sendStore.send('foo'); + sendStore.send(mockEthToken, 'foo'); expect(sendStore.acceptRequest).toHaveBeenCalledWith(1, 'foo'); }); }); -describe('setter setEstimated', () => { - test('should add a 1.25 factor', () => { - sendStore.setEstimated(new BigNumber(2)); - expect(sendStore.estimated).toEqual(new BigNumber(2 * 1.25)); - }); -}); - -describe('@computed txForErc20', () => { - test('should return correct value', () => { - sendStore.setTokenAddress('foo'); - sendStore.setTx(mockTx); - expect(sendStore.txForErc20).toEqual({ - args: ['0x123', new BigNumber('10000000000000000')], - options: { gasPrice: new BigNumber('4000000000') } - }); - }); -}); - -describe('@computed txForEth', () => { - test('should return correct value', () => { - sendStore.setTokenAddress('foo'); - sendStore.setTx(mockTx); - expect(sendStore.txForEth).toEqual({ - gasPrice: new BigNumber('4000000000'), - to: '0x123', - value: new BigNumber('10000000000000000') - }); - }); -}); - storeTests.setterTest(SendStore, 'blockNumber'); -storeTests.setterTest(SendStore, 'tokenAddress'); storeTests.setterTest(SendStore, 'tx'); storeTests.setterTest(SendStore, 'txStatus'); -- GitLab