2021-03-16 22:00:08 +01:00
|
|
|
import * as actionConstants from '../../store/actionConstants';
|
2023-01-20 16:14:40 +01:00
|
|
|
import { HardwareDeviceNames } from '../../../shared/constants/hardware-wallets';
|
2021-03-16 22:00:08 +01:00
|
|
|
import reduceApp from './app';
|
2020-01-09 04:34:58 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const actions = actionConstants;
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('App State', () => {
|
2018-10-10 10:12:46 +02:00
|
|
|
const metamaskState = {
|
2020-02-06 17:38:14 +01:00
|
|
|
selectedAddress: '0xAddress',
|
|
|
|
identities: {
|
|
|
|
'0xAddress': {
|
|
|
|
name: 'account 1',
|
|
|
|
address: '0xAddress',
|
2018-10-10 10:12:46 +02:00
|
|
|
},
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('app init state', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const initState = reduceApp(metamaskState, {});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect.anything(initState);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01: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,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.networkDropdownOpen).toStrictEqual(true);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('sets networkDropdownOpen dropdown to false', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
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,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(newState.networkDropdownOpen).toStrictEqual(false);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01: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,
|
2023-02-03 18:56:44 +01:00
|
|
|
payload: 'test message',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.alertOpen).toStrictEqual(true);
|
|
|
|
expect(state.alertMessage).toStrictEqual('test message');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('closes alert', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
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,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(newState.alertOpen).toStrictEqual(false);
|
|
|
|
expect(newState.alertMessage).toBeNull();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01: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',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.qrCodeData).toStrictEqual('qr data');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01: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',
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.modal.open).toStrictEqual(true);
|
|
|
|
expect(state.modal.modalState.name).toStrictEqual('test');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01: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',
|
|
|
|
},
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-10-31 13:45:27 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const state = { ...metamaskState, appState: { ...opensModal } };
|
2018-10-10 10:12:46 +02:00
|
|
|
const newState = reduceApp(state, {
|
|
|
|
type: actions.MODAL_CLOSE,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(newState.modal.open).toStrictEqual(false);
|
|
|
|
expect(newState.modal.modalState.name).toBeNull();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01: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,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.warning).toBeNull();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('locks Metamask', () => {
|
2018-10-10 10:12:46 +02:00
|
|
|
const state = reduceApp(metamaskState, {
|
|
|
|
type: actions.LOCK_METAMASK,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.warning).toBeNull();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('goes home', () => {
|
2018-10-10 10:12:46 +02:00
|
|
|
const state = reduceApp(metamaskState, {
|
|
|
|
type: actions.GO_HOME,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.accountDetail.privateKey).toStrictEqual('');
|
|
|
|
expect(state.warning).toBeNull();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('clears account details', () => {
|
2020-08-15 01:08:26 +02:00
|
|
|
const exportPrivKeyModal = {
|
|
|
|
accountDetail: {
|
|
|
|
privateKey: 'a-priv-key',
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-08-15 01:08:26 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const state = { ...metamaskState, appState: { ...exportPrivKeyModal } };
|
2020-08-15 01:08:26 +02:00
|
|
|
const newState = reduceApp(state, {
|
|
|
|
type: actions.CLEAR_ACCOUNT_DETAILS,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-08-15 01:08:26 +02:00
|
|
|
|
2023-02-02 23:00:05 +01:00
|
|
|
expect(newState.accountDetail).toStrictEqual({ privateKey: '' });
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-08-15 01:08:26 +02:00
|
|
|
|
2023-02-02 23:00:05 +01:00
|
|
|
it('shows account page', () => {
|
2018-10-10 10:12:46 +02:00
|
|
|
const state = reduceApp(metamaskState, {
|
|
|
|
type: actions.SHOW_ACCOUNTS_PAGE,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.isLoading).toStrictEqual(false);
|
|
|
|
expect(state.warning).toBeNull();
|
|
|
|
expect(state.scrollToBottom).toStrictEqual(false);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01: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,
|
|
|
|
},
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
const oldState = { ...metamaskState, ...txs };
|
2018-10-10 10:12:46 +02:00
|
|
|
const state = reduceApp(oldState, {
|
|
|
|
type: actions.SHOW_CONF_TX_PAGE,
|
|
|
|
id: 2,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.txId).toStrictEqual(2);
|
|
|
|
expect(state.warning).toBeNull();
|
|
|
|
expect(state.isLoading).toStrictEqual(false);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01: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,
|
|
|
|
},
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const oldState = { ...metamaskState, ...txs };
|
2018-10-10 10:12:46 +02:00
|
|
|
|
|
|
|
const state = reduceApp(oldState, {
|
|
|
|
type: actions.COMPLETED_TX,
|
2020-02-06 17:38:14 +01:00
|
|
|
value: {
|
|
|
|
id: 1,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.txId).toBeNull();
|
|
|
|
expect(state.warning).toBeNull();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01: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,
|
2020-02-06 17:38:14 +01:00
|
|
|
value: {
|
|
|
|
unconfirmedActionsCount: 0,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.warning).toBeNull();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01: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,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.warning).toStrictEqual('Incorrect password. Try again.');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01: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',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.warning).toStrictEqual('errors');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('sets warning to empty string when unlock succeeds', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const errorState = { warning: 'errors' };
|
|
|
|
const oldState = { ...metamaskState, ...errorState };
|
2018-10-10 10:12:46 +02:00
|
|
|
const state = reduceApp(oldState, {
|
|
|
|
type: actions.UNLOCK_SUCCEEDED,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.warning).toStrictEqual('');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01: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'",
|
2021-11-08 15:48:41 +01:00
|
|
|
lattice: "m/44'/60'/0'/0",
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-10-10 10:12:46 +02:00
|
|
|
const state = reduceApp(metamaskState, {
|
|
|
|
type: actions.SET_HARDWARE_WALLET_DEFAULT_HD_PATH,
|
2023-02-03 18:56:44 +01:00
|
|
|
payload: {
|
2023-01-20 16:14:40 +01:00
|
|
|
device: HardwareDeviceNames.ledger,
|
2018-10-10 10:12:46 +02:00
|
|
|
path: "m/44'/60'/0'",
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.defaultHdPaths).toStrictEqual(hdPaths);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01: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,
|
2023-02-03 18:56:44 +01:00
|
|
|
payload: 'loading',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.isLoading).toStrictEqual(true);
|
|
|
|
expect(state.loadingMessage).toStrictEqual('loading');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('hides loading message', () => {
|
2021-02-04 19:15:23 +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,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.isLoading).toStrictEqual(false);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01: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,
|
2023-02-03 18:56:44 +01:00
|
|
|
payload: 'warning',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.isLoading).toStrictEqual(false);
|
|
|
|
expect(state.warning).toStrictEqual('warning');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('hides warning', () => {
|
2021-02-04 19:15:23 +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,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.warning).toBeUndefined();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('shows private key', () => {
|
2018-10-10 10:12:46 +02:00
|
|
|
const state = reduceApp(metamaskState, {
|
|
|
|
type: actions.SHOW_PRIVATE_KEY,
|
2023-02-03 18:56:44 +01:00
|
|
|
payload: 'private key',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.accountDetail.privateKey).toStrictEqual('private key');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01: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,
|
2023-02-03 18:56:44 +01:00
|
|
|
payload: true,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-10 10:12:46 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(state.isMouseUser).toStrictEqual(true);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2022-02-18 17:48:38 +01:00
|
|
|
|
|
|
|
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');
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|