Refactor configCustomNetworks / fix configMeta

This commit is contained in:
Connor Bryan 2018-07-13 17:26:08 -05:00
parent 7cd6b83fa3
commit 05aee772d0
9 changed files with 100 additions and 97 deletions

View File

@ -1,11 +1,11 @@
import * as configMetaTypes from './types';
import * as types from './types';
import { changeLanguage } from './actions';
describe('actions', () => {
it('should create an action to change language to index', () => {
const value = 'en';
const expectedAction = {
type: configMetaTypes.ConfigMetaActions.LANGUAGE_CHANGE,
type: types.ConfigMetaActions.LANGUAGE_CHANGE,
payload: value
};
expect(changeLanguage(value)).toEqual(expectedAction);

View File

@ -1,45 +1,45 @@
import { Theme } from 'config';
import * as configMetaTypes from './types';
import * as types from './types';
export function setOnline(): configMetaTypes.SetOnlineAction {
export function setOnline(): types.SetOnlineAction {
return {
type: configMetaTypes.ConfigMetaActions.SET_ONLINE
type: types.ConfigMetaActions.SET_ONLINE
};
}
export function setOffline(): configMetaTypes.SetOfflineAction {
export function setOffline(): types.SetOfflineAction {
return {
type: configMetaTypes.ConfigMetaActions.SET_OFFLINE
type: types.ConfigMetaActions.SET_OFFLINE
};
}
export type TToggleAutoGasLimit = typeof toggleAutoGasLimit;
export function toggleAutoGasLimit(): configMetaTypes.ToggleAutoGasLimitAction {
export function toggleAutoGasLimit(): types.ToggleAutoGasLimitAction {
return {
type: configMetaTypes.ConfigMetaActions.TOGGLE_AUTO_GAS_LIMIT
type: types.ConfigMetaActions.TOGGLE_AUTO_GAS_LIMIT
};
}
export type TChangeLanguage = typeof changeLanguage;
export function changeLanguage(sign: string): configMetaTypes.ChangeLanguageAction {
export function changeLanguage(sign: string): types.ChangeLanguageAction {
return {
type: configMetaTypes.ConfigMetaActions.LANGUAGE_CHANGE,
type: types.ConfigMetaActions.LANGUAGE_CHANGE,
payload: sign
};
}
export type TChangeTheme = typeof changeTheme;
export function changeTheme(theme: Theme): configMetaTypes.ChangeThemeAction {
export function changeTheme(theme: Theme): types.ChangeThemeAction {
return {
type: configMetaTypes.ConfigMetaActions.THEME_CHANGE,
type: types.ConfigMetaActions.THEME_CHANGE,
payload: theme
};
}
export type TSetLatestBlock = typeof setLatestBlock;
export function setLatestBlock(payload: string): configMetaTypes.SetLatestBlockAction {
export function setLatestBlock(payload: string): types.SetLatestBlockAction {
return {
type: configMetaTypes.ConfigMetaActions.SET_LATEST_BLOCK,
type: types.ConfigMetaActions.SET_LATEST_BLOCK,
payload
};
}

View File

@ -1,5 +1,5 @@
import * as configMetaActions from './actions';
import * as configMetaReducer from './reducer';
import * as actions from './actions';
import * as reducer from './reducer';
import { Theme } from 'config';
describe('meta reducer', () => {
@ -39,43 +39,41 @@ describe('meta reducer', () => {
}
};
const actions = {
changeLangauge: configMetaActions.changeLanguage('langaugeToChange'),
setOnline: configMetaActions.setOnline(),
setOffline: configMetaActions.setOffline(),
toggleAutoGasLimit: configMetaActions.toggleAutoGasLimit(),
setLatestBlock: configMetaActions.setLatestBlock('12345')
const actionsToDispatch = {
changeLangauge: actions.changeLanguage('langaugeToChange'),
setOnline: actions.setOnline(),
setOffline: actions.setOffline(),
toggleAutoGasLimit: actions.toggleAutoGasLimit(),
setLatestBlock: actions.setLatestBlock('12345')
};
it('should return the inital state', () =>
expect(configMetaReducer.metaReducer(undefined, {} as any)).toEqual(
expectedState.initialState
));
expect(reducer.metaReducer(undefined, {} as any)).toEqual(expectedState.initialState));
it('should handle toggling to offline', () =>
expect(configMetaReducer.metaReducer(expectedState.initialState, actions.setOffline)).toEqual(
expect(reducer.metaReducer(expectedState.initialState, actionsToDispatch.setOffline)).toEqual(
expectedState.togglingToOffline
));
it('should handle toggling back to online', () =>
expect(
configMetaReducer.metaReducer(expectedState.togglingToOffline, actions.setOnline)
reducer.metaReducer(expectedState.togglingToOffline, actionsToDispatch.setOnline)
).toEqual(expectedState.togglingToOnline));
it('should handle toggling to manual gas limit', () =>
expect(
configMetaReducer.metaReducer(expectedState.initialState, actions.toggleAutoGasLimit)
reducer.metaReducer(expectedState.initialState, actionsToDispatch.toggleAutoGasLimit)
).toEqual(expectedState.togglingToManualGasLimit));
it('should handle toggling back to auto gas limit', () =>
expect(
configMetaReducer.metaReducer(
reducer.metaReducer(
expectedState.togglingToManualGasLimit,
actions.toggleAutoGasLimit
actionsToDispatch.toggleAutoGasLimit
)
).toEqual(expectedState.togglingToAutoGasLimit));
it('should handle setting the latest block', () =>
expect(
configMetaReducer.metaReducer(expectedState.initialState, actions.setLatestBlock)
reducer.metaReducer(expectedState.initialState, actionsToDispatch.setLatestBlock)
).toEqual(expectedState.settingLatestBlock));
});

View File

@ -1,7 +1,7 @@
import { Theme } from 'config';
import * as configMetaTypes from './types';
import * as types from './types';
const META_INITIAL_STATE: configMetaTypes.ConfigMetaState = {
const META_INITIAL_STATE: types.ConfigMetaState = {
languageSelection: 'en',
offline: false,
autoGasLimit: true,
@ -10,32 +10,30 @@ const META_INITIAL_STATE: configMetaTypes.ConfigMetaState = {
};
function changeLanguage(
state: configMetaTypes.ConfigMetaState,
action: configMetaTypes.ChangeLanguageAction
): configMetaTypes.ConfigMetaState {
state: types.ConfigMetaState,
action: types.ChangeLanguageAction
): types.ConfigMetaState {
return {
...state,
languageSelection: action.payload
};
}
function setOnline(state: configMetaTypes.ConfigMetaState): configMetaTypes.ConfigMetaState {
function setOnline(state: types.ConfigMetaState): types.ConfigMetaState {
return {
...state,
offline: false
};
}
function setOffline(state: configMetaTypes.ConfigMetaState): configMetaTypes.ConfigMetaState {
function setOffline(state: types.ConfigMetaState): types.ConfigMetaState {
return {
...state,
offline: true
};
}
function toggleAutoGasLimitEstimation(
state: configMetaTypes.ConfigMetaState
): configMetaTypes.ConfigMetaState {
function toggleAutoGasLimitEstimation(state: types.ConfigMetaState): types.ConfigMetaState {
return {
...state,
autoGasLimit: !state.autoGasLimit
@ -43,9 +41,9 @@ function toggleAutoGasLimitEstimation(
}
function setLatestBlock(
state: configMetaTypes.ConfigMetaState,
action: configMetaTypes.SetLatestBlockAction
): configMetaTypes.ConfigMetaState {
state: types.ConfigMetaState,
action: types.SetLatestBlockAction
): types.ConfigMetaState {
return {
...state,
latestBlock: action.payload
@ -53,9 +51,9 @@ function setLatestBlock(
}
function setTheme(
state: configMetaTypes.ConfigMetaState,
action: configMetaTypes.ChangeThemeAction
): configMetaTypes.ConfigMetaState {
state: types.ConfigMetaState,
action: types.ChangeThemeAction
): types.ConfigMetaState {
return {
...state,
theme: action.payload
@ -63,25 +61,25 @@ function setTheme(
}
export function metaReducer(
state: configMetaTypes.ConfigMetaState = META_INITIAL_STATE,
action: configMetaTypes.MetaAction
): configMetaTypes.ConfigMetaState {
state: types.ConfigMetaState = META_INITIAL_STATE,
action: types.MetaAction
): types.ConfigMetaState {
switch (action.type) {
case configMetaTypes.ConfigMetaActions.LANGUAGE_CHANGE:
case types.ConfigMetaActions.LANGUAGE_CHANGE:
return changeLanguage(state, action);
case configMetaTypes.ConfigMetaActions.SET_ONLINE:
case types.ConfigMetaActions.SET_ONLINE:
return setOnline(state);
case configMetaTypes.ConfigMetaActions.SET_OFFLINE:
case types.ConfigMetaActions.SET_OFFLINE:
return setOffline(state);
case configMetaTypes.ConfigMetaActions.TOGGLE_AUTO_GAS_LIMIT:
case types.ConfigMetaActions.TOGGLE_AUTO_GAS_LIMIT:
return toggleAutoGasLimitEstimation(state);
case configMetaTypes.ConfigMetaActions.SET_LATEST_BLOCK:
case types.ConfigMetaActions.SET_LATEST_BLOCK:
return setLatestBlock(state, action);
case configMetaTypes.ConfigMetaActions.THEME_CHANGE:
case types.ConfigMetaActions.THEME_CHANGE:
return setTheme(state, action);
default:

View File

@ -1,21 +1,21 @@
import { CONFIG_NETWORKS_CUSTOM, AddCustomNetworkAction, RemoveCustomNetworkAction } from './types';
import * as types from './types';
export type TAddCustomNetwork = typeof addCustomNetwork;
export function addCustomNetwork(
payload: AddCustomNetworkAction['payload']
): AddCustomNetworkAction {
payload: types.AddCustomNetworkAction['payload']
): types.AddCustomNetworkAction {
return {
type: CONFIG_NETWORKS_CUSTOM.ADD,
type: types.ConfigNetworksCustomActions.ADD,
payload
};
}
export type TRemoveCustomNetwork = typeof removeCustomNetwork;
export function removeCustomNetwork(
payload: RemoveCustomNetworkAction['payload']
): RemoveCustomNetworkAction {
payload: types.RemoveCustomNetworkAction['payload']
): types.RemoveCustomNetworkAction {
return {
type: CONFIG_NETWORKS_CUSTOM.REMOVE,
type: types.ConfigNetworksCustomActions.REMOVE,
payload
};
}

View File

@ -1,4 +1,11 @@
export * from './types';
export * from './actions';
export * from './reducer';
export * from './selectors';
import * as configNetworksCustomTypes from './types';
import * as configNetworksCustomActions from './actions';
import * as configNetworksCustomReducer from './reducer';
import * as configNetworksCustomSelectors from './selectors';
export {
configNetworksCustomTypes,
configNetworksCustomActions,
configNetworksCustomReducer,
configNetworksCustomSelectors
};

View File

@ -1,5 +1,5 @@
import { CustomNetworkConfig } from 'types/network';
import { addCustomNetwork, removeCustomNetwork } from './actions';
import * as actions from './actions';
import { customNetworksReducer } from './reducer';
const firstCustomNetwork: CustomNetworkConfig = {
@ -28,10 +28,10 @@ const expectedState = {
removeFirstCustomNetwork: { [secondCustomNetwork.id]: secondCustomNetwork }
};
const actions = {
addFirstCustomNetwork: addCustomNetwork(firstCustomNetwork),
addSecondCustomNetwork: addCustomNetwork(secondCustomNetwork),
removeFirstCustomNetwork: removeCustomNetwork(firstCustomNetwork.id)
const actionsToDispatch = {
addFirstCustomNetwork: actions.addCustomNetwork(firstCustomNetwork),
addSecondCustomNetwork: actions.addCustomNetwork(secondCustomNetwork),
removeFirstCustomNetwork: actions.removeCustomNetwork(firstCustomNetwork.id)
};
describe('custom networks reducer', () => {
@ -40,17 +40,23 @@ describe('custom networks reducer', () => {
it('should handle adding the first custom network', () =>
expect(
customNetworksReducer(expectedState.initialState, actions.addFirstCustomNetwork)
customNetworksReducer(expectedState.initialState, actionsToDispatch.addFirstCustomNetwork)
).toEqual(expectedState.addFirstCustomNetwork));
it('should handle adding the second custom network', () =>
expect(
customNetworksReducer(expectedState.addFirstCustomNetwork, actions.addSecondCustomNetwork)
customNetworksReducer(
expectedState.addFirstCustomNetwork,
actionsToDispatch.addSecondCustomNetwork
)
).toEqual(expectedState.addSecondCustomNetwork));
it('should handle removing the first custom network', () =>
expect(
customNetworksReducer(expectedState.addSecondCustomNetwork, actions.removeFirstCustomNetwork)
customNetworksReducer(
expectedState.addSecondCustomNetwork,
actionsToDispatch.removeFirstCustomNetwork
)
).toEqual(expectedState.removeFirstCustomNetwork));
});

View File

@ -1,36 +1,30 @@
import {
CONFIG_NETWORKS_CUSTOM,
AddCustomNetworkAction,
RemoveCustomNetworkAction,
CustomNetworkAction,
CustomNetworksState
} from './types';
import * as types from './types';
const addCustomNetwork = (
state: CustomNetworksState,
{ payload }: AddCustomNetworkAction
): CustomNetworksState => ({
state: types.ConfigCustomNetworksState,
{ payload }: types.AddCustomNetworkAction
): types.ConfigCustomNetworksState => ({
...state,
[payload.id]: payload
});
function removeCustomNetwork(
state: CustomNetworksState,
{ payload }: RemoveCustomNetworkAction
): CustomNetworksState {
state: types.ConfigCustomNetworksState,
{ payload }: types.RemoveCustomNetworkAction
): types.ConfigCustomNetworksState {
const stateCopy = { ...state };
Reflect.deleteProperty(stateCopy, payload);
return stateCopy;
}
export function customNetworksReducer(
state: CustomNetworksState = {},
action: CustomNetworkAction
state: types.ConfigCustomNetworksState = {},
action: types.CustomNetworkAction
) {
switch (action.type) {
case CONFIG_NETWORKS_CUSTOM.ADD:
case types.ConfigNetworksCustomActions.ADD:
return addCustomNetwork(state, action);
case CONFIG_NETWORKS_CUSTOM.REMOVE:
case types.ConfigNetworksCustomActions.REMOVE:
return removeCustomNetwork(state, action);
default:
return state;

View File

@ -1,21 +1,21 @@
import { CustomNetworkConfig } from 'types/network';
export enum CONFIG_NETWORKS_CUSTOM {
export enum ConfigNetworksCustomActions {
ADD = 'CONFIG_NETWORKS_CUSTOM_ADD',
REMOVE = 'CONFIG_NETWORKS_CUSTOM_REMOVE'
}
export interface CustomNetworksState {
export interface ConfigCustomNetworksState {
[customNetworkId: string]: CustomNetworkConfig;
}
export interface AddCustomNetworkAction {
type: CONFIG_NETWORKS_CUSTOM.ADD;
type: ConfigNetworksCustomActions.ADD;
payload: CustomNetworkConfig;
}
export interface RemoveCustomNetworkAction {
type: CONFIG_NETWORKS_CUSTOM.REMOVE;
type: ConfigNetworksCustomActions.REMOVE;
payload: string;
}