From be3f411a6016fc363029ef618c9a58bf4d0cf015 Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 10 Oct 2018 01:12:46 -0700 Subject: Reducers --- test/unit/ui/app/reducers/app.spec.js | 987 +++++++++++++++++++++++++++++ test/unit/ui/app/reducers/metamask.spec.js | 504 +++++++++++++++ 2 files changed, 1491 insertions(+) create mode 100644 test/unit/ui/app/reducers/app.spec.js create mode 100644 test/unit/ui/app/reducers/metamask.spec.js (limited to 'test/unit/ui/app/reducers') diff --git a/test/unit/ui/app/reducers/app.spec.js b/test/unit/ui/app/reducers/app.spec.js new file mode 100644 index 000000000..7cd17a25a --- /dev/null +++ b/test/unit/ui/app/reducers/app.spec.js @@ -0,0 +1,987 @@ +import assert from 'assert' +import reduceApp from '../../../../../ui/app/reducers/app' +import * as actions from '../../../../../ui/app/actions' + +describe('App State', () => { + let initState, + dropdownOpen, + openSidebar, + alertOpen, + opensModal, + transitionForwardState, + errorState, + loadingState, + subLoadingIndicatorState, + displayWarningState, + requestAccountExportState, + buyEthState, + gasLoadingState + + const metamaskState = { + metamask: { + selectedAddress: '0xAddress', + identities: { + '0xAddress': { + name: 'account 1', + address: '0xAddress', + }, + }, + }, + } + + it('App init state', () => { + initState = reduceApp(metamaskState, {}) + + assert(initState) + }) + + it('sets networkd dropdown to true', () => { + dropdownOpen = reduceApp(metamaskState, { + type: actions.NETWORK_DROPDOWN_OPEN, + }) + + assert.equal(dropdownOpen.networkDropdownOpen, true) + }) + + it('sets networkd dropdown to false', () => { + const state = {...metamaskState, ...dropdownOpen} + const newState = reduceApp(state, { + type: actions.NETWORK_DROPDOWN_CLOSE, + }) + + assert.equal(newState.networkDropdownOpen, false) + }) + + it('opens sidebar', () => { + const value = { + 'transitionName': 'sidebar-right', + 'type': 'wallet-view', + 'isOpen': true, + } + openSidebar = reduceApp(metamaskState, { + type: actions.SIDEBAR_OPEN, + value, + }) + + assert.deepEqual(openSidebar.sidebar, value) + }) + + it('closes sidebar', () => { + const state = {...metamaskState, ...openSidebar} + + const newState = reduceApp(state, { + type: actions.SIDEBAR_CLOSE, + }) + + assert.equal(newState.sidebar.isOpen, false) + }) + + it('opens alert', () => { + alertOpen = reduceApp(metamaskState, { + type: actions.ALERT_OPEN, + value: 'test message', + }) + + assert.equal(alertOpen.alertOpen, true) + assert.equal(alertOpen.alertMessage, 'test message') + }) + + it('closes alert', () => { + const state = {...metamaskState, ...alertOpen} + const newState = reduceApp(state, { + type: actions.ALERT_CLOSE, + }) + + assert.equal(newState.alertOpen, false) + assert.equal(newState.alertMessage, null) + }) + + it('detects qr code data', () => { + const state = reduceApp(metamaskState, { + type: actions.QR_CODE_DETECTED, + value: 'qr data', + }) + + assert.equal(state.qrCodeData, 'qr data') + }) + + it('opens modal', () => { + opensModal = reduceApp(metamaskState, { + type: actions.MODAL_OPEN, + payload: { + name: 'test', + }, + }) + + assert.equal(opensModal.modal.open, true) + assert.equal(opensModal.modal.modalState.name, 'test') + }) + + it('closes modal, but moves open modal state to previous modal state', () => { + 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('tansitions forwards', () => { + transitionForwardState = reduceApp(metamaskState, { + type: actions.TRANSITION_FORWARD, + }) + + assert.equal(transitionForwardState.transForward, true) + }) + + it('transition backwards', () => { + const state = { ...metamaskState, ...transitionForwardState } + const newState = reduceApp(state, { + type: actions.TRANSITION_BACKWARD, + }) + + assert.equal(newState.transForward, false) + }) + + it('shows create vault', () => { + const state = reduceApp(metamaskState, { + type: actions.SHOW_CREATE_VAULT, + }) + + assert.equal(state.currentView.name, 'createVault') + assert.equal(state.transForward, true) + assert.equal(state.warning, null) + }) + + it('shows restore vault', () => { + const state = reduceApp(metamaskState, { + type: actions.SHOW_RESTORE_VAULT, + }) + + assert.equal(state.currentView.name, 'restoreVault') + assert.equal(state.transForward, true) + assert.equal(state.forgottenPassword, true) + }) + + it('sets forgot password', () => { + const state = reduceApp(metamaskState, { + type: actions.FORGOT_PASSWORD, + value: true, + }) + + assert.equal(state.currentView.name, 'restoreVault') + }) + + it('shows init menu', () => { + const state = reduceApp(metamaskState, { + type: actions.SHOW_INIT_MENU, + }) + + assert.equal(state.currentView.name, 'accountDetail') + assert.equal(state.currentView.context, '0xAddress') + }) + + it('shows config page', () => { + const state = reduceApp(metamaskState, { + type: actions.SHOW_CONFIG_PAGE, + value: true, + }) + + assert.equal(state.currentView.name, 'config') + assert.equal(state.currentView.context, '0xAddress') + assert.equal(state.transForward, true) + }) + + it('shows add token page', () => { + const state = reduceApp(metamaskState, { + type: actions.SHOW_ADD_TOKEN_PAGE, + value: true, + }) + + assert.equal(state.currentView.name, 'add-token') + assert.equal(state.currentView.context, '0xAddress') + assert.equal(state.transForward, true) + }) + + it('shows add suggested token page', () => { + const state = reduceApp(metamaskState, { + type: actions.SHOW_ADD_SUGGESTED_TOKEN_PAGE, + value: true, + }) + + assert.equal(state.currentView.name, 'add-suggested-token') + assert.equal(state.currentView.context, '0xAddress') + assert.equal(state.transForward, true) + }) + + it('shows import page', () => { + const state = reduceApp(metamaskState, { + type: actions.SHOW_IMPORT_PAGE, + }) + + assert.equal(state.currentView.name, 'import-menu') + assert.equal(state.transForward, true) + assert.equal(state.warning, null) + }) + + it('shows new account page', () => { + const state = reduceApp(metamaskState, { + type: actions.SHOW_NEW_ACCOUNT_PAGE, + formToSelect: 'context', + }) + + assert.equal(state.currentView.name, 'new-account-page') + assert.equal(state.currentView.context, 'context') + assert.equal(state.transForward, true) + assert.equal(state.warning, null) + }) + + it('sets new account form', () => { + const state = reduceApp(metamaskState, { + type: actions.SET_NEW_ACCOUNT_FORM, + formToSelect: 'context', + }) + + assert.equal(state.currentView.name, 'accountDetail') + assert.equal(state.currentView.context, 'context') + }) + + it('shows info page', () => { + const state = reduceApp(metamaskState, { + type: actions.SHOW_INFO_PAGE, + }) + + assert.equal(state.currentView.name, 'info') + assert.equal(state.currentView.context, '0xAddress') + assert.equal(state.transForward, true) + }) + + it('creates new vault in progress', () => { + const state = reduceApp(metamaskState, { + type: actions.CREATE_NEW_VAULT_IN_PROGRESS, + }) + + assert.equal(state.currentView.name, 'createVault') + assert.equal(state.currentView.inProgress, true) + assert.equal(state.transForward, true) + assert.equal(state.isLoading, true) + }) + + it('shows new vault seed', () => { + const state = reduceApp(metamaskState, { + type: actions.SHOW_NEW_VAULT_SEED, + value: 'test seed words', + }) + + assert.equal(state.currentView.name, 'createVaultComplete') + assert.equal(state.currentView.seedWords, 'test seed words') + assert.equal(state.transForward, true) + assert.equal(state.isLoading, false) + }) + + it('shows new account screen', () => { + const state = reduceApp(metamaskState, { + type: actions.NEW_ACCOUNT_SCREEN, + }) + + assert.equal(state.currentView.name, 'new-account') + assert.equal(state.currentView.context, '0xAddress') + assert.equal(state.transForward, true) + }) + + it('shows send page', () => { + const state = reduceApp(metamaskState, { + type: actions.SHOW_SEND_PAGE, + }) + + assert.equal(state.currentView.name, 'sendTransaction') + assert.equal(state.currentView.context, '0xAddress') + assert.equal(state.transForward, true) + assert.equal(state.warning, null) + }) + + it('shows send token page', () => { + const state = reduceApp(metamaskState, { + type: actions.SHOW_SEND_TOKEN_PAGE, + }) + + assert.equal(state.currentView.name, 'sendToken') + assert.equal(state.currentView.context, '0xAddress') + assert.equal(state.transForward, true) + assert.equal(state.warning, null) + }) + + it('shows new keychain', () => { + const state = reduceApp(metamaskState, { + type: actions.SHOW_NEW_KEYCHAIN, + }) + + assert.equal(state.currentView.name, 'newKeychain') + assert.equal(state.currentView.context, '0xAddress') + assert.equal(state.transForward, true) + }) + + it('unlocks Metamask', () => { + const state = reduceApp(metamaskState, { + type: actions.UNLOCK_METAMASK, + }) + + assert.equal(state.forgottenPassword, null) + assert.deepEqual(state.detailView, {}) + assert.equal(state.transForward, true) + assert.equal(state.warning, null) + }) + + it('locks Metamask', () => { + const state = reduceApp(metamaskState, { + type: actions.LOCK_METAMASK, + }) + + assert.equal(state.currentView.name, 'accountDetail') + assert.equal(state.currentView.context, '0xAddress') + assert.equal(state.transForward, false) + assert.equal(state.warning, null) + }) + + it('goes back to init menu', () => { + const state = reduceApp(metamaskState, { + type: actions.BACK_TO_INIT_MENU, + }) + + assert.equal(state.currentView.name, 'InitMenu') + assert.equal(state.transForward, false) + assert.equal(state.warning, null) + assert.equal(state.forgottenPassword, true) + }) + + it('goes back to unlock view', () => { + const state = reduceApp(metamaskState, { + type: actions.BACK_TO_UNLOCK_VIEW, + }) + + assert.equal(state.currentView.name, 'UnlockScreen') + assert.equal(state.transForward, true) + assert.equal(state.warning, null) + assert.equal(state.forgottenPassword, false) + }) + + it('reveals seed words', () => { + const state = reduceApp(metamaskState, { + type: actions.REVEAL_SEED_CONFIRMATION, + }) + + assert.equal(state.currentView.name, 'reveal-seed-conf') + assert.equal(state.transForward, true) + assert.equal(state.warning, null) + }) + + it('sets selected account', () => { + const state = reduceApp(metamaskState, { + type: actions.SET_SELECTED_ACCOUNT, + value: 'active address', + }) + + assert.equal(state.activeAddress, 'active address') + }) + + it('goes home', () => { + const state = reduceApp(metamaskState, { + type: actions.GO_HOME, + }) + + assert.equal(state.currentView.name, 'accountDetail') + assert.equal(state.accountDetail.subview, 'transactions') + assert.equal(state.accountDetail.accountExport, 'none') + assert.equal(state.accountDetail.privateKey, '') + assert.equal(state.transForward, false) + assert.equal(state.warning, null) + + }) + + it('shows account detail', () => { + const state = reduceApp(metamaskState, { + type: actions.SHOW_ACCOUNT_DETAIL, + value: 'context address', + }) + assert.equal(state.forgottenPassword, null) // default + assert.equal(state.currentView.name, 'accountDetail') + assert.equal(state.currentView.context, 'context address') + assert.equal(state.accountDetail.subview, 'transactions') // default + assert.equal(state.accountDetail.accountExport, 'none') // default + assert.equal(state.accountDetail.privateKey, '') // default + assert.equal(state.transForward, false) + + }) + + it('goes back to account detail', () => { + const state = reduceApp(metamaskState, { + type: actions.BACK_TO_ACCOUNT_DETAIL, + value: 'context address', + }) + assert.equal(state.forgottenPassword, null) // default + assert.equal(state.currentView.name, 'accountDetail') + assert.equal(state.currentView.context, 'context address') + assert.equal(state.accountDetail.subview, 'transactions') // default + assert.equal(state.accountDetail.accountExport, 'none') // default + assert.equal(state.accountDetail.privateKey, '') // default + assert.equal(state.transForward, false) + + }) + + it('shoes account page', () => { + const state = reduceApp(metamaskState, { + type: actions.SHOW_ACCOUNTS_PAGE, + }) + + assert.equal(state.currentView.name, 'accounts') + assert.equal(state.currentView.seedWords, undefined) + assert.equal(state.transForward, true) + assert.equal(state.isLoading, false) + assert.equal(state.warning, null) + assert.equal(state.scrollToBottom, false) + assert.equal(state.forgottenPassword, false) + }) + + it('shows notice', () => { + const state = reduceApp(metamaskState, { + type: actions.SHOW_NOTICE, + }) + + assert.equal(state.transForward, true) + assert.equal(state.isLoading, false) + }) + + it('reveals account', () => { + const state = reduceApp(metamaskState, { + type: actions.REVEAL_ACCOUNT, + }) + assert.equal(state.scrollToBottom, true) + }) + + it('shows confirm tx page', () => { + const txs = { + unapprovedTxs: { + 1: { + id: 1, + }, + 2: { + id: 2, + }, + }, + } + const oldState = { + metamask: {...metamaskState.metamask, ...txs}, + } + const state = reduceApp(oldState, { + type: actions.SHOW_CONF_TX_PAGE, + id: 2, + transForward: false, + }) + + assert.equal(state.currentView.name, 'confTx') + assert.equal(state.currentView.context, 1) + assert.equal(state.transForward, false) + assert.equal(state.warning, null) + assert.equal(state.isLoading, false) + + }) + + it('shows confirm msg page', () => { + const msgs = { + unapprovedMsgs: { + 1: { + id: 1, + }, + 2: { + id: 2, + }, + }, + } + + const oldState = { + metamask: {...metamaskState, ...msgs}, + } + + const state = reduceApp(oldState, { + type: actions.SHOW_CONF_MSG_PAGE, + }) + + assert.equal(state.currentView.name, 'confTx') + assert.equal(state.currentView.context, 0) + assert.equal(state.transForward, true) + assert.equal(state.warning, null) + assert.equal(state.isLoading, false) + + }) + + it('completes tx continues to show pending txs current view context', () => { + const txs = { + unapprovedTxs: { + 1: { + id: 1, + }, + 2: { + id: 2, + }, + }, + } + + const oldState = { + metamask: {...metamaskState, ...txs}, + } + + const state = reduceApp(oldState, { + type: actions.COMPLETED_TX, + value: 1, + }) + + assert.equal(state.currentView.name, 'confTx') + assert.equal(state.currentView.context, 0) + assert.equal(state.transForward, false) + assert.equal(state.warning, null) + }) + + it('returns to account detail page when no unconf actions completed tx', () => { + const state = reduceApp(metamaskState, { + type: actions.COMPLETED_TX, + }) + + assert.equal(state.currentView.name, 'accountDetail') + assert.equal(state.currentView.context, '0xAddress') + assert.equal(state.transForward, false) + assert.equal(state.warning, null) + assert.equal(state.accountDetail.subview, 'transactions') + + }) + + it('proceeds to change current view context in confTx', () => { + + const oldState = { + metamask: {metamaskState}, + appState: {currentView: {context: 0}}, + } + + const state = reduceApp(oldState, { + type: actions.NEXT_TX, + }) + + assert.equal(state.currentView.name, 'confTx') + assert.equal(state.currentView.context, 1) + assert.equal(state.warning, null) + }) + + it('views pending tx', () => { + const txs = { + unapprovedTxs: { + 1: { + id: 1, + }, + 2: { + id: 2, + }, + }, + } + + + const oldState = { + metamask: {...metamaskState, ...txs}, + } + + const state = reduceApp(oldState, { + type: actions.VIEW_PENDING_TX, + value: 2, + }) + + assert.equal(state.currentView.name, 'confTx') + assert.equal(state.currentView.context, 1) + assert.equal(state.warning, null) + }) + + it('views previous tx', () => { + const txs = { + unapprovedTxs: { + 1: { + id: 1, + }, + 2: { + id: 2, + }, + }, + } + + + const oldState = { + metamask: {...metamaskState, ...txs}, + } + + const state = reduceApp(oldState, { + type: actions.VIEW_PENDING_TX, + value: 2, + }) + + assert.equal(state.currentView.name, 'confTx') + assert.equal(state.currentView.context, 1) + assert.equal(state.warning, null) + }) + + it('sets error message in confTx view', () => { + const state = reduceApp(metamaskState, { + type: actions.TRANSACTION_ERROR, + }) + + assert.equal(state.currentView.name, 'confTx') + assert.equal(state.currentView.errorMessage, 'There was a problem submitting this transaction.') + }) + + it('sets default warning when unlock fails', () => { + const state = reduceApp(metamaskState, { + type: actions.UNLOCK_FAILED, + }) + + assert.equal(state.warning, 'Incorrect password. Try again.') + }) + + it('sets default warning when unlock fails', () => { + errorState = reduceApp(metamaskState, { + type: actions.UNLOCK_FAILED, + value: 'errors', + }) + + assert.equal(errorState.warning, 'errors') + }) + + it('sets warning to empty string when unlock succeeds', () => { + const oldState = {...metamaskState, ...errorState} + const state = reduceApp(oldState, { + type: actions.UNLOCK_SUCCEEDED, + }) + + assert.equal(state.warning, '') + }) + + it('sets hardware wallet default hd path', () => { + 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', () => { + loadingState = reduceApp(metamaskState, { + type: actions.SHOW_LOADING, + value: 'loading', + }) + + assert.equal(loadingState.isLoading, true) + assert.equal(loadingState.loadingMessage, 'loading') + }) + + it('hides loading message', () => { + const oldState = {...metamaskState, ...loadingState} + + const state = reduceApp(oldState, { + type: actions.HIDE_LOADING, + }) + + assert.equal(state.isLoading, false) + }) + + it('shows sub loading indicator', () => { + subLoadingIndicatorState = reduceApp(metamaskState, { + type: actions.SHOW_SUB_LOADING_INDICATION, + }) + + assert.equal(subLoadingIndicatorState.isSubLoading, true) + }) + + it('hides sub loading indicator', () => { + const oldState = {...metamaskState, ...oldState} + const state = reduceApp(oldState, { + type: actions.HIDE_SUB_LOADING_INDICATION, + }) + + assert.equal(state.isSubLoading, false) + }) + + it('displays warning', () => { + displayWarningState = reduceApp(metamaskState, { + type: actions.DISPLAY_WARNING, + value: 'warning', + }) + + assert.equal(displayWarningState.isLoading, false) + assert.equal(displayWarningState.warning, 'warning') + }) + + it('hides warning', () => { + const oldState = {...metamaskState, ...displayWarningState} + const state = reduceApp(oldState, { + type: actions.HIDE_WARNING, + }) + + assert.equal(state.warning, undefined) + }) + + it('request to display account export', () => { + requestAccountExportState = reduceApp(metamaskState, { + type: actions.REQUEST_ACCOUNT_EXPORT, + }) + + assert.equal(requestAccountExportState.transForward, true) + assert.equal(requestAccountExportState.accountDetail.subview, 'export') + assert.equal(requestAccountExportState.accountDetail.accountExport, 'requested') + }) + + it('completes account export', () => { + const oldState = {...metamaskState, ...requestAccountExportState} + const state = reduceApp(oldState, { + type: actions.EXPORT_ACCOUNT, + }) + + assert.equal(state.accountDetail.subview, 'export') + assert.equal(state.accountDetail.accountExport, 'completed') + }) + + it('shows private key', () => { + 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('shows buy eth view', () => { + + buyEthState = reduceApp(metamaskState, { + type: actions.BUY_ETH_VIEW, + value: '0xAddress', + }) + + assert.equal(buyEthState.currentView.name, 'buyEth') + assert.equal(buyEthState.currentView.context, 'accountDetail') + assert.equal(buyEthState.identity.address, '0xAddress') + assert.equal(buyEthState.buyView.subview, 'Coinbase') + assert.equal(buyEthState.buyView.amount, '15.00') + assert.equal(buyEthState.buyView.buyAddress, '0xAddress') + assert.equal(buyEthState.buyView.formView.coinbase, true) + assert.equal(buyEthState.buyView.formView.shapeshift, false) + }) + + it('shows onboarding subview to buy eth', () => { + const state = reduceApp(metamaskState, { + type: actions.ONBOARDING_BUY_ETH_VIEW, + value: '0xAddress', + }) + + assert.equal(state.currentView.name, 'onboardingBuyEth') + assert.equal(state.currentView.context, 'accountDetail') + assert.equal(state.identity.address, '0xAddress') + }) + + it('shows coinbase subview', () => { + const appState = { + appState: { + buyView: { + buyAddress: '0xAddress', + amount: '12.00', + }, + }, + } + const oldState = {...metamaskState, ...appState} + const state = reduceApp(oldState, { + type: actions.COINBASE_SUBVIEW, + }) + + assert.equal(state.buyView.subview, 'Coinbase') + assert.equal(state.buyView.formView.coinbase, true) + assert.equal(state.buyView.buyAddress, '0xAddress') + assert.equal(state.buyView.amount, '12.00') + }) + + it('shows shapeshift subview', () => { + const appState = { + appState: { + buyView: { + buyAddress: '0xAddress', + amount: '12.00', + }, + }, + } + + const marketinfo = { + pair: 'BTC_ETH', + rate: 28.91191106, + minerFee: 0.0022, + limit: 0.76617432, + minimum: 0.00015323, + maxLimit: 0.76617432, + } + + const coinOptions = { + BTC: { + symbol: 'BTC', + name: 'Bitcoin', + image: 'https://shapeshift.io/images/coins/bitcoin.png', + imageSmall: 'https://shapeshift.io/images/coins-sm/bitcoin.png', + status: 'available', + minerFee: 0.00025, + }, + } + + const oldState = {...metamaskState, ...appState} + + const state = reduceApp(oldState, { + type: actions.SHAPESHIFT_SUBVIEW, + value: { + marketinfo, + coinOptions, + }, + }) + + assert.equal(state.buyView.subview, 'ShapeShift') + assert.equal(state.buyView.formView.shapeshift, true) + assert.deepEqual(state.buyView.formView.marketinfo, marketinfo) + assert.deepEqual(state.buyView.formView.coinOptions, coinOptions) + assert.equal(state.buyView.buyAddress, '0xAddress') + assert.equal(state.buyView.amount, '12.00') + }) + + it('updates pair', () => { + const coinOptions = { + BTC: { + symbol: 'BTC', + name: 'Bitcoin', + image: 'https://shapeshift.io/images/coins/bitcoin.png', + imageSmall: 'https://shapeshift.io/images/coins-sm/bitcoin.png', + status: 'available', + minerFee: 0.00025, + }, + } + + const appState = { + appState: { + buyView: { + buyAddress: '0xAddress', + amount: '12.00', + formView: { + coinOptions, + }, + }, + }, + } + + const marketinfo = { + pair: 'BTC_ETH', + rate: 28.91191106, + minerFee: 0.0022, + limit: 0.76617432, + minimum: 0.00015323, + maxLimit: 0.76617432, + } + + const oldState = {...metamaskState, ...appState} + + const state = reduceApp(oldState, { + type: actions.PAIR_UPDATE, + value: { + marketinfo, + }, + }) + + assert.equal(state.buyView.subview, 'ShapeShift') + assert.equal(state.buyView.formView.shapeshift, true) + assert.deepEqual(state.buyView.formView.marketinfo, marketinfo) + assert.deepEqual(state.buyView.formView.coinOptions, coinOptions) + assert.equal(state.buyView.buyAddress, '0xAddress') + assert.equal(state.buyView.amount, '12.00') + }) + + it('shows QR', () => { + const state = reduceApp(metamaskState, { + type: actions.SHOW_QR, + value: { + message: 'message', + data: 'data', + }, + }) + + assert.equal(state.qrRequested, true) + assert.equal(state.transForward, true) + assert.equal(state.Qr.message, 'message') + assert.equal(state.Qr.data, 'data') + }) + + it('shows qr view', () => { + const appState = { + appState: { + currentView: { + context: 'accounts', + }, + }, + } + + const oldState = {...metamaskState, ...appState} + const state = reduceApp(oldState, { + type: actions.SHOW_QR_VIEW, + value: { + message: 'message', + data: 'data', + }, + }) + + assert.equal(state.currentView.name, 'qr') + assert.equal(state.currentView.context, 'accounts') + assert.equal(state.transForward, true) + assert.equal(state.Qr.message, 'message') + assert.equal(state.Qr.data, 'data') + }) + + it('set mouse user state', () => { + const state = reduceApp(metamaskState, { + type: actions.SET_MOUSE_USER_STATE, + value: true, + }) + + assert.equal(state.isMouseUser, true) + }) + + it('sets gas loading', () => { + gasLoadingState = reduceApp(metamaskState, { + type: actions.GAS_LOADING_STARTED, + }) + + assert.equal(gasLoadingState.gasIsLoading, true) + }) + + it('unsets gas loading', () => { + const oldState = {...metamaskState, ...gasLoadingState} + const state = reduceApp(oldState, { + type: actions.GAS_LOADING_FINISHED, + }) + + assert.equal(state.gasIsLoading, false) + }) + + it('sets network nonce', () => { + const state = reduceApp(metamaskState, { + type: actions.SET_NETWORK_NONCE, + value: '33', + }) + + assert.equal(state.networkNonce, '33') + }) +}) diff --git a/test/unit/ui/app/reducers/metamask.spec.js b/test/unit/ui/app/reducers/metamask.spec.js new file mode 100644 index 000000000..b0c57304b --- /dev/null +++ b/test/unit/ui/app/reducers/metamask.spec.js @@ -0,0 +1,504 @@ +import assert from 'assert' +import reduceMetamask from '../../../../../ui/app/reducers/metamask' +import * as actions from '../../../../../ui/app/actions' + +describe('MetaMask Reducers', () => { + let initState, + noticesState, + unlockMetaMaskState, + seedWordsState, + showNewVaultSeedState, + sendState, + pendingTokensState + + it('init state', () => { + initState = reduceMetamask({metamask:{}}, {}) + assert(initState) + }) + + it('sets revealing seed to true and adds seed words to new state', () => { + seedWordsState = reduceMetamask({}, { + type: actions.SHOW_NEW_VAULT_SEED, + value: 'test seed words', + }) + + assert.equal(seedWordsState.seedWords, 'test seed words') + assert.equal(seedWordsState.isRevealingSeedWords, true) + }) + + it('shows account page', () => { + const state = reduceMetamask(seedWordsState, { + type: actions.SHOW_ACCOUNTS_PAGE, + }) + + assert.equal(seedWordsState.seedWords, 'test seed words') + assert.equal(state.seedWords, undefined) + assert.equal(state.isRevealingSeedWords, false) + }) + + it('shows notice', () => { + const notice = { + id: 0, + read: false, + date: 'Date', + title: 'Title', + body: 'Body', + } + noticesState = reduceMetamask({}, { + type: actions.SHOW_NOTICE, + value: notice, + }) + + assert.equal(noticesState.noActiveNotices, false) + assert.equal(noticesState.nextUnreadNotice, notice) + }) + + it('clears notice', () => { + const state = reduceMetamask(noticesState, { + type: actions.CLEAR_NOTICES, + }) + + assert.equal(state.noActiveNotices, true) + }) + + it('unlocks MetaMask', () => { + unlockMetaMaskState = reduceMetamask({}, { + type: actions.UNLOCK_METAMASK, + value: 'test address', + }) + + assert.equal(unlockMetaMaskState.isUnlocked, true) + assert.equal(unlockMetaMaskState.isInitialized, true) + assert.equal(unlockMetaMaskState.selectedAddress, 'test address') + }) + + it('locks MetaMask', () => { + const lockMetaMask = reduceMetamask(unlockMetaMaskState, { + type: actions.LOCK_METAMASK, + }) + + assert.equal(lockMetaMask.isUnlocked, false) + }) + + it('sets frequent rpc list', () => { + const state = reduceMetamask({}, { + type: actions.SET_RPC_LIST, + value: 'https://custom.rpc', + }) + + assert.equal(state.frequentRpcList, 'https://custom.rpc') + }) + + it('sets rpc target', () => { + const state = reduceMetamask({}, { + type: actions.SET_RPC_TARGET, + value: 'https://custom.rpc', + }) + + assert.equal(state.provider.rpcTarget, 'https://custom.rpc') + }) + + it('sets provider type', () => { + const state = reduceMetamask({}, { + type: actions.SET_PROVIDER_TYPE, + value: 'provider type', + }) + + assert.equal(state.provider.type, 'provider type') + }) + + describe('CompletedTx', () => { + const oldState = { + metamask: { + unapprovedTxs: { + 1: { + id: 1, + time: 1538495996507, + status: 'unapproved', + metamaskNetworkId: 4, + loadingDefaults: false, + txParams: { + from: '0xAddress', + to: '0xAddress2', + value: '0x16345785d8a0000', + gas: '0x5208', + gasPrice: '0x3b9aca00', + }, + type: 'standard', + }, + 2: { + test: 'Should persist', + }, + }, + unapprovedMsgs: { + 1: { + id: 2, + msgParams: { + from: '0xAddress', + data: '0xData', + origin: 'test origin', + }, + time: 1538498521717, + status: 'unapproved', + type: 'eth_sign', + }, + 2: { + test: 'Should Persist', + }, + }, + }, + } + + it('removes tx from new state if completed in action.', () => { + + const state = reduceMetamask(oldState, { + type: actions.COMPLETED_TX, + id: 1, + }) + + assert.equal(Object.keys(state.unapprovedTxs).length, 1) + assert.equal(state.unapprovedTxs[2].test, 'Should persist') + }) + + it('removes msg from new state if completed id in action', () => { + const state = reduceMetamask(oldState, { + type: actions.COMPLETED_TX, + id: 1, + }) + + assert.equal(Object.keys(state.unapprovedMsgs).length, 1) + assert.equal(state.unapprovedTxs[2].test, 'Should persist') + }) + }) + + it('shows new vault seed words and sets isRevealingSeedWords to true', () => { + showNewVaultSeedState = reduceMetamask({}, { + type: actions.SHOW_NEW_VAULT_SEED, + value: 'test seed words', + }) + + assert.equal(showNewVaultSeedState.isRevealingSeedWords, true) + assert.equal(showNewVaultSeedState.seedWords, 'test seed words') + }) + + it('shows account detail', () => { + const state = reduceMetamask(showNewVaultSeedState, { + type: actions.SHOW_ACCOUNT_DETAIL, + value: 'test address', + }) + + assert.equal(state.isUnlocked, true) + assert.equal(state.isInitialized, true) + assert.equal(state.selectedAddress, 'test address') + }) + + it('sets select ', () => { + const state = reduceMetamask({}, { + type: actions.SET_SELECTED_TOKEN, + value: 'test token', + }) + + assert.equal(state.selectedTokenAddress, 'test token') + }) + + it('sets account label', () => { + const state = reduceMetamask({}, { + type: actions.SET_ACCOUNT_LABEL, + value: { + account: 'test account', + label: 'test label', + }, + }) + + assert.deepEqual(state.identities, { 'test account': { name: 'test label' } }) + }) + + it('sets current fiat', () => { + const value = { + currentCurrency: 'yen', + conversionRate: 3.14, + conversionDate: new Date(2018, 9), + } + + const state = reduceMetamask({}, { + type: actions.SET_CURRENT_FIAT, + value, + }) + + assert.equal(state.currentCurrency, value.currentCurrency) + assert.equal(state.conversionRate, value.conversionRate) + assert.equal(state.conversionDate, value.conversionDate) + }) + + it('updates tokens', () => { + const newTokens = { + 'address': '0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4', + 'decimals': 18, + 'symbol': 'META', + } + + const state = reduceMetamask({}, { + type: actions.UPDATE_TOKENS, + newTokens, + }) + + assert.deepEqual(state.tokens, newTokens) + }) + + it('updates send gas limit', () => { + + const state = reduceMetamask({}, { + type: actions.UPDATE_GAS_LIMIT, + value: '0xGasLimit', + }) + + assert.equal(state.send.gasLimit, '0xGasLimit') + }) + + it('updates send gas price', () => { + const state = reduceMetamask({}, { + type: actions.UPDATE_GAS_PRICE, + value: '0xGasPrice', + }) + + assert.equal(state.send.gasPrice, '0xGasPrice') + }) + + it('toggles account menu ', () => { + const state = reduceMetamask({}, { + type: actions.TOGGLE_ACCOUNT_MENU, + }) + + assert.equal(state.isAccountMenuOpen, true) + }) + + it('updates gas total', () => { + const state = reduceMetamask({}, { + type: actions.UPDATE_GAS_TOTAL, + value: '0xGasTotal', + }) + + assert.equal(state.send.gasTotal, '0xGasTotal') + }) + + it('updates send token balance', () => { + const state = reduceMetamask({}, { + type: actions.UPDATE_SEND_TOKEN_BALANCE, + value: '0xTokenBalance', + }) + + assert.equal(state.send.tokenBalance, '0xTokenBalance') + }) + + it('updates data', () => { + const state = reduceMetamask({}, { + type: actions.UPDATE_SEND_HEX_DATA, + value: '0xData', + }) + + assert.equal(state.send.data, '0xData') + }) + + it('updates send to', () => { + const state = reduceMetamask({}, { + type: actions.UPDATE_SEND_TO, + value: { + to: '0xAddress', + nickname: 'nickname', + }, + }) + + assert.equal(state.send.to, '0xAddress') + assert.equal(state.send.toNickname, 'nickname') + }) + + it('update send from', () => { + const state = reduceMetamask({}, { + type: actions.UPDATE_SEND_FROM, + value: '0xAddress', + }) + + assert.equal(state.send.from, '0xAddress') + }) + + it('update send amount', () => { + const state = reduceMetamask({}, { + type: actions.UPDATE_SEND_AMOUNT, + value: '0xAmount', + }) + + assert.equal(state.send.amount, '0xAmount') + }) + + it('update send memo', () => { + const state = reduceMetamask({}, { + type: actions.UPDATE_SEND_MEMO, + value: '0xMemo', + }) + + assert.equal(state.send.memo, '0xMemo') + }) + + it('updates max mode', () => { + const state = reduceMetamask({}, { + type: actions.UPDATE_MAX_MODE, + value: true, + }) + + assert.equal(state.send.maxModeOn, true) + }) + + it('update send', () => { + const value = { + gasLimit: '0xGasLimit', + gasPrice: '0xGasPrice', + gasTotal: '0xGasTotal', + tokenBalance: '0xBalance', + from: '0xAddress', + to: '0xAddress', + toNickname: '', + maxModeOn: false, + amount: '0xAmount', + memo: '0xMemo', + errors: {}, + editingTransactionId: 22, + forceGasMin: '0xGas', + } + + sendState = reduceMetamask({}, { + type: actions.UPDATE_SEND, + value, + }) + + assert.deepEqual(sendState.send, value) + }) + + it('clears send', () => { + const state = reduceMetamask(sendState, { + type: actions.CLEAR_SEND, + }) + assert.deepEqual(state.send, initState.send) + }) + + it('updates value of tx by id', () => { + const oldState = { + metamask: { + selectedAddressTxList: [ + { + id: 1, + txParams: 'foo', + }, + ], + }, + } + + const state = reduceMetamask(oldState, { + type: actions.UPDATE_TRANSACTION_PARAMS, + id: 1, + value: 'bar', + }) + + assert.equal(state.selectedAddressTxList[0].txParams, 'bar') + }) + + it('updates pair for shapeshift', () => { + const state = reduceMetamask({}, { + type: actions.PAIR_UPDATE, + value: { + marketinfo: { + pair: 'test pair', + foo: 'bar', + }, + }, + }) + assert.equal(state.tokenExchangeRates['test pair'].pair, 'test pair') + }) + + it('upates pair and coin options for shapeshift subview', () => { + const state = reduceMetamask({}, { + type: actions.SHAPESHIFT_SUBVIEW, + value: { + marketinfo: { + pair: 'test pair', + }, + coinOptions: { + foo: 'bar', + }, + }, + }) + + assert.equal(state.coinOptions.foo, 'bar') + assert.equal(state.tokenExchangeRates['test pair'].pair, 'test pair') + }) + + it('sets blockies', () => { + const state = reduceMetamask({}, { + type: actions.SET_USE_BLOCKIE, + value: true, + }) + + assert.equal(state.useBlockie, true) + }) + + it('updates feature flag', () => { + const state = reduceMetamask({}, { + type: actions.UPDATE_FEATURE_FLAGS, + value: { + betaUI: true, + skipAnnounceBetaUI: true, + }, + }) + + assert.equal(state.featureFlags.betaUI, true) + assert.equal(state.featureFlags.skipAnnounceBetaUI, true) + }) + + it('updates network endpoint type', () => { + const state = reduceMetamask({}, { + type: actions.UPDATE_NETWORK_ENDPOINT_TYPE, + value: 'endpoint', + }) + + assert.equal(state.networkEndpointType, 'endpoint') + }) + + it('close welcome screen', () => { + const state = reduceMetamask({}, { + type: actions.CLOSE_WELCOME_SCREEN, + }) + + assert.equal(state.welcomeScreenSeen, true) + }) + + it('sets current locale', () => { + const state = reduceMetamask({}, { + type: actions.SET_CURRENT_LOCALE, + value: 'ge', + }) + + assert.equal(state.currentLocale, 'ge') + }) + + it('sets pending tokens ', () => { + const payload = { + 'address': '0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4', + 'decimals': 18, + 'symbol': 'META', + } + + pendingTokensState = reduceMetamask({}, { + type: actions.SET_PENDING_TOKENS, + payload, + }) + + assert.deepEqual(pendingTokensState.pendingTokens, payload) + }) + + it('clears pending tokens', () => { + const state = reduceMetamask(pendingTokensState, { + type: actions.CLEAR_PENDING_TOKENS, + }) + + assert.deepEqual(state.pendingTokens, {}) + }) +}) -- cgit From 611a61b7bc576bb83a6b7851f26d00488fe060f2 Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 31 Oct 2018 05:45:27 -0700 Subject: Isolate variables to individual tests --- test/unit/ui/app/reducers/app.spec.js | 117 +++++++++++++++++++--------------- 1 file changed, 64 insertions(+), 53 deletions(-) (limited to 'test/unit/ui/app/reducers') diff --git a/test/unit/ui/app/reducers/app.spec.js b/test/unit/ui/app/reducers/app.spec.js index 7cd17a25a..bee4963e5 100644 --- a/test/unit/ui/app/reducers/app.spec.js +++ b/test/unit/ui/app/reducers/app.spec.js @@ -3,19 +3,6 @@ import reduceApp from '../../../../../ui/app/reducers/app' import * as actions from '../../../../../ui/app/actions' describe('App State', () => { - let initState, - dropdownOpen, - openSidebar, - alertOpen, - opensModal, - transitionForwardState, - errorState, - loadingState, - subLoadingIndicatorState, - displayWarningState, - requestAccountExportState, - buyEthState, - gasLoadingState const metamaskState = { metamask: { @@ -30,21 +17,22 @@ describe('App State', () => { } it('App init state', () => { - initState = reduceApp(metamaskState, {}) + const initState = reduceApp(metamaskState, {}) assert(initState) }) it('sets networkd dropdown to true', () => { - dropdownOpen = reduceApp(metamaskState, { + const state = reduceApp(metamaskState, { type: actions.NETWORK_DROPDOWN_OPEN, }) - assert.equal(dropdownOpen.networkDropdownOpen, true) + assert.equal(state.networkDropdownOpen, true) }) it('sets networkd dropdown to false', () => { - const state = {...metamaskState, ...dropdownOpen} + const dropdown = { networkDropdowopen: true } + const state = {...metamaskState, ...dropdown} const newState = reduceApp(state, { type: actions.NETWORK_DROPDOWN_CLOSE, }) @@ -58,15 +46,16 @@ describe('App State', () => { 'type': 'wallet-view', 'isOpen': true, } - openSidebar = reduceApp(metamaskState, { + const state = reduceApp(metamaskState, { type: actions.SIDEBAR_OPEN, value, }) - assert.deepEqual(openSidebar.sidebar, value) + assert.deepEqual(state.sidebar, value) }) it('closes sidebar', () => { + const openSidebar = { sidebar: { isOpen: true }} const state = {...metamaskState, ...openSidebar} const newState = reduceApp(state, { @@ -77,17 +66,18 @@ describe('App State', () => { }) it('opens alert', () => { - alertOpen = reduceApp(metamaskState, { + const state = reduceApp(metamaskState, { type: actions.ALERT_OPEN, value: 'test message', }) - assert.equal(alertOpen.alertOpen, true) - assert.equal(alertOpen.alertMessage, 'test message') + assert.equal(state.alertOpen, true) + assert.equal(state.alertMessage, 'test message') }) it('closes alert', () => { - const state = {...metamaskState, ...alertOpen} + const alert = { alertOpen: true, alertMessage: 'test message' } + const state = {...metamaskState, ...alert} const newState = reduceApp(state, { type: actions.ALERT_CLOSE, }) @@ -106,18 +96,27 @@ describe('App State', () => { }) it('opens modal', () => { - opensModal = reduceApp(metamaskState, { + const state = reduceApp(metamaskState, { type: actions.MODAL_OPEN, payload: { name: 'test', }, }) - assert.equal(opensModal.modal.open, true) - assert.equal(opensModal.modal.modalState.name, 'test') + assert.equal(state.modal.open, true) + assert.equal(state.modal.modalState.name, 'test') }) it('closes modal, but moves open modal state to previous modal state', () => { + const opensModal = { + modal: { + open: true, + modalState: { + name: 'test', + }, + }, + } + const state = { ...metamaskState, appState: { ...opensModal } } const newState = reduceApp(state, { type: actions.MODAL_CLOSE, @@ -129,14 +128,16 @@ describe('App State', () => { }) it('tansitions forwards', () => { - transitionForwardState = reduceApp(metamaskState, { + const state = reduceApp(metamaskState, { type: actions.TRANSITION_FORWARD, }) - assert.equal(transitionForwardState.transForward, true) + assert.equal(state.transForward, true) }) it('transition backwards', () => { + const transitionForwardState = { transitionForward: true } + const state = { ...metamaskState, ...transitionForwardState } const newState = reduceApp(state, { type: actions.TRANSITION_BACKWARD, @@ -644,15 +645,16 @@ describe('App State', () => { }) it('sets default warning when unlock fails', () => { - errorState = reduceApp(metamaskState, { + const state = reduceApp(metamaskState, { type: actions.UNLOCK_FAILED, value: 'errors', }) - assert.equal(errorState.warning, 'errors') + assert.equal(state.warning, 'errors') }) it('sets warning to empty string when unlock succeeds', () => { + const errorState = { warning: 'errors' } const oldState = {...metamaskState, ...errorState} const state = reduceApp(oldState, { type: actions.UNLOCK_SUCCEEDED, @@ -678,16 +680,17 @@ describe('App State', () => { }) it('shows loading message', () => { - loadingState = reduceApp(metamaskState, { + const state = reduceApp(metamaskState, { type: actions.SHOW_LOADING, value: 'loading', }) - assert.equal(loadingState.isLoading, true) - assert.equal(loadingState.loadingMessage, 'loading') + assert.equal(state.isLoading, true) + assert.equal(state.loadingMessage, 'loading') }) it('hides loading message', () => { + const loadingState = { isLoading: true} const oldState = {...metamaskState, ...loadingState} const state = reduceApp(oldState, { @@ -698,11 +701,11 @@ describe('App State', () => { }) it('shows sub loading indicator', () => { - subLoadingIndicatorState = reduceApp(metamaskState, { + const state = reduceApp(metamaskState, { type: actions.SHOW_SUB_LOADING_INDICATION, }) - assert.equal(subLoadingIndicatorState.isSubLoading, true) + assert.equal(state.isSubLoading, true) }) it('hides sub loading indicator', () => { @@ -715,16 +718,17 @@ describe('App State', () => { }) it('displays warning', () => { - displayWarningState = reduceApp(metamaskState, { + const state = reduceApp(metamaskState, { type: actions.DISPLAY_WARNING, value: 'warning', }) - assert.equal(displayWarningState.isLoading, false) - assert.equal(displayWarningState.warning, 'warning') + assert.equal(state.isLoading, false) + assert.equal(state.warning, 'warning') }) it('hides warning', () => { + const displayWarningState = { warning: 'warning'} const oldState = {...metamaskState, ...displayWarningState} const state = reduceApp(oldState, { type: actions.HIDE_WARNING, @@ -734,16 +738,22 @@ describe('App State', () => { }) it('request to display account export', () => { - requestAccountExportState = reduceApp(metamaskState, { + const state = reduceApp(metamaskState, { type: actions.REQUEST_ACCOUNT_EXPORT, }) - assert.equal(requestAccountExportState.transForward, true) - assert.equal(requestAccountExportState.accountDetail.subview, 'export') - assert.equal(requestAccountExportState.accountDetail.accountExport, 'requested') + assert.equal(state.transForward, true) + assert.equal(state.accountDetail.subview, 'export') + assert.equal(state.accountDetail.accountExport, 'requested') }) it('completes account export', () => { + const requestAccountExportState = { + accountDetail: { + subview: 'something', + accountExport: 'progress', + }, + } const oldState = {...metamaskState, ...requestAccountExportState} const state = reduceApp(oldState, { type: actions.EXPORT_ACCOUNT, @@ -766,19 +776,19 @@ describe('App State', () => { it('shows buy eth view', () => { - buyEthState = reduceApp(metamaskState, { + const state = reduceApp(metamaskState, { type: actions.BUY_ETH_VIEW, value: '0xAddress', }) - assert.equal(buyEthState.currentView.name, 'buyEth') - assert.equal(buyEthState.currentView.context, 'accountDetail') - assert.equal(buyEthState.identity.address, '0xAddress') - assert.equal(buyEthState.buyView.subview, 'Coinbase') - assert.equal(buyEthState.buyView.amount, '15.00') - assert.equal(buyEthState.buyView.buyAddress, '0xAddress') - assert.equal(buyEthState.buyView.formView.coinbase, true) - assert.equal(buyEthState.buyView.formView.shapeshift, false) + assert.equal(state.currentView.name, 'buyEth') + assert.equal(state.currentView.context, 'accountDetail') + assert.equal(state.identity.address, '0xAddress') + assert.equal(state.buyView.subview, 'Coinbase') + assert.equal(state.buyView.amount, '15.00') + assert.equal(state.buyView.buyAddress, '0xAddress') + assert.equal(state.buyView.formView.coinbase, true) + assert.equal(state.buyView.formView.shapeshift, false) }) it('shows onboarding subview to buy eth', () => { @@ -960,14 +970,15 @@ describe('App State', () => { }) it('sets gas loading', () => { - gasLoadingState = reduceApp(metamaskState, { + const state = reduceApp(metamaskState, { type: actions.GAS_LOADING_STARTED, }) - assert.equal(gasLoadingState.gasIsLoading, true) + assert.equal(state.gasIsLoading, true) }) it('unsets gas loading', () => { + const gasLoadingState = { gasIsLoading: true } const oldState = {...metamaskState, ...gasLoadingState} const state = reduceApp(oldState, { type: actions.GAS_LOADING_FINISHED, -- cgit From 7607794851ff534faf51fce6ed37d24d33818f18 Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 31 Oct 2018 07:07:25 -0700 Subject: Readable tests, and set nextUnreadNotice to null when CLEAR_NOTICES is dispatched --- test/unit/ui/app/reducers/metamask.spec.js | 118 +++++++++++++++++++++++------ 1 file changed, 95 insertions(+), 23 deletions(-) (limited to 'test/unit/ui/app/reducers') diff --git a/test/unit/ui/app/reducers/metamask.spec.js b/test/unit/ui/app/reducers/metamask.spec.js index b0c57304b..6d43a97d4 100644 --- a/test/unit/ui/app/reducers/metamask.spec.js +++ b/test/unit/ui/app/reducers/metamask.spec.js @@ -2,22 +2,15 @@ import assert from 'assert' import reduceMetamask from '../../../../../ui/app/reducers/metamask' import * as actions from '../../../../../ui/app/actions' -describe('MetaMask Reducers', () => { - let initState, - noticesState, - unlockMetaMaskState, - seedWordsState, - showNewVaultSeedState, - sendState, - pendingTokensState +describe.only('MetaMask Reducers', () => { it('init state', () => { - initState = reduceMetamask({metamask:{}}, {}) + const initState = reduceMetamask({metamask:{}}, {}) assert(initState) }) it('sets revealing seed to true and adds seed words to new state', () => { - seedWordsState = reduceMetamask({}, { + const seedWordsState = reduceMetamask({}, { type: actions.SHOW_NEW_VAULT_SEED, value: 'test seed words', }) @@ -27,11 +20,17 @@ describe('MetaMask Reducers', () => { }) it('shows account page', () => { + const seedWordsState = { + metamask: { + seedwords: 'test seed words', + isRevealing: true, + }, + } + const state = reduceMetamask(seedWordsState, { type: actions.SHOW_ACCOUNTS_PAGE, }) - assert.equal(seedWordsState.seedWords, 'test seed words') assert.equal(state.seedWords, undefined) assert.equal(state.isRevealingSeedWords, false) }) @@ -44,35 +43,60 @@ describe('MetaMask Reducers', () => { title: 'Title', body: 'Body', } - noticesState = reduceMetamask({}, { + + const state = reduceMetamask({}, { type: actions.SHOW_NOTICE, value: notice, }) - assert.equal(noticesState.noActiveNotices, false) - assert.equal(noticesState.nextUnreadNotice, notice) + assert.equal(state.noActiveNotices, false) + assert.equal(state.nextUnreadNotice, notice) }) it('clears notice', () => { + + const notice = { + id: 0, + read: false, + date: 'Date', + title: 'Title', + body: 'Body', + } + + const noticesState = { + metamask: { + noActiveNotices: false, + nextUnreadNotice: notice, + }, + } + const state = reduceMetamask(noticesState, { type: actions.CLEAR_NOTICES, }) assert.equal(state.noActiveNotices, true) + assert.equal(state.nextUnreadNotice, null) }) it('unlocks MetaMask', () => { - unlockMetaMaskState = reduceMetamask({}, { + const state = reduceMetamask({}, { type: actions.UNLOCK_METAMASK, value: 'test address', }) - assert.equal(unlockMetaMaskState.isUnlocked, true) - assert.equal(unlockMetaMaskState.isInitialized, true) - assert.equal(unlockMetaMaskState.selectedAddress, 'test address') + assert.equal(state.isUnlocked, true) + assert.equal(state.isInitialized, true) + assert.equal(state.selectedAddress, 'test address') }) it('locks MetaMask', () => { + const unlockMetaMaskState = { + metamask: { + isUnlocked: true, + isInitialzed: false, + selectedAddress: 'test address', + }, + } const lockMetaMask = reduceMetamask(unlockMetaMaskState, { type: actions.LOCK_METAMASK, }) @@ -172,7 +196,7 @@ describe('MetaMask Reducers', () => { }) it('shows new vault seed words and sets isRevealingSeedWords to true', () => { - showNewVaultSeedState = reduceMetamask({}, { + const showNewVaultSeedState = reduceMetamask({}, { type: actions.SHOW_NEW_VAULT_SEED, value: 'test seed words', }) @@ -182,7 +206,8 @@ describe('MetaMask Reducers', () => { }) it('shows account detail', () => { - const state = reduceMetamask(showNewVaultSeedState, { + + const state = reduceMetamask({}, { type: actions.SHOW_ACCOUNT_DETAIL, value: 'test address', }) @@ -365,7 +390,7 @@ describe('MetaMask Reducers', () => { forceGasMin: '0xGas', } - sendState = reduceMetamask({}, { + const sendState = reduceMetamask({}, { type: actions.UPDATE_SEND, value, }) @@ -374,10 +399,47 @@ describe('MetaMask Reducers', () => { }) it('clears send', () => { + const initStateSend = { + send: + { gasLimit: null, + gasPrice: null, + gasTotal: null, + tokenBalance: null, + from: '', + to: '', + amount: '0x0', + memo: '', + errors: {}, + maxModeOn: false, + editingTransactionId: null, + forceGasMin: null, + toNickname: '' }, + } + + const sendState = { + send: { + gasLimit: '0xGasLimit', + gasPrice: '0xGasPrice', + gasTotal: '0xGasTotal', + tokenBalance: '0xBalance', + from: '0xAddress', + to: '0xAddress', + toNickname: '', + maxModeOn: false, + amount: '0xAmount', + memo: '0xMemo', + errors: {}, + editingTransactionId: 22, + forceGasMin: '0xGas', + }, + } + + const state = reduceMetamask(sendState, { type: actions.CLEAR_SEND, }) - assert.deepEqual(state.send, initState.send) + + assert.deepEqual(state.send, initStateSend.send) }) it('updates value of tx by id', () => { @@ -486,7 +548,7 @@ describe('MetaMask Reducers', () => { 'symbol': 'META', } - pendingTokensState = reduceMetamask({}, { + const pendingTokensState = reduceMetamask({}, { type: actions.SET_PENDING_TOKENS, payload, }) @@ -495,6 +557,16 @@ describe('MetaMask Reducers', () => { }) it('clears pending tokens', () => { + const payload = { + 'address': '0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4', + 'decimals': 18, + 'symbol': 'META', + } + + const pendingTokensState = { + pendingTokens: payload, + } + const state = reduceMetamask(pendingTokensState, { type: actions.CLEAR_PENDING_TOKENS, }) -- cgit From f2fea8043edca84f1aa9ebc13ecea55c9919070d Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 31 Oct 2018 07:09:02 -0700 Subject: Remove .only --- test/unit/ui/app/reducers/metamask.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/unit/ui/app/reducers') diff --git a/test/unit/ui/app/reducers/metamask.spec.js b/test/unit/ui/app/reducers/metamask.spec.js index 6d43a97d4..e1a50eef2 100644 --- a/test/unit/ui/app/reducers/metamask.spec.js +++ b/test/unit/ui/app/reducers/metamask.spec.js @@ -2,7 +2,7 @@ import assert from 'assert' import reduceMetamask from '../../../../../ui/app/reducers/metamask' import * as actions from '../../../../../ui/app/actions' -describe.only('MetaMask Reducers', () => { +describe('MetaMask Reducers', () => { it('init state', () => { const initState = reduceMetamask({metamask:{}}, {}) -- cgit