diff options
Diffstat (limited to 'ui/app/ducks/tests')
-rw-r--r-- | ui/app/ducks/tests/confirm-transaction.duck.test.js | 685 | ||||
-rw-r--r-- | ui/app/ducks/tests/gas-duck.test.js | 600 | ||||
-rw-r--r-- | ui/app/ducks/tests/send-duck.test.js | 186 |
3 files changed, 0 insertions, 1471 deletions
diff --git a/ui/app/ducks/tests/confirm-transaction.duck.test.js b/ui/app/ducks/tests/confirm-transaction.duck.test.js deleted file mode 100644 index eceacd0bd..000000000 --- a/ui/app/ducks/tests/confirm-transaction.duck.test.js +++ /dev/null @@ -1,685 +0,0 @@ -import assert from 'assert' -import configureMockStore from 'redux-mock-store' -import thunk from 'redux-thunk' -import sinon from 'sinon' - -import ConfirmTransactionReducer, * as actions from '../confirm-transaction.duck.js' - -const initialState = { - txData: {}, - tokenData: {}, - methodData: {}, - tokenProps: { - tokenDecimals: '', - tokenSymbol: '', - }, - fiatTransactionAmount: '', - fiatTransactionFee: '', - fiatTransactionTotal: '', - ethTransactionAmount: '', - ethTransactionFee: '', - ethTransactionTotal: '', - hexTransactionAmount: '', - hexTransactionFee: '', - hexTransactionTotal: '', - nonce: '', - toSmartContract: false, - fetchingData: false, -} - -const UPDATE_TX_DATA = 'metamask/confirm-transaction/UPDATE_TX_DATA' -const CLEAR_TX_DATA = 'metamask/confirm-transaction/CLEAR_TX_DATA' -const UPDATE_TOKEN_DATA = 'metamask/confirm-transaction/UPDATE_TOKEN_DATA' -const CLEAR_TOKEN_DATA = 'metamask/confirm-transaction/CLEAR_TOKEN_DATA' -const UPDATE_METHOD_DATA = 'metamask/confirm-transaction/UPDATE_METHOD_DATA' -const CLEAR_METHOD_DATA = 'metamask/confirm-transaction/CLEAR_METHOD_DATA' -const UPDATE_TRANSACTION_AMOUNTS = 'metamask/confirm-transaction/UPDATE_TRANSACTION_AMOUNTS' -const UPDATE_TRANSACTION_FEES = 'metamask/confirm-transaction/UPDATE_TRANSACTION_FEES' -const UPDATE_TRANSACTION_TOTALS = 'metamask/confirm-transaction/UPDATE_TRANSACTION_TOTALS' -const UPDATE_TOKEN_PROPS = 'metamask/confirm-transaction/UPDATE_TOKEN_PROPS' -const UPDATE_NONCE = 'metamask/confirm-transaction/UPDATE_NONCE' -const UPDATE_TO_SMART_CONTRACT = 'metamask/confirm-transaction/UPDATE_TO_SMART_CONTRACT' -const FETCH_DATA_START = 'metamask/confirm-transaction/FETCH_DATA_START' -const FETCH_DATA_END = 'metamask/confirm-transaction/FETCH_DATA_END' -const CLEAR_CONFIRM_TRANSACTION = 'metamask/confirm-transaction/CLEAR_CONFIRM_TRANSACTION' - -describe('Confirm Transaction Duck', () => { - describe('State changes', () => { - const mockState = { - confirmTransaction: { - txData: { - id: 1, - }, - tokenData: { - name: 'abcToken', - }, - methodData: { - name: 'approve', - }, - tokenProps: { - tokenDecimals: '3', - tokenSymbol: 'ABC', - }, - fiatTransactionAmount: '469.26', - fiatTransactionFee: '0.01', - fiatTransactionTotal: '1.000021', - ethTransactionAmount: '1', - ethTransactionFee: '0.000021', - ethTransactionTotal: '469.27', - hexTransactionAmount: '', - hexTransactionFee: '0x1319718a5000', - hexTransactionTotal: '', - nonce: '0x0', - toSmartContract: false, - fetchingData: false, - }, - } - - it('should initialize state', () => { - assert.deepEqual( - ConfirmTransactionReducer({}), - initialState - ) - }) - - it('should return state unchanged if it does not match a dispatched actions type', () => { - assert.deepEqual( - ConfirmTransactionReducer(mockState, { - type: 'someOtherAction', - value: 'someValue', - }), - { ...mockState.confirmTransaction }, - ) - }) - - it('should set txData when receiving a UPDATE_TX_DATA action', () => { - assert.deepEqual( - ConfirmTransactionReducer(mockState, { - type: UPDATE_TX_DATA, - payload: { - id: 2, - }, - }), - { - ...mockState.confirmTransaction, - txData: { - ...mockState.confirmTransaction.txData, - id: 2, - }, - } - ) - }) - - it('should clear txData when receiving a CLEAR_TX_DATA action', () => { - assert.deepEqual( - ConfirmTransactionReducer(mockState, { - type: CLEAR_TX_DATA, - }), - { - ...mockState.confirmTransaction, - txData: {}, - } - ) - }) - - it('should set tokenData when receiving a UPDATE_TOKEN_DATA action', () => { - assert.deepEqual( - ConfirmTransactionReducer(mockState, { - type: UPDATE_TOKEN_DATA, - payload: { - name: 'defToken', - }, - }), - { - ...mockState.confirmTransaction, - tokenData: { - ...mockState.confirmTransaction.tokenData, - name: 'defToken', - }, - } - ) - }) - - it('should clear tokenData when receiving a CLEAR_TOKEN_DATA action', () => { - assert.deepEqual( - ConfirmTransactionReducer(mockState, { - type: CLEAR_TOKEN_DATA, - }), - { - ...mockState.confirmTransaction, - tokenData: {}, - } - ) - }) - - it('should set methodData when receiving a UPDATE_METHOD_DATA action', () => { - assert.deepEqual( - ConfirmTransactionReducer(mockState, { - type: UPDATE_METHOD_DATA, - payload: { - name: 'transferFrom', - }, - }), - { - ...mockState.confirmTransaction, - methodData: { - ...mockState.confirmTransaction.methodData, - name: 'transferFrom', - }, - } - ) - }) - - it('should clear methodData when receiving a CLEAR_METHOD_DATA action', () => { - assert.deepEqual( - ConfirmTransactionReducer(mockState, { - type: CLEAR_METHOD_DATA, - }), - { - ...mockState.confirmTransaction, - methodData: {}, - } - ) - }) - - it('should update transaction amounts when receiving an UPDATE_TRANSACTION_AMOUNTS action', () => { - assert.deepEqual( - ConfirmTransactionReducer(mockState, { - type: UPDATE_TRANSACTION_AMOUNTS, - payload: { - fiatTransactionAmount: '123.45', - ethTransactionAmount: '.5', - hexTransactionAmount: '0x1', - }, - }), - { - ...mockState.confirmTransaction, - fiatTransactionAmount: '123.45', - ethTransactionAmount: '.5', - hexTransactionAmount: '0x1', - } - ) - }) - - it('should update transaction fees when receiving an UPDATE_TRANSACTION_FEES action', () => { - assert.deepEqual( - ConfirmTransactionReducer(mockState, { - type: UPDATE_TRANSACTION_FEES, - payload: { - fiatTransactionFee: '123.45', - ethTransactionFee: '.5', - hexTransactionFee: '0x1', - }, - }), - { - ...mockState.confirmTransaction, - fiatTransactionFee: '123.45', - ethTransactionFee: '.5', - hexTransactionFee: '0x1', - } - ) - }) - - it('should update transaction totals when receiving an UPDATE_TRANSACTION_TOTALS action', () => { - assert.deepEqual( - ConfirmTransactionReducer(mockState, { - type: UPDATE_TRANSACTION_TOTALS, - payload: { - fiatTransactionTotal: '123.45', - ethTransactionTotal: '.5', - hexTransactionTotal: '0x1', - }, - }), - { - ...mockState.confirmTransaction, - fiatTransactionTotal: '123.45', - ethTransactionTotal: '.5', - hexTransactionTotal: '0x1', - } - ) - }) - - it('should update tokenProps when receiving an UPDATE_TOKEN_PROPS action', () => { - assert.deepEqual( - ConfirmTransactionReducer(mockState, { - type: UPDATE_TOKEN_PROPS, - payload: { - tokenSymbol: 'DEF', - tokenDecimals: '1', - }, - }), - { - ...mockState.confirmTransaction, - tokenProps: { - tokenSymbol: 'DEF', - tokenDecimals: '1', - }, - } - ) - }) - - it('should update nonce when receiving an UPDATE_NONCE action', () => { - assert.deepEqual( - ConfirmTransactionReducer(mockState, { - type: UPDATE_NONCE, - payload: '0x1', - }), - { - ...mockState.confirmTransaction, - nonce: '0x1', - } - ) - }) - - it('should update nonce when receiving an UPDATE_TO_SMART_CONTRACT action', () => { - assert.deepEqual( - ConfirmTransactionReducer(mockState, { - type: UPDATE_TO_SMART_CONTRACT, - payload: true, - }), - { - ...mockState.confirmTransaction, - toSmartContract: true, - } - ) - }) - - it('should set fetchingData to true when receiving a FETCH_DATA_START action', () => { - assert.deepEqual( - ConfirmTransactionReducer(mockState, { - type: FETCH_DATA_START, - }), - { - ...mockState.confirmTransaction, - fetchingData: true, - } - ) - }) - - it('should set fetchingData to false when receiving a FETCH_DATA_END action', () => { - assert.deepEqual( - ConfirmTransactionReducer({ confirmTransaction: { fetchingData: true } }, { - type: FETCH_DATA_END, - }), - { - fetchingData: false, - } - ) - }) - - it('should clear confirmTransaction when receiving a FETCH_DATA_END action', () => { - assert.deepEqual( - ConfirmTransactionReducer(mockState, { - type: CLEAR_CONFIRM_TRANSACTION, - }), - { - ...initialState, - } - ) - }) - }) - - describe('Single actions', () => { - it('should create an action to update txData', () => { - const txData = { test: 123 } - const expectedAction = { - type: UPDATE_TX_DATA, - payload: txData, - } - - assert.deepEqual( - actions.updateTxData(txData), - expectedAction - ) - }) - - it('should create an action to clear txData', () => { - const expectedAction = { - type: CLEAR_TX_DATA, - } - - assert.deepEqual( - actions.clearTxData(), - expectedAction - ) - }) - - it('should create an action to update tokenData', () => { - const tokenData = { test: 123 } - const expectedAction = { - type: UPDATE_TOKEN_DATA, - payload: tokenData, - } - - assert.deepEqual( - actions.updateTokenData(tokenData), - expectedAction - ) - }) - - it('should create an action to clear tokenData', () => { - const expectedAction = { - type: CLEAR_TOKEN_DATA, - } - - assert.deepEqual( - actions.clearTokenData(), - expectedAction - ) - }) - - it('should create an action to update methodData', () => { - const methodData = { test: 123 } - const expectedAction = { - type: UPDATE_METHOD_DATA, - payload: methodData, - } - - assert.deepEqual( - actions.updateMethodData(methodData), - expectedAction - ) - }) - - it('should create an action to clear methodData', () => { - const expectedAction = { - type: CLEAR_METHOD_DATA, - } - - assert.deepEqual( - actions.clearMethodData(), - expectedAction - ) - }) - - it('should create an action to update transaction amounts', () => { - const transactionAmounts = { test: 123 } - const expectedAction = { - type: UPDATE_TRANSACTION_AMOUNTS, - payload: transactionAmounts, - } - - assert.deepEqual( - actions.updateTransactionAmounts(transactionAmounts), - expectedAction - ) - }) - - it('should create an action to update transaction fees', () => { - const transactionFees = { test: 123 } - const expectedAction = { - type: UPDATE_TRANSACTION_FEES, - payload: transactionFees, - } - - assert.deepEqual( - actions.updateTransactionFees(transactionFees), - expectedAction - ) - }) - - it('should create an action to update transaction totals', () => { - const transactionTotals = { test: 123 } - const expectedAction = { - type: UPDATE_TRANSACTION_TOTALS, - payload: transactionTotals, - } - - assert.deepEqual( - actions.updateTransactionTotals(transactionTotals), - expectedAction - ) - }) - - it('should create an action to update tokenProps', () => { - const tokenProps = { - tokenDecimals: '1', - tokenSymbol: 'abc', - } - const expectedAction = { - type: UPDATE_TOKEN_PROPS, - payload: tokenProps, - } - - assert.deepEqual( - actions.updateTokenProps(tokenProps), - expectedAction - ) - }) - - it('should create an action to update nonce', () => { - const nonce = '0x1' - const expectedAction = { - type: UPDATE_NONCE, - payload: nonce, - } - - assert.deepEqual( - actions.updateNonce(nonce), - expectedAction - ) - }) - - it('should create an action to set fetchingData to true', () => { - const expectedAction = { - type: FETCH_DATA_START, - } - - assert.deepEqual( - actions.setFetchingData(true), - expectedAction - ) - }) - - it('should create an action to set fetchingData to false', () => { - const expectedAction = { - type: FETCH_DATA_END, - } - - assert.deepEqual( - actions.setFetchingData(false), - expectedAction - ) - }) - - it('should create an action to clear confirmTransaction', () => { - const expectedAction = { - type: CLEAR_CONFIRM_TRANSACTION, - } - - assert.deepEqual( - actions.clearConfirmTransaction(), - expectedAction - ) - }) - }) - - describe('Thunk actions', done => { - beforeEach(() => { - global.eth = { - getCode: sinon.stub().callsFake( - address => Promise.resolve(address && address.match(/isContract/) ? 'not-0x' : '0x') - ), - } - }) - - afterEach(() => { - global.eth.getCode.resetHistory() - }) - - it('updates txData and gas on an existing transaction in confirmTransaction', () => { - const mockState = { - metamask: { - conversionRate: 468.58, - currentCurrency: 'usd', - }, - confirmTransaction: { - ethTransactionAmount: '1', - ethTransactionFee: '0.000021', - ethTransactionTotal: '1.000021', - fetchingData: false, - fiatTransactionAmount: '469.26', - fiatTransactionFee: '0.01', - fiatTransactionTotal: '469.27', - hexGasTotal: '0x1319718a5000', - methodData: {}, - nonce: '', - tokenData: {}, - tokenProps: { - tokenDecimals: '', - tokenSymbol: '', - }, - txData: { - estimatedGas: '0x5208', - gasLimitSpecified: false, - gasPriceSpecified: false, - history: [], - id: 2603411941761054, - loadingDefaults: false, - metamaskNetworkId: '3', - origin: 'faucet.metamask.io', - simpleSend: true, - status: 'unapproved', - time: 1530838113716, - }, - }, - } - - const middlewares = [thunk] - const mockStore = configureMockStore(middlewares) - const store = mockStore(mockState) - const expectedActions = [ - 'metamask/confirm-transaction/UPDATE_TX_DATA', - 'metamask/confirm-transaction/UPDATE_TRANSACTION_AMOUNTS', - 'metamask/confirm-transaction/UPDATE_TRANSACTION_FEES', - 'metamask/confirm-transaction/UPDATE_TRANSACTION_TOTALS', - ] - - store.dispatch(actions.updateGasAndCalculate({ gasLimit: '0x2', gasPrice: '0x25' })) - - const storeActions = store.getActions() - assert.equal(storeActions.length, expectedActions.length) - storeActions.forEach((action, index) => assert.equal(action.type, expectedActions[index])) - }) - - it('updates txData and updates gas values in confirmTransaction', () => { - const txData = { - estimatedGas: '0x5208', - gasLimitSpecified: false, - gasPriceSpecified: false, - history: [], - id: 2603411941761054, - loadingDefaults: false, - metamaskNetworkId: '3', - origin: 'faucet.metamask.io', - simpleSend: true, - status: 'unapproved', - time: 1530838113716, - txParams: { - from: '0xc5ae6383e126f901dcb06131d97a88745bfa88d6', - gas: '0x33450', - gasPrice: '0x2540be400', - to: '0x81b7e08f65bdf5648606c89998a9cc8164397647', - value: '0xde0b6b3a7640000', - }, - } - const mockState = { - metamask: { - conversionRate: 468.58, - currentCurrency: 'usd', - }, - confirmTransaction: { - ethTransactionAmount: '1', - ethTransactionFee: '0.000021', - ethTransactionTotal: '1.000021', - fetchingData: false, - fiatTransactionAmount: '469.26', - fiatTransactionFee: '0.01', - fiatTransactionTotal: '469.27', - hexGasTotal: '0x1319718a5000', - methodData: {}, - nonce: '', - tokenData: {}, - tokenProps: { - tokenDecimals: '', - tokenSymbol: '', - }, - txData: { - ...txData, - txParams: { - ...txData.txParams, - }, - }, - }, - } - - const middlewares = [thunk] - const mockStore = configureMockStore(middlewares) - const store = mockStore(mockState) - const expectedActions = [ - 'metamask/confirm-transaction/UPDATE_TX_DATA', - 'metamask/confirm-transaction/UPDATE_TRANSACTION_AMOUNTS', - 'metamask/confirm-transaction/UPDATE_TRANSACTION_FEES', - 'metamask/confirm-transaction/UPDATE_TRANSACTION_TOTALS', - ] - - store.dispatch(actions.updateTxDataAndCalculate(txData)) - - const storeActions = store.getActions() - assert.equal(storeActions.length, expectedActions.length) - storeActions.forEach((action, index) => assert.equal(action.type, expectedActions[index])) - }) - - it('updates confirmTransaction transaction', done => { - const mockState = { - metamask: { - conversionRate: 468.58, - currentCurrency: 'usd', - network: '3', - unapprovedTxs: { - 2603411941761054: { - estimatedGas: '0x5208', - gasLimitSpecified: false, - gasPriceSpecified: false, - history: [], - id: 2603411941761054, - loadingDefaults: false, - metamaskNetworkId: '3', - origin: 'faucet.metamask.io', - simpleSend: true, - status: 'unapproved', - time: 1530838113716, - txParams: { - from: '0xc5ae6383e126f901dcb06131d97a88745bfa88d6', - gas: '0x33450', - gasPrice: '0x2540be400', - to: '0x81b7e08f65bdf5648606c89998a9cc8164397647', - value: '0xde0b6b3a7640000', - }, - }, - }, - }, - confirmTransaction: {}, - } - - const middlewares = [thunk] - const mockStore = configureMockStore(middlewares) - const store = mockStore(mockState) - const expectedActions = [ - 'metamask/confirm-transaction/UPDATE_TX_DATA', - 'metamask/confirm-transaction/UPDATE_TRANSACTION_AMOUNTS', - 'metamask/confirm-transaction/UPDATE_TRANSACTION_FEES', - 'metamask/confirm-transaction/UPDATE_TRANSACTION_TOTALS', - ] - - store.dispatch(actions.setTransactionToConfirm(2603411941761054)) - .then(() => { - const storeActions = store.getActions() - assert.equal(storeActions.length, expectedActions.length) - - storeActions.forEach((action, index) => assert.equal(action.type, expectedActions[index])) - done() - }) - }) - }) -}) diff --git a/ui/app/ducks/tests/gas-duck.test.js b/ui/app/ducks/tests/gas-duck.test.js deleted file mode 100644 index cd963aed4..000000000 --- a/ui/app/ducks/tests/gas-duck.test.js +++ /dev/null @@ -1,600 +0,0 @@ -import assert from 'assert' -import sinon from 'sinon' -import proxyquire from 'proxyquire' - - -const GasDuck = proxyquire('../gas.duck.js', { - '../../lib/local-storage-helpers': { - loadLocalStorageData: sinon.spy(), - saveLocalStorageData: sinon.spy(), - }, -}) - -const { - basicGasEstimatesLoadingStarted, - basicGasEstimatesLoadingFinished, - setBasicGasEstimateData, - setCustomGasPrice, - setCustomGasLimit, - setCustomGasTotal, - setCustomGasErrors, - resetCustomGasState, - fetchBasicGasAndTimeEstimates, - fetchBasicGasEstimates, - gasEstimatesLoadingStarted, - gasEstimatesLoadingFinished, - setPricesAndTimeEstimates, - fetchGasEstimates, - setApiEstimatesLastRetrieved, -} = GasDuck -const GasReducer = GasDuck.default - -describe('Gas Duck', () => { - let tempFetch - let tempDateNow - const mockEthGasApiResponse = { - average: 20, - avgWait: 'mockAvgWait', - block_time: 'mockBlock_time', - blockNum: 'mockBlockNum', - fast: 30, - fastest: 40, - fastestWait: 'mockFastestWait', - fastWait: 'mockFastWait', - safeLow: 10, - safeLowWait: 'mockSafeLowWait', - speed: 'mockSpeed', - standard: 20, - } - const mockPredictTableResponse = [ - { expectedTime: 400, expectedWait: 40, gasprice: 0.25, somethingElse: 'foobar' }, - { expectedTime: 200, expectedWait: 20, gasprice: 0.5, somethingElse: 'foobar' }, - { expectedTime: 100, expectedWait: 10, gasprice: 1, somethingElse: 'foobar' }, - { expectedTime: 75, expectedWait: 7.5, gasprice: 1.5, somethingElse: 'foobar' }, - { expectedTime: 50, expectedWait: 5, gasprice: 2, somethingElse: 'foobar' }, - { expectedTime: 35, expectedWait: 4.5, gasprice: 3, somethingElse: 'foobar' }, - { expectedTime: 34, expectedWait: 4.4, gasprice: 3.1, somethingElse: 'foobar' }, - { expectedTime: 25, expectedWait: 4.2, gasprice: 3.5, somethingElse: 'foobar' }, - { expectedTime: 20, expectedWait: 4, gasprice: 4, somethingElse: 'foobar' }, - { expectedTime: 19, expectedWait: 3.9, gasprice: 4.1, somethingElse: 'foobar' }, - { expectedTime: 15, expectedWait: 3, gasprice: 7, somethingElse: 'foobar' }, - { expectedTime: 14, expectedWait: 2.9, gasprice: 7.1, somethingElse: 'foobar' }, - { expectedTime: 12, expectedWait: 2.5, gasprice: 8, somethingElse: 'foobar' }, - { expectedTime: 10, expectedWait: 2, gasprice: 10, somethingElse: 'foobar' }, - { expectedTime: 9, expectedWait: 1.9, gasprice: 10.1, somethingElse: 'foobar' }, - { expectedTime: 5, expectedWait: 1, gasprice: 15, somethingElse: 'foobar' }, - { expectedTime: 4, expectedWait: 0.9, gasprice: 15.1, somethingElse: 'foobar' }, - { expectedTime: 2, expectedWait: 0.8, gasprice: 17, somethingElse: 'foobar' }, - { expectedTime: 1.1, expectedWait: 0.6, gasprice: 19.9, somethingElse: 'foobar' }, - { expectedTime: 1, expectedWait: 0.5, gasprice: 20, somethingElse: 'foobar' }, - ] - const fetchStub = sinon.stub().callsFake((url) => new Promise(resolve => { - const dataToResolve = url.match(/ethgasAPI|gasexpress/) - ? mockEthGasApiResponse - : mockPredictTableResponse - resolve({ - json: () => new Promise(resolve => resolve(dataToResolve)), - }) - })) - - beforeEach(() => { - tempFetch = global.fetch - tempDateNow = global.Date.now - global.fetch = fetchStub - global.Date.now = () => 2000000 - }) - - afterEach(() => { - fetchStub.resetHistory() - global.fetch = tempFetch - global.Date.now = tempDateNow - }) - - const mockState = { - gas: { - mockProp: 123, - }, - } - const initState = { - customData: { - price: null, - limit: null, - }, - basicEstimates: { - average: null, - fastestWait: null, - fastWait: null, - fast: null, - safeLowWait: null, - blockNum: null, - avgWait: null, - blockTime: null, - speed: null, - fastest: null, - safeLow: null, - }, - basicEstimateIsLoading: true, - errors: {}, - gasEstimatesLoading: true, - priceAndTimeEstimates: [], - priceAndTimeEstimatesLastRetrieved: 0, - basicPriceAndTimeEstimates: [], - basicPriceAndTimeEstimatesLastRetrieved: 0, - basicPriceEstimatesLastRetrieved: 0, - } - const BASIC_GAS_ESTIMATE_LOADING_FINISHED = 'metamask/gas/BASIC_GAS_ESTIMATE_LOADING_FINISHED' - const BASIC_GAS_ESTIMATE_LOADING_STARTED = 'metamask/gas/BASIC_GAS_ESTIMATE_LOADING_STARTED' - const GAS_ESTIMATE_LOADING_FINISHED = 'metamask/gas/GAS_ESTIMATE_LOADING_FINISHED' - const GAS_ESTIMATE_LOADING_STARTED = 'metamask/gas/GAS_ESTIMATE_LOADING_STARTED' - const RESET_CUSTOM_GAS_STATE = 'metamask/gas/RESET_CUSTOM_GAS_STATE' - const SET_BASIC_GAS_ESTIMATE_DATA = 'metamask/gas/SET_BASIC_GAS_ESTIMATE_DATA' - const SET_CUSTOM_GAS_ERRORS = 'metamask/gas/SET_CUSTOM_GAS_ERRORS' - const SET_CUSTOM_GAS_LIMIT = 'metamask/gas/SET_CUSTOM_GAS_LIMIT' - const SET_CUSTOM_GAS_PRICE = 'metamask/gas/SET_CUSTOM_GAS_PRICE' - const SET_CUSTOM_GAS_TOTAL = 'metamask/gas/SET_CUSTOM_GAS_TOTAL' - const SET_PRICE_AND_TIME_ESTIMATES = 'metamask/gas/SET_PRICE_AND_TIME_ESTIMATES' - const SET_API_ESTIMATES_LAST_RETRIEVED = 'metamask/gas/SET_API_ESTIMATES_LAST_RETRIEVED' - const SET_BASIC_API_ESTIMATES_LAST_RETRIEVED = 'metamask/gas/SET_BASIC_API_ESTIMATES_LAST_RETRIEVED' - const SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED = 'metamask/gas/SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED' - - describe('GasReducer()', () => { - it('should initialize state', () => { - assert.deepEqual( - GasReducer({}), - initState - ) - }) - - it('should return state unchanged if it does not match a dispatched actions type', () => { - assert.deepEqual( - GasReducer(mockState, { - type: 'someOtherAction', - value: 'someValue', - }), - Object.assign({}, mockState.gas) - ) - }) - - it('should set basicEstimateIsLoading to true when receiving a BASIC_GAS_ESTIMATE_LOADING_STARTED action', () => { - assert.deepEqual( - GasReducer(mockState, { - type: BASIC_GAS_ESTIMATE_LOADING_STARTED, - }), - Object.assign({basicEstimateIsLoading: true}, mockState.gas) - ) - }) - - it('should set basicEstimateIsLoading to false when receiving a BASIC_GAS_ESTIMATE_LOADING_FINISHED action', () => { - assert.deepEqual( - GasReducer(mockState, { - type: BASIC_GAS_ESTIMATE_LOADING_FINISHED, - }), - Object.assign({basicEstimateIsLoading: false}, mockState.gas) - ) - }) - - it('should set gasEstimatesLoading to true when receiving a GAS_ESTIMATE_LOADING_STARTED action', () => { - assert.deepEqual( - GasReducer(mockState, { - type: GAS_ESTIMATE_LOADING_STARTED, - }), - Object.assign({gasEstimatesLoading: true}, mockState.gas) - ) - }) - - it('should set gasEstimatesLoading to false when receiving a GAS_ESTIMATE_LOADING_FINISHED action', () => { - assert.deepEqual( - GasReducer(mockState, { - type: GAS_ESTIMATE_LOADING_FINISHED, - }), - Object.assign({gasEstimatesLoading: false}, mockState.gas) - ) - }) - - it('should return a new object (and not just modify the existing state object)', () => { - assert.deepEqual(GasReducer(mockState), mockState.gas) - assert.notEqual(GasReducer(mockState), mockState.gas) - }) - - it('should set basicEstimates when receiving a SET_BASIC_GAS_ESTIMATE_DATA action', () => { - assert.deepEqual( - GasReducer(mockState, { - type: SET_BASIC_GAS_ESTIMATE_DATA, - value: { someProp: 'someData123' }, - }), - Object.assign({basicEstimates: {someProp: 'someData123'} }, mockState.gas) - ) - }) - - it('should set priceAndTimeEstimates when receiving a SET_PRICE_AND_TIME_ESTIMATES action', () => { - assert.deepEqual( - GasReducer(mockState, { - type: SET_PRICE_AND_TIME_ESTIMATES, - value: { someProp: 'someData123' }, - }), - Object.assign({priceAndTimeEstimates: {someProp: 'someData123'} }, mockState.gas) - ) - }) - - it('should set customData.price when receiving a SET_CUSTOM_GAS_PRICE action', () => { - assert.deepEqual( - GasReducer(mockState, { - type: SET_CUSTOM_GAS_PRICE, - value: 4321, - }), - Object.assign({customData: {price: 4321} }, mockState.gas) - ) - }) - - it('should set customData.limit when receiving a SET_CUSTOM_GAS_LIMIT action', () => { - assert.deepEqual( - GasReducer(mockState, { - type: SET_CUSTOM_GAS_LIMIT, - value: 9876, - }), - Object.assign({customData: {limit: 9876} }, mockState.gas) - ) - }) - - it('should set customData.total when receiving a SET_CUSTOM_GAS_TOTAL action', () => { - assert.deepEqual( - GasReducer(mockState, { - type: SET_CUSTOM_GAS_TOTAL, - value: 10000, - }), - Object.assign({customData: {total: 10000} }, mockState.gas) - ) - }) - - it('should set priceAndTimeEstimatesLastRetrieved when receiving a SET_API_ESTIMATES_LAST_RETRIEVED action', () => { - assert.deepEqual( - GasReducer(mockState, { - type: SET_API_ESTIMATES_LAST_RETRIEVED, - value: 1500000000000, - }), - Object.assign({ priceAndTimeEstimatesLastRetrieved: 1500000000000 }, mockState.gas) - ) - }) - - it('should set priceAndTimeEstimatesLastRetrieved when receiving a SET_BASIC_API_ESTIMATES_LAST_RETRIEVED action', () => { - assert.deepEqual( - GasReducer(mockState, { - type: SET_BASIC_API_ESTIMATES_LAST_RETRIEVED, - value: 1700000000000, - }), - Object.assign({ basicPriceAndTimeEstimatesLastRetrieved: 1700000000000 }, mockState.gas) - ) - }) - - it('should set errors when receiving a SET_CUSTOM_GAS_ERRORS action', () => { - assert.deepEqual( - GasReducer(mockState, { - type: SET_CUSTOM_GAS_ERRORS, - value: { someError: 'error_error' }, - }), - Object.assign({errors: {someError: 'error_error'} }, mockState.gas) - ) - }) - - it('should return the initial state in response to a RESET_CUSTOM_GAS_STATE action', () => { - assert.deepEqual( - GasReducer(mockState, { - type: RESET_CUSTOM_GAS_STATE, - }), - Object.assign({}, initState) - ) - }) - }) - - describe('basicGasEstimatesLoadingStarted', () => { - it('should create the correct action', () => { - assert.deepEqual( - basicGasEstimatesLoadingStarted(), - { type: BASIC_GAS_ESTIMATE_LOADING_STARTED } - ) - }) - }) - - describe('basicGasEstimatesLoadingFinished', () => { - it('should create the correct action', () => { - assert.deepEqual( - basicGasEstimatesLoadingFinished(), - { type: BASIC_GAS_ESTIMATE_LOADING_FINISHED } - ) - }) - }) - - describe('fetchBasicGasEstimates', () => { - const mockDistpatch = sinon.spy() - it('should call fetch with the expected params', async () => { - await fetchBasicGasEstimates()(mockDistpatch, () => ({ gas: Object.assign( - {}, - initState, - { basicPriceAEstimatesLastRetrieved: 1000000 } - ) })) - assert.deepEqual( - mockDistpatch.getCall(0).args, - [{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED} ] - ) - assert.deepEqual( - global.fetch.getCall(0).args, - [ - 'https://dev.blockscale.net/api/gasexpress.json', - { - 'headers': {}, - 'referrer': 'https://dev.blockscale.net/api/', - 'referrerPolicy': 'no-referrer-when-downgrade', - 'body': null, - 'method': 'GET', - 'mode': 'cors', - }, - ] - ) - - assert.deepEqual( - mockDistpatch.getCall(1).args, - [{ type: SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED, value: 2000000 } ] - ) - - assert.deepEqual( - mockDistpatch.getCall(2).args, - [{ - type: SET_BASIC_GAS_ESTIMATE_DATA, - value: { - average: 20, - blockTime: 'mockBlock_time', - blockNum: 'mockBlockNum', - fast: 30, - fastest: 40, - safeLow: 10, - }, - }] - ) - assert.deepEqual( - mockDistpatch.getCall(3).args, - [{ type: BASIC_GAS_ESTIMATE_LOADING_FINISHED }] - ) - }) - }) - - describe('fetchBasicGasAndTimeEstimates', () => { - const mockDistpatch = sinon.spy() - it('should call fetch with the expected params', async () => { - await fetchBasicGasAndTimeEstimates()(mockDistpatch, () => ({ gas: Object.assign( - {}, - initState, - { basicPriceAndTimeEstimatesLastRetrieved: 1000000 } - ) })) - assert.deepEqual( - mockDistpatch.getCall(0).args, - [{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED} ] - ) - assert.deepEqual( - global.fetch.getCall(0).args, - [ - 'https://ethgasstation.info/json/ethgasAPI.json', - { - 'headers': {}, - 'referrer': 'http://ethgasstation.info/json/', - 'referrerPolicy': 'no-referrer-when-downgrade', - 'body': null, - 'method': 'GET', - 'mode': 'cors', - }, - ] - ) - - assert.deepEqual( - mockDistpatch.getCall(1).args, - [{ type: SET_BASIC_API_ESTIMATES_LAST_RETRIEVED, value: 2000000 } ] - ) - - assert.deepEqual( - mockDistpatch.getCall(2).args, - [{ - type: SET_BASIC_GAS_ESTIMATE_DATA, - value: { - average: 2, - avgWait: 'mockAvgWait', - blockTime: 'mockBlock_time', - blockNum: 'mockBlockNum', - fast: 3, - fastest: 4, - fastestWait: 'mockFastestWait', - fastWait: 'mockFastWait', - safeLow: 1, - safeLowWait: 'mockSafeLowWait', - speed: 'mockSpeed', - }, - }] - ) - assert.deepEqual( - mockDistpatch.getCall(3).args, - [{ type: BASIC_GAS_ESTIMATE_LOADING_FINISHED }] - ) - }) - }) - - describe('fetchGasEstimates', () => { - const mockDistpatch = sinon.spy() - - beforeEach(() => { - mockDistpatch.resetHistory() - }) - - it('should call fetch with the expected params', async () => { - global.fetch.resetHistory() - await fetchGasEstimates(5)(mockDistpatch, () => ({ gas: Object.assign( - {}, - initState, - { priceAndTimeEstimatesLastRetrieved: 1000000 } - ) })) - assert.deepEqual( - mockDistpatch.getCall(0).args, - [{ type: GAS_ESTIMATE_LOADING_STARTED} ] - ) - assert.deepEqual( - global.fetch.getCall(0).args, - [ - 'https://ethgasstation.info/json/predictTable.json', - { - 'headers': {}, - 'referrer': 'http://ethgasstation.info/json/', - 'referrerPolicy': 'no-referrer-when-downgrade', - 'body': null, - 'method': 'GET', - 'mode': 'cors', - }, - ] - ) - - assert.deepEqual( - mockDistpatch.getCall(1).args, - [{ type: SET_API_ESTIMATES_LAST_RETRIEVED, value: 2000000 }] - ) - - const { type: thirdDispatchCallType, value: priceAndTimeEstimateResult } = mockDistpatch.getCall(2).args[0] - assert.equal(thirdDispatchCallType, SET_PRICE_AND_TIME_ESTIMATES) - assert(priceAndTimeEstimateResult.length < mockPredictTableResponse.length * 3 - 2) - assert(!priceAndTimeEstimateResult.find(d => d.expectedTime > 100)) - assert(!priceAndTimeEstimateResult.find((d, i, a) => a[a + 1] && d.expectedTime > a[a + 1].expectedTime)) - assert(!priceAndTimeEstimateResult.find((d, i, a) => a[a + 1] && d.gasprice > a[a + 1].gasprice)) - - assert.deepEqual( - mockDistpatch.getCall(3).args, - [{ type: GAS_ESTIMATE_LOADING_FINISHED }] - ) - }) - - it('should not call fetch if the estimates were retrieved < 75000 ms ago', async () => { - global.fetch.resetHistory() - await fetchGasEstimates(5)(mockDistpatch, () => ({ gas: Object.assign( - {}, - initState, - { - priceAndTimeEstimatesLastRetrieved: Date.now(), - priceAndTimeEstimates: [{ - expectedTime: '10', - expectedWait: 2, - gasprice: 50, - }], - } - ) })) - assert.deepEqual( - mockDistpatch.getCall(0).args, - [{ type: GAS_ESTIMATE_LOADING_STARTED} ] - ) - assert.equal(global.fetch.callCount, 0) - - assert.deepEqual( - mockDistpatch.getCall(1).args, - [{ - type: SET_PRICE_AND_TIME_ESTIMATES, - value: [ - { - expectedTime: '10', - expectedWait: 2, - gasprice: 50, - }, - ], - - }] - ) - assert.deepEqual( - mockDistpatch.getCall(2).args, - [{ type: GAS_ESTIMATE_LOADING_FINISHED }] - ) - }) - }) - - describe('gasEstimatesLoadingStarted', () => { - it('should create the correct action', () => { - assert.deepEqual( - gasEstimatesLoadingStarted(), - { type: GAS_ESTIMATE_LOADING_STARTED } - ) - }) - }) - - describe('gasEstimatesLoadingFinished', () => { - it('should create the correct action', () => { - assert.deepEqual( - gasEstimatesLoadingFinished(), - { type: GAS_ESTIMATE_LOADING_FINISHED } - ) - }) - }) - - describe('setPricesAndTimeEstimates', () => { - it('should create the correct action', () => { - assert.deepEqual( - setPricesAndTimeEstimates('mockPricesAndTimeEstimates'), - { type: SET_PRICE_AND_TIME_ESTIMATES, value: 'mockPricesAndTimeEstimates' } - ) - }) - }) - - describe('setBasicGasEstimateData', () => { - it('should create the correct action', () => { - assert.deepEqual( - setBasicGasEstimateData('mockBasicEstimatData'), - { type: SET_BASIC_GAS_ESTIMATE_DATA, value: 'mockBasicEstimatData' } - ) - }) - }) - - describe('setCustomGasPrice', () => { - it('should create the correct action', () => { - assert.deepEqual( - setCustomGasPrice('mockCustomGasPrice'), - { type: SET_CUSTOM_GAS_PRICE, value: 'mockCustomGasPrice' } - ) - }) - }) - - describe('setCustomGasLimit', () => { - it('should create the correct action', () => { - assert.deepEqual( - setCustomGasLimit('mockCustomGasLimit'), - { type: SET_CUSTOM_GAS_LIMIT, value: 'mockCustomGasLimit' } - ) - }) - }) - - describe('setCustomGasTotal', () => { - it('should create the correct action', () => { - assert.deepEqual( - setCustomGasTotal('mockCustomGasTotal'), - { type: SET_CUSTOM_GAS_TOTAL, value: 'mockCustomGasTotal' } - ) - }) - }) - - describe('setCustomGasErrors', () => { - it('should create the correct action', () => { - assert.deepEqual( - setCustomGasErrors('mockErrorObject'), - { type: SET_CUSTOM_GAS_ERRORS, value: 'mockErrorObject' } - ) - }) - }) - - describe('setApiEstimatesLastRetrieved', () => { - it('should create the correct action', () => { - assert.deepEqual( - setApiEstimatesLastRetrieved(1234), - { type: SET_API_ESTIMATES_LAST_RETRIEVED, value: 1234 } - ) - }) - }) - - describe('resetCustomGasState', () => { - it('should create the correct action', () => { - assert.deepEqual( - resetCustomGasState(), - { type: RESET_CUSTOM_GAS_STATE } - ) - }) - }) - -}) diff --git a/ui/app/ducks/tests/send-duck.test.js b/ui/app/ducks/tests/send-duck.test.js deleted file mode 100644 index 43f51c631..000000000 --- a/ui/app/ducks/tests/send-duck.test.js +++ /dev/null @@ -1,186 +0,0 @@ -import assert from 'assert' - -import SendReducer, { - openToDropdown, - closeToDropdown, - updateSendErrors, - showGasButtonGroup, - hideGasButtonGroup, - updateSendWarnings, -} from '../send.duck.js' - -describe('Send Duck', () => { - const mockState = { - send: { - mockProp: 123, - }, - } - const initState = { - fromDropdownOpen: false, - toDropdownOpen: false, - errors: {}, - gasButtonGroupShown: true, - warnings: {}, - } - const OPEN_FROM_DROPDOWN = 'metamask/send/OPEN_FROM_DROPDOWN' - const CLOSE_FROM_DROPDOWN = 'metamask/send/CLOSE_FROM_DROPDOWN' - const OPEN_TO_DROPDOWN = 'metamask/send/OPEN_TO_DROPDOWN' - const CLOSE_TO_DROPDOWN = 'metamask/send/CLOSE_TO_DROPDOWN' - const UPDATE_SEND_ERRORS = 'metamask/send/UPDATE_SEND_ERRORS' - const UPDATE_SEND_WARNINGS = 'metamask/send/UPDATE_SEND_WARNINGS' - const RESET_SEND_STATE = 'metamask/send/RESET_SEND_STATE' - const SHOW_GAS_BUTTON_GROUP = 'metamask/send/SHOW_GAS_BUTTON_GROUP' - const HIDE_GAS_BUTTON_GROUP = 'metamask/send/HIDE_GAS_BUTTON_GROUP' - - describe('SendReducer()', () => { - it('should initialize state', () => { - assert.deepEqual( - SendReducer({}), - initState - ) - }) - - it('should return state unchanged if it does not match a dispatched actions type', () => { - assert.deepEqual( - SendReducer(mockState, { - type: 'someOtherAction', - value: 'someValue', - }), - Object.assign({}, mockState.send) - ) - }) - - it('should set fromDropdownOpen to true when receiving a OPEN_FROM_DROPDOWN action', () => { - assert.deepEqual( - SendReducer(mockState, { - type: OPEN_FROM_DROPDOWN, - }), - Object.assign({fromDropdownOpen: true}, mockState.send) - ) - }) - - it('should return a new object (and not just modify the existing state object)', () => { - assert.deepEqual(SendReducer(mockState), mockState.send) - assert.notEqual(SendReducer(mockState), mockState.send) - }) - - it('should set fromDropdownOpen to false when receiving a CLOSE_FROM_DROPDOWN action', () => { - assert.deepEqual( - SendReducer(mockState, { - type: CLOSE_FROM_DROPDOWN, - }), - Object.assign({fromDropdownOpen: false}, mockState.send) - ) - }) - - it('should set toDropdownOpen to true when receiving a OPEN_TO_DROPDOWN action', () => { - assert.deepEqual( - SendReducer(mockState, { - type: OPEN_TO_DROPDOWN, - }), - Object.assign({toDropdownOpen: true}, mockState.send) - ) - }) - - it('should set toDropdownOpen to false when receiving a CLOSE_TO_DROPDOWN action', () => { - assert.deepEqual( - SendReducer(mockState, { - type: CLOSE_TO_DROPDOWN, - }), - Object.assign({toDropdownOpen: false}, mockState.send) - ) - }) - - it('should set gasButtonGroupShown to true when receiving a SHOW_GAS_BUTTON_GROUP action', () => { - assert.deepEqual( - SendReducer(Object.assign({}, mockState, { gasButtonGroupShown: false }), { - type: SHOW_GAS_BUTTON_GROUP, - }), - Object.assign({gasButtonGroupShown: true}, mockState.send) - ) - }) - - it('should set gasButtonGroupShown to false when receiving a HIDE_GAS_BUTTON_GROUP action', () => { - assert.deepEqual( - SendReducer(mockState, { - type: HIDE_GAS_BUTTON_GROUP, - }), - Object.assign({gasButtonGroupShown: false}, mockState.send) - ) - }) - - it('should extend send.errors with the value of a UPDATE_SEND_ERRORS action', () => { - const modifiedMockState = Object.assign({}, mockState, { - send: { - errors: { - someError: false, - }, - }, - }) - assert.deepEqual( - SendReducer(modifiedMockState, { - type: UPDATE_SEND_ERRORS, - value: { someOtherError: true }, - }), - Object.assign({}, modifiedMockState.send, { - errors: { - someError: false, - someOtherError: true, - }, - }) - ) - }) - - it('should return the initial state in response to a RESET_SEND_STATE action', () => { - assert.deepEqual( - SendReducer(mockState, { - type: RESET_SEND_STATE, - }), - Object.assign({}, initState) - ) - }) - }) - - describe('openToDropdown', () => { - assert.deepEqual( - openToDropdown(), - { type: OPEN_TO_DROPDOWN } - ) - }) - - describe('closeToDropdown', () => { - assert.deepEqual( - closeToDropdown(), - { type: CLOSE_TO_DROPDOWN } - ) - }) - - describe('showGasButtonGroup', () => { - assert.deepEqual( - showGasButtonGroup(), - { type: SHOW_GAS_BUTTON_GROUP } - ) - }) - - describe('hideGasButtonGroup', () => { - assert.deepEqual( - hideGasButtonGroup(), - { type: HIDE_GAS_BUTTON_GROUP } - ) - }) - - describe('updateSendErrors', () => { - assert.deepEqual( - updateSendErrors('mockErrorObject'), - { type: UPDATE_SEND_ERRORS, value: 'mockErrorObject' } - ) - }) - - describe('updateSendWarnings', () => { - assert.deepEqual( - updateSendWarnings('mockWarningObject'), - { type: UPDATE_SEND_WARNINGS, value: 'mockWarningObject' } - ) - }) - -}) |