1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-23 03:36:18 +02:00
metamask-extension/test/unit/ui/app/reducers/app.spec.js

375 lines
9.3 KiB
JavaScript
Raw Normal View History

2018-10-10 10:12:46 +02:00
import assert from 'assert'
import reduceApp from '../../../../../ui/app/ducks/app/app'
import * as actionConstants from '../../../../../ui/app/store/actionConstants'
const actions = actionConstants
2018-10-10 10:12:46 +02:00
describe('App State', function () {
2018-10-10 10:12:46 +02:00
const metamaskState = {
selectedAddress: '0xAddress',
identities: {
'0xAddress': {
name: 'account 1',
address: '0xAddress',
2018-10-10 10:12:46 +02:00
},
},
}
it('App init state', function () {
2018-10-31 13:45:27 +01:00
const initState = reduceApp(metamaskState, {})
2018-10-10 10:12:46 +02:00
assert(initState)
})
it('sets networkDropdownOpen dropdown to true', function () {
2018-10-31 13:45:27 +01:00
const state = reduceApp(metamaskState, {
2018-10-10 10:12:46 +02:00
type: actions.NETWORK_DROPDOWN_OPEN,
})
2018-10-31 13:45:27 +01:00
assert.equal(state.networkDropdownOpen, true)
2018-10-10 10:12:46 +02:00
})
it('sets networkDropdownOpen dropdown to false', function () {
2018-10-31 13:45:27 +01:00
const dropdown = { networkDropdowopen: true }
2019-12-03 21:50:55 +01:00
const state = { ...metamaskState, ...dropdown }
2018-10-10 10:12:46 +02:00
const newState = reduceApp(state, {
type: actions.NETWORK_DROPDOWN_CLOSE,
})
assert.equal(newState.networkDropdownOpen, false)
})
it('opens sidebar', function () {
2018-10-10 10:12:46 +02:00
const value = {
2020-11-03 00:41:28 +01:00
transitionName: 'sidebar-right',
type: 'wallet-view',
isOpen: true,
2018-10-10 10:12:46 +02:00
}
2018-10-31 13:45:27 +01:00
const state = reduceApp(metamaskState, {
2018-10-10 10:12:46 +02:00
type: actions.SIDEBAR_OPEN,
value,
})
2018-10-31 13:45:27 +01:00
assert.deepEqual(state.sidebar, value)
2018-10-10 10:12:46 +02:00
})
it('closes sidebar', function () {
2019-12-03 21:50:55 +01:00
const openSidebar = { sidebar: { isOpen: true } }
const state = { ...metamaskState, ...openSidebar }
2018-10-10 10:12:46 +02:00
const newState = reduceApp(state, {
type: actions.SIDEBAR_CLOSE,
})
assert.equal(newState.sidebar.isOpen, false)
})
it('opens alert', function () {
2018-10-31 13:45:27 +01:00
const state = reduceApp(metamaskState, {
2018-10-10 10:12:46 +02:00
type: actions.ALERT_OPEN,
value: 'test message',
})
2018-10-31 13:45:27 +01:00
assert.equal(state.alertOpen, true)
assert.equal(state.alertMessage, 'test message')
2018-10-10 10:12:46 +02:00
})
it('closes alert', function () {
2018-10-31 13:45:27 +01:00
const alert = { alertOpen: true, alertMessage: 'test message' }
2019-12-03 21:50:55 +01:00
const state = { ...metamaskState, ...alert }
2018-10-10 10:12:46 +02:00
const newState = reduceApp(state, {
type: actions.ALERT_CLOSE,
})
assert.equal(newState.alertOpen, false)
assert.equal(newState.alertMessage, null)
})
it('detects qr code data', function () {
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.QR_CODE_DETECTED,
value: 'qr data',
})
assert.equal(state.qrCodeData, 'qr data')
})
it('opens modal', function () {
2018-10-31 13:45:27 +01:00
const state = reduceApp(metamaskState, {
2018-10-10 10:12:46 +02:00
type: actions.MODAL_OPEN,
payload: {
name: 'test',
},
})
2018-10-31 13:45:27 +01:00
assert.equal(state.modal.open, true)
assert.equal(state.modal.modalState.name, 'test')
2018-10-10 10:12:46 +02:00
})
it('closes modal, but moves open modal state to previous modal state', function () {
2018-10-31 13:45:27 +01:00
const opensModal = {
modal: {
open: true,
modalState: {
name: 'test',
},
},
}
2018-10-10 10:12:46 +02:00
const state = { ...metamaskState, appState: { ...opensModal } }
const newState = reduceApp(state, {
type: actions.MODAL_CLOSE,
})
assert.equal(newState.modal.open, false)
assert.equal(newState.modal.modalState.name, null)
})
it('shows send token page', function () {
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.SHOW_SEND_TOKEN_PAGE,
})
assert.equal(state.warning, null)
})
it('locks Metamask', function () {
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.LOCK_METAMASK,
})
assert.equal(state.warning, null)
})
it('goes home', function () {
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.GO_HOME,
})
assert.equal(state.accountDetail.subview, 'transactions')
assert.equal(state.accountDetail.accountExport, 'none')
assert.equal(state.accountDetail.privateKey, '')
assert.equal(state.warning, null)
})
it('shows account detail', function () {
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.SHOW_ACCOUNT_DETAIL,
value: 'context address',
})
assert.equal(state.forgottenPassword, null) // default
assert.equal(state.accountDetail.subview, 'transactions') // default
assert.equal(state.accountDetail.accountExport, 'none') // default
assert.equal(state.accountDetail.privateKey, '') // default
})
it('clears account details', function () {
const exportPrivKeyModal = {
accountDetail: {
subview: 'export',
accountExport: 'completed',
privateKey: 'a-priv-key',
},
}
const state = { ...metamaskState, appState: { ...exportPrivKeyModal } }
const newState = reduceApp(state, {
type: actions.CLEAR_ACCOUNT_DETAILS,
})
assert.deepStrictEqual(newState.accountDetail, {})
})
it('shoes account page', function () {
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.SHOW_ACCOUNTS_PAGE,
})
assert.equal(state.isLoading, false)
assert.equal(state.warning, null)
assert.equal(state.scrollToBottom, false)
assert.equal(state.forgottenPassword, false)
})
it('shows confirm tx page', function () {
2018-10-10 10:12:46 +02:00
const txs = {
unapprovedTxs: {
1: {
id: 1,
},
2: {
id: 2,
},
},
}
const oldState = { ...metamaskState, ...txs }
2018-10-10 10:12:46 +02:00
const state = reduceApp(oldState, {
type: actions.SHOW_CONF_TX_PAGE,
id: 2,
})
assert.equal(state.txId, 2)
2018-10-10 10:12:46 +02:00
assert.equal(state.warning, null)
assert.equal(state.isLoading, false)
})
it('completes tx continues to show pending txs current view context', function () {
2018-10-10 10:12:46 +02:00
const txs = {
unapprovedTxs: {
1: {
id: 1,
},
2: {
id: 2,
},
},
}
const oldState = { ...metamaskState, ...txs }
2018-10-10 10:12:46 +02:00
const state = reduceApp(oldState, {
type: actions.COMPLETED_TX,
value: {
id: 1,
},
2018-10-10 10:12:46 +02:00
})
assert.equal(state.txId, null)
2018-10-10 10:12:46 +02:00
assert.equal(state.warning, null)
})
it('returns to account detail page when no unconf actions completed tx', function () {
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.COMPLETED_TX,
value: {
unconfirmedActionsCount: 0,
},
2018-10-10 10:12:46 +02:00
})
assert.equal(state.warning, null)
assert.equal(state.accountDetail.subview, 'transactions')
})
it('sets default warning when unlock fails', function () {
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.UNLOCK_FAILED,
})
assert.equal(state.warning, 'Incorrect password. Try again.')
})
it('sets errors when unlock fails', function () {
2018-10-31 13:45:27 +01:00
const state = reduceApp(metamaskState, {
2018-10-10 10:12:46 +02:00
type: actions.UNLOCK_FAILED,
value: 'errors',
})
2018-10-31 13:45:27 +01:00
assert.equal(state.warning, 'errors')
2018-10-10 10:12:46 +02:00
})
it('sets warning to empty string when unlock succeeds', function () {
2018-10-31 13:45:27 +01:00
const errorState = { warning: 'errors' }
2019-12-03 21:50:55 +01:00
const oldState = { ...metamaskState, ...errorState }
2018-10-10 10:12:46 +02:00
const state = reduceApp(oldState, {
type: actions.UNLOCK_SUCCEEDED,
})
assert.equal(state.warning, '')
})
it('sets hardware wallet default hd path', function () {
2018-10-10 10:12:46 +02:00
const hdPaths = {
trezor: "m/44'/60'/0'/0",
ledger: "m/44'/60'/0'",
}
const state = reduceApp(metamaskState, {
type: actions.SET_HARDWARE_WALLET_DEFAULT_HD_PATH,
value: {
device: 'ledger',
path: "m/44'/60'/0'",
},
})
assert.deepEqual(state.defaultHdPaths, hdPaths)
})
it('shows loading message', function () {
2018-10-31 13:45:27 +01:00
const state = reduceApp(metamaskState, {
2018-10-10 10:12:46 +02:00
type: actions.SHOW_LOADING,
value: 'loading',
})
2018-10-31 13:45:27 +01:00
assert.equal(state.isLoading, true)
assert.equal(state.loadingMessage, 'loading')
2018-10-10 10:12:46 +02:00
})
it('hides loading message', function () {
2019-12-03 21:50:55 +01:00
const loadingState = { isLoading: true }
const oldState = { ...metamaskState, ...loadingState }
2018-10-10 10:12:46 +02:00
const state = reduceApp(oldState, {
type: actions.HIDE_LOADING,
})
assert.equal(state.isLoading, false)
})
it('displays warning', function () {
2018-10-31 13:45:27 +01:00
const state = reduceApp(metamaskState, {
2018-10-10 10:12:46 +02:00
type: actions.DISPLAY_WARNING,
value: 'warning',
})
2018-10-31 13:45:27 +01:00
assert.equal(state.isLoading, false)
assert.equal(state.warning, 'warning')
2018-10-10 10:12:46 +02:00
})
it('hides warning', function () {
2019-12-03 21:50:55 +01:00
const displayWarningState = { warning: 'warning' }
const oldState = { ...metamaskState, ...displayWarningState }
2018-10-10 10:12:46 +02:00
const state = reduceApp(oldState, {
type: actions.HIDE_WARNING,
})
assert.equal(state.warning, undefined)
})
it('shows private key', function () {
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.SHOW_PRIVATE_KEY,
value: 'private key',
})
assert.equal(state.accountDetail.subview, 'export')
assert.equal(state.accountDetail.accountExport, 'completed')
assert.equal(state.accountDetail.privateKey, 'private key')
})
it('set mouse user state', function () {
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.SET_MOUSE_USER_STATE,
value: true,
})
assert.equal(state.isMouseUser, true)
})
it('sets gas loading', function () {
2018-10-31 13:45:27 +01:00
const state = reduceApp(metamaskState, {
2018-10-10 10:12:46 +02:00
type: actions.GAS_LOADING_STARTED,
})
2018-10-31 13:45:27 +01:00
assert.equal(state.gasIsLoading, true)
2018-10-10 10:12:46 +02:00
})
it('unsets gas loading', function () {
2018-10-31 13:45:27 +01:00
const gasLoadingState = { gasIsLoading: true }
2019-12-03 21:50:55 +01:00
const oldState = { ...metamaskState, ...gasLoadingState }
2018-10-10 10:12:46 +02:00
const state = reduceApp(oldState, {
type: actions.GAS_LOADING_FINISHED,
})
assert.equal(state.gasIsLoading, false)
})
})