sendStore.spec.js 3.82 KiB
Newer Older
Amaury Martiny's avatar
Amaury Martiny committed
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: BSD-3-Clause

/* eslint-env jest */

Amaury Martiny's avatar
Amaury Martiny committed
import lightJs from '@parity/light.js'; // Mocked

import parityStore from './parityStore';
import { SendStore } from './sendStore';
Amaury Martiny's avatar
Amaury Martiny committed
import * as storeTests from '../utils/testHelpers/storeTests';

Amaury Martiny's avatar
Amaury Martiny committed
jest.mock('@parity/light.js', () => ({
  blockNumber$: jest.fn(() => ({
    subscribe: () =>
      jest.fn(() => ({
        unsubscribe: jest.fn()
      }))
  })),
  makeContract$: jest.fn(() => ({
    transfer$: jest.fn(() => ({ subscribe: jest.fn() }))
  })),
Amaury Martiny's avatar
Amaury Martiny committed
  post$: jest.fn(() => ({
    subscribe: jest.fn(callback => {
      setTimeout(callback({ estimating: true }), 100); // eslint-disable-line standard/no-callback-literal
      setTimeout(callback({ requested: 1 }), 200); // eslint-disable-line standard/no-callback-literal
    })
  }))
Amaury Martiny's avatar
Amaury Martiny committed
}));

jest.mock('./parityStore', () => ({
  api: {
    signer: {
      confirmRequest: jest.fn(() => Promise.resolve(true))
    }
  }
}));

jest.mock('./tokensStore', () => ({
  tokens: {
    ETH: { decimals: 18 },
    foo: { decimals: 18 }
  }
}));

const mockTx = {
  amount: 0.01, // In Ether or in token
  gasPrice: 4, // in Gwei
  to: '0x123'
};

Amaury Martiny's avatar
Amaury Martiny committed
const mockErc20Token = {
  address: 'foo',
  decimals: 18
};

const mockEthToken = {
  address: 'ETH'
};

Amaury Martiny's avatar
Amaury Martiny committed
let sendStore; // Will hold the newly created instance of SendStore in each test
beforeEach(() => {
  sendStore = new SendStore();
});

describe('method acceptRequest', () => {
  test('should call api.signer.confirmRequest', () => {
    sendStore.acceptRequest(1, 'foo');
    expect(parityStore.api.signer.confirmRequest).toHaveBeenCalledWith(
      1,
      null,
      'foo'
    );
  });

  test('should set a subscription on blockNumber$', () => {
    sendStore.acceptRequest(1, 'foo');
    expect(lightJs.blockNumber$).toHaveBeenCalled();
  });
});

describe('method clear', () => {
  test('should clear tx', () => {
    sendStore.setTx(mockTx);
    sendStore.clear();
    expect(sendStore.tx).toEqual({});
  });
Amaury Martiny's avatar
Amaury Martiny committed

  test('should unsubscribe', () => {
    sendStore.subscription = { unsubscribe: jest.fn() };
    sendStore.clear();
    expect(sendStore.subscription.unsubscribe).toHaveBeenCalled();
  });
Amaury Martiny's avatar
Amaury Martiny committed
});
Amaury Martiny's avatar
Amaury Martiny committed

describe('@computed confirmations', () => {
Amaury Martiny's avatar
Amaury Martiny committed
  test('should return correct value if txStatus is not set', () => {
Amaury Martiny's avatar
Amaury Martiny committed
    sendStore.setTxStatus(null);
    expect(sendStore.confirmations).toBe(-1);
  });

Amaury Martiny's avatar
Amaury Martiny committed
  test('should return correct value if txStatus is not `confirmed`', () => {
Amaury Martiny's avatar
Amaury Martiny committed
    sendStore.setTxStatus({ estimating: true });
    expect(sendStore.confirmations).toBe(-1);
  });

Amaury Martiny's avatar
Amaury Martiny committed
  test('should return correct value if txStatus is `confirmed`', () => {
Amaury Martiny's avatar
Amaury Martiny committed
    sendStore.setBlockNumber(5);
    sendStore.setTxStatus({ confirmed: { blockNumber: 4 } });
    expect(sendStore.confirmations).toBe(1);
  });
});
Amaury Martiny's avatar
Amaury Martiny committed

describe('method send', () => {
  beforeEach(() => {
    sendStore.setTx(mockTx);
  });

Amaury Martiny's avatar
Amaury Martiny committed
  test.skip('should call transfer$ if the token is Erc20 and subscribe to it', () => {
Amaury Martiny's avatar
Amaury Martiny committed
    sendStore.send(mockErc20Token);
    expect(sendStore.contract.transfer$).toHaveBeenCalled();
Amaury Martiny's avatar
Amaury Martiny committed
  });

  test('should call post$ if the token is ETH  and subscribe to it', () => {
Amaury Martiny's avatar
Amaury Martiny committed
    sendStore.send(mockEthToken);
    expect(lightJs.post$).toHaveBeenCalled();
Amaury Martiny's avatar
Amaury Martiny committed
  });
Amaury Martiny's avatar
Amaury Martiny committed

  test('should update txStatus', () => {
    sendStore.setTxStatus = jest.fn();
Amaury Martiny's avatar
Amaury Martiny committed
    sendStore.send(mockEthToken);
Amaury Martiny's avatar
Amaury Martiny committed
    expect(sendStore.setTxStatus).toHaveBeenCalledWith({ estimating: true });
  });

  test('should call acceptRequest when txStatus is requested', () => {
    sendStore.acceptRequest = jest.fn(() => Promise.resolve(true));
Amaury Martiny's avatar
Amaury Martiny committed
    sendStore.send(mockEthToken, 'foo');
Amaury Martiny's avatar
Amaury Martiny committed
    expect(sendStore.acceptRequest).toHaveBeenCalledWith(1, 'foo');
  });
});

Amaury Martiny's avatar
Amaury Martiny committed
storeTests.setterTest(SendStore, 'blockNumber');
storeTests.setterTest(SendStore, 'tx');
storeTests.setterTest(SendStore, 'txStatus');