1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/ui/ducks/app/app.test.js

341 lines
8.8 KiB
JavaScript
Raw Normal View History

import * as actionConstants from '../../store/actionConstants';
import { DEVICE_NAMES } from '../../../shared/constants/hardware-wallets';
import reduceApp from './app';
const actions = actionConstants;
2018-10-10 10:12:46 +02:00
describe('App State', () => {
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
},
},
};
2018-10-10 10:12:46 +02:00
it('app init state', () => {
const initState = reduceApp(metamaskState, {});
2018-10-10 10:12:46 +02:00
expect.anything(initState);
});
2018-10-10 10:12:46 +02:00
it('sets networkDropdownOpen dropdown to true', () => {
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-10 10:12:46 +02:00
expect(state.networkDropdownOpen).toStrictEqual(true);
});
2018-10-10 10:12:46 +02:00
it('sets networkDropdownOpen dropdown to false', () => {
const dropdown = { networkDropdowopen: true };
const state = { ...metamaskState, ...dropdown };
2018-10-10 10:12:46 +02:00
const newState = reduceApp(state, {
type: actions.NETWORK_DROPDOWN_CLOSE,
});
2018-10-10 10:12:46 +02:00
expect(newState.networkDropdownOpen).toStrictEqual(false);
});
2018-10-10 10:12:46 +02:00
it('opens alert', () => {
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-10 10:12:46 +02:00
expect(state.alertOpen).toStrictEqual(true);
expect(state.alertMessage).toStrictEqual('test message');
});
2018-10-10 10:12:46 +02:00
it('closes alert', () => {
const alert = { alertOpen: true, alertMessage: 'test message' };
const state = { ...metamaskState, ...alert };
2018-10-10 10:12:46 +02:00
const newState = reduceApp(state, {
type: actions.ALERT_CLOSE,
});
2018-10-10 10:12:46 +02:00
expect(newState.alertOpen).toStrictEqual(false);
expect(newState.alertMessage).toBeNull();
});
2018-10-10 10:12:46 +02:00
it('detects qr code data', () => {
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.QR_CODE_DETECTED,
value: 'qr data',
});
2018-10-10 10:12:46 +02:00
expect(state.qrCodeData).toStrictEqual('qr data');
});
2018-10-10 10:12:46 +02:00
it('opens modal', () => {
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-10 10:12:46 +02:00
expect(state.modal.open).toStrictEqual(true);
expect(state.modal.modalState.name).toStrictEqual('test');
});
2018-10-10 10:12:46 +02:00
it('closes modal, but moves open modal state to previous modal state', () => {
2018-10-31 13:45:27 +01:00
const opensModal = {
modal: {
open: true,
modalState: {
name: 'test',
},
},
};
2018-10-31 13:45:27 +01:00
const state = { ...metamaskState, appState: { ...opensModal } };
2018-10-10 10:12:46 +02:00
const newState = reduceApp(state, {
type: actions.MODAL_CLOSE,
});
2018-10-10 10:12:46 +02:00
expect(newState.modal.open).toStrictEqual(false);
expect(newState.modal.modalState.name).toBeNull();
});
2018-10-10 10:12:46 +02:00
it('shows send token page', () => {
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.SHOW_SEND_TOKEN_PAGE,
});
2018-10-10 10:12:46 +02:00
expect(state.warning).toBeNull();
});
2018-10-10 10:12:46 +02:00
it('locks Metamask', () => {
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.LOCK_METAMASK,
});
2018-10-10 10:12:46 +02:00
expect(state.warning).toBeNull();
});
2018-10-10 10:12:46 +02:00
it('goes home', () => {
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.GO_HOME,
});
2018-10-10 10:12:46 +02:00
expect(state.accountDetail.subview).toStrictEqual('transactions');
expect(state.accountDetail.accountExport).toStrictEqual('none');
expect(state.accountDetail.privateKey).toStrictEqual('');
expect(state.warning).toBeNull();
});
2018-10-10 10:12:46 +02:00
it('shows account detail', () => {
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.SHOW_ACCOUNT_DETAIL,
value: 'context address',
});
expect(state.forgottenPassword).toBeNull(); // default
expect(state.accountDetail.subview).toStrictEqual('transactions'); // default
expect(state.accountDetail.accountExport).toStrictEqual('none'); // default
expect(state.accountDetail.privateKey).toStrictEqual(''); // default
});
2018-10-10 10:12:46 +02:00
it('clears account details', () => {
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,
});
expect(newState.accountDetail).toStrictEqual({});
});
it('shoes account page', () => {
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.SHOW_ACCOUNTS_PAGE,
});
2018-10-10 10:12:46 +02:00
expect(state.isLoading).toStrictEqual(false);
expect(state.warning).toBeNull();
expect(state.scrollToBottom).toStrictEqual(false);
expect(state.forgottenPassword).toStrictEqual(false);
});
2018-10-10 10:12:46 +02:00
it('shows confirm tx page', () => {
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,
});
2018-10-10 10:12:46 +02:00
expect(state.txId).toStrictEqual(2);
expect(state.warning).toBeNull();
expect(state.isLoading).toStrictEqual(false);
});
2018-10-10 10:12:46 +02:00
it('completes tx continues to show pending txs current view context', () => {
2018-10-10 10:12:46 +02:00
const txs = {
unapprovedTxs: {
1: {
id: 1,
},
2: {
id: 2,
},
},
};
2018-10-10 10:12:46 +02:00
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
expect(state.txId).toBeNull();
expect(state.warning).toBeNull();
});
2018-10-10 10:12:46 +02:00
it('returns to account detail page when no unconf actions completed tx', () => {
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
expect(state.warning).toBeNull();
expect(state.accountDetail.subview).toStrictEqual('transactions');
});
2018-10-10 10:12:46 +02:00
it('sets default warning when unlock fails', () => {
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.UNLOCK_FAILED,
});
2018-10-10 10:12:46 +02:00
expect(state.warning).toStrictEqual('Incorrect password. Try again.');
});
2018-10-10 10:12:46 +02:00
it('sets errors when unlock fails', () => {
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-10 10:12:46 +02:00
expect(state.warning).toStrictEqual('errors');
});
2018-10-10 10:12:46 +02:00
it('sets warning to empty string when unlock succeeds', () => {
const errorState = { warning: 'errors' };
const oldState = { ...metamaskState, ...errorState };
2018-10-10 10:12:46 +02:00
const state = reduceApp(oldState, {
type: actions.UNLOCK_SUCCEEDED,
});
2018-10-10 10:12:46 +02:00
expect(state.warning).toStrictEqual('');
});
2018-10-10 10:12:46 +02:00
it('sets hardware wallet default hd path', () => {
2018-10-10 10:12:46 +02:00
const hdPaths = {
trezor: "m/44'/60'/0'/0",
ledger: "m/44'/60'/0'",
lattice: "m/44'/60'/0'/0",
};
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.SET_HARDWARE_WALLET_DEFAULT_HD_PATH,
value: {
device: DEVICE_NAMES.LEDGER,
2018-10-10 10:12:46 +02:00
path: "m/44'/60'/0'",
},
});
2018-10-10 10:12:46 +02:00
expect(state.defaultHdPaths).toStrictEqual(hdPaths);
});
2018-10-10 10:12:46 +02:00
it('shows loading message', () => {
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-10 10:12:46 +02:00
expect(state.isLoading).toStrictEqual(true);
expect(state.loadingMessage).toStrictEqual('loading');
});
2018-10-10 10:12:46 +02:00
it('hides loading message', () => {
const loadingState = { isLoading: true };
const oldState = { ...metamaskState, ...loadingState };
2018-10-10 10:12:46 +02:00
const state = reduceApp(oldState, {
type: actions.HIDE_LOADING,
});
2018-10-10 10:12:46 +02:00
expect(state.isLoading).toStrictEqual(false);
});
2018-10-10 10:12:46 +02:00
it('displays warning', () => {
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-10 10:12:46 +02:00
expect(state.isLoading).toStrictEqual(false);
expect(state.warning).toStrictEqual('warning');
});
2018-10-10 10:12:46 +02:00
it('hides warning', () => {
const displayWarningState = { warning: 'warning' };
const oldState = { ...metamaskState, ...displayWarningState };
2018-10-10 10:12:46 +02:00
const state = reduceApp(oldState, {
type: actions.HIDE_WARNING,
});
2018-10-10 10:12:46 +02:00
expect(state.warning).toBeUndefined();
});
2018-10-10 10:12:46 +02:00
it('shows private key', () => {
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.SHOW_PRIVATE_KEY,
value: 'private key',
});
2018-10-10 10:12:46 +02:00
expect(state.accountDetail.subview).toStrictEqual('export');
expect(state.accountDetail.accountExport).toStrictEqual('completed');
expect(state.accountDetail.privateKey).toStrictEqual('private key');
});
2018-10-10 10:12:46 +02:00
it('set mouse user state', () => {
2018-10-10 10:12:46 +02:00
const state = reduceApp(metamaskState, {
type: actions.SET_MOUSE_USER_STATE,
value: true,
});
2018-10-10 10:12:46 +02:00
expect(state.isMouseUser).toStrictEqual(true);
});
it('smart transactions - SET_SMART_TRANSACTIONS_ERROR', () => {
const state = reduceApp(metamaskState, {
type: actions.SET_SMART_TRANSACTIONS_ERROR,
payload: 'Server Side Error',
});
expect(state.smartTransactionsError).toStrictEqual('Server Side Error');
});
});