Project

General

Profile

« Previous | Next » 

Revision 878f746e

Added by matan about 7 years ago

Fixes #18888 - refactor powerStatus

View differences:

webpack/assets/javascripts/react_app/components/hosts/powerStatus/index.js
import React from 'react';
import { connect } from 'react-redux';
import * as HostsActions from '../../../redux/actions/hosts/';
import * as HostsActions from '../../../redux/actions/hosts/powerStatus/';
import PowerStatusInner from './powerStatusInner';
class PowerStatus extends React.Component {
webpack/assets/javascripts/react_app/redux/actions/hosts/hosts.fixtures.js
export const requestData = { id: 1, url: 'test' };
export const onFailureActions = [
{ payload: { id: 1 }, type: 'HOST_POWER_STATUS_REQUEST' },
{
payload: { error: {}, item: { id: 1 } },
type: 'HOST_POWER_STATUS_FAILURE'
}
];
webpack/assets/javascripts/react_app/redux/actions/hosts/hosts.test.js
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import * as actions from './index';
import immutable from 'seamless-immutable';
import { requestData, onFailureActions } from './hosts.fixtures';
const mockStore = configureMockStore([thunk]);
describe('hosts actions', () => {
it(
'creates HOST_POWER_STATUS_REQUEST and fails when http mocking is not applied',
() => {
const store = mockStore({
hosts: {
powerStatus: immutable({})
}
});
store.dispatch(actions.getHostPowerState(requestData));
expect(store.getActions()).toEqual(onFailureActions);
}
);
});
webpack/assets/javascripts/react_app/redux/actions/hosts/index.js
import {
HOST_POWER_STATUS_REQUEST,
HOST_POWER_STATUS_SUCCESS,
HOST_POWER_STATUS_FAILURE
} from '../../consts';
import { ajaxRequestAction } from '../common';
export const getHostPowerState = ({id, url}) => dispatch => ajaxRequestAction({
dispatch,
requestAction: HOST_POWER_STATUS_REQUEST,
successAction: HOST_POWER_STATUS_SUCCESS,
failedAction: HOST_POWER_STATUS_FAILURE,
url,
item: { id }
});
webpack/assets/javascripts/react_app/redux/actions/hosts/powerStatus/index.js
import {
HOST_POWER_STATUS_REQUEST,
HOST_POWER_STATUS_SUCCESS,
HOST_POWER_STATUS_FAILURE
} from '../../../consts';
import { ajaxRequestAction } from '../../common';
export const getHostPowerState = ({id, url}) => dispatch => ajaxRequestAction({
dispatch,
requestAction: HOST_POWER_STATUS_REQUEST,
successAction: HOST_POWER_STATUS_SUCCESS,
failedAction: HOST_POWER_STATUS_FAILURE,
url,
item: { id }
});
webpack/assets/javascripts/react_app/redux/actions/hosts/powerStatus/powerStatus.fixtures.js
export const requestData = { id: 1, url: 'test' };
export const onFailureActions = [
{ payload: { id: 1 }, type: 'HOST_POWER_STATUS_REQUEST' },
{
payload: { error: {}, item: { id: 1 } },
type: 'HOST_POWER_STATUS_FAILURE'
}
];
webpack/assets/javascripts/react_app/redux/actions/hosts/powerStatus/powerStatus.js
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import * as actions from './index';
import immutable from 'seamless-immutable';
import { requestData, onFailureActions } from './hosts.fixtures';
const mockStore = configureMockStore([thunk]);
describe('hosts actions', () => {
it(
'creates HOST_POWER_STATUS_REQUEST and fails when http mocking is not applied',
() => {
const store = mockStore({
hosts: {
powerStatus: immutable({})
}
});
store.dispatch(actions.getHostPowerState(requestData));
expect(store.getActions()).toEqual(onFailureActions);
}
);
});
webpack/assets/javascripts/react_app/redux/reducers/hosts/hosts.fixtures.js
import Immutable from 'seamless-immutable';
export const initialState = Immutable({
powerStatus: Immutable({})
});
export const request = {
id: '2',
url: 'test'
};
export const response = {
id: '2',
data: 'data'
};
export const error = 'some error happened';
export const stateBeforeResponse = Immutable({
powerStatus: {
[request.id]: request
}
});
export const stateAfterSuccess = Immutable({
powerStatus: Immutable({
[request.id]: {
...response
}
})
});
export const stateAfterFailure = Immutable({
powerStatus: Immutable({
[request.id]: {
error
}
})
});
webpack/assets/javascripts/react_app/redux/reducers/hosts/hosts.test.js
import reducer from './index';
import * as types from '../../consts';
import {
initialState,
request,
stateBeforeResponse,
response,
stateAfterSuccess,
stateAfterFailure,
error
} from './hosts.fixtures';
describe('statistics reducer', () => {
it('should return the initial state', () => {
expect(reducer(undefined, {})).toEqual(initialState);
});
it('should handle HOST_POWER_STATUS_REQUEST', () => {
expect(
reducer(initialState, {
type: types.HOST_POWER_STATUS_REQUEST,
payload: request
})
).toEqual(stateBeforeResponse);
});
it('should handle HOST_POWER_STATUS_SUCCESS', () => {
expect(
reducer(stateBeforeResponse, {
type: types.HOST_POWER_STATUS_SUCCESS,
payload: response
})
).toEqual(stateAfterSuccess);
});
it('should handle HOST_POWER_STATUS_FAILURE', () => {
expect(
reducer(stateBeforeResponse, {
type: types.HOST_POWER_STATUS_FAILURE,
payload: { error, id: request.id}
})
).toEqual(stateAfterFailure);
});
});
webpack/assets/javascripts/react_app/redux/reducers/hosts/index.js
import {
HOST_POWER_STATUS_REQUEST,
HOST_POWER_STATUS_SUCCESS,
HOST_POWER_STATUS_FAILURE
} from '../../consts';
import Immutable from 'seamless-immutable';
import { combineReducers } from 'redux';
import powerStatus from './powerStatus';
const initialState = Immutable({
powerStatus: Immutable({})
export default combineReducers({
powerStatus
});
export default (state = initialState, action) => {
const { payload } = action;
switch (action.type) {
case HOST_POWER_STATUS_REQUEST:
case HOST_POWER_STATUS_SUCCESS:
return state.setIn(
['powerStatus', payload.id],
payload
);
case HOST_POWER_STATUS_FAILURE:
return state.setIn(
['powerStatus', payload.id],
{ error: payload.error }
);
default:
return state;
}
};
webpack/assets/javascripts/react_app/redux/reducers/hosts/powerStatus/index.js
import {
HOST_POWER_STATUS_REQUEST,
HOST_POWER_STATUS_SUCCESS,
HOST_POWER_STATUS_FAILURE
} from '../../../consts';
import Immutable from 'seamless-immutable';
const initialState = Immutable({});
export default (state = initialState, action) => {
const { payload } = action;
switch (action.type) {
case HOST_POWER_STATUS_REQUEST:
case HOST_POWER_STATUS_SUCCESS:
return state.set(
payload.id,
payload
);
case HOST_POWER_STATUS_FAILURE:
return state.set(
payload.id,
{ error: payload.error }
);
default:
return state;
}
};
webpack/assets/javascripts/react_app/redux/reducers/hosts/powerStatus/powerStatus.fixtures.js
import Immutable from 'seamless-immutable';
export const initialState = Immutable({});
export const request = {
id: '2',
url: 'test'
};
export const response = {
id: '2',
data: 'data'
};
export const error = 'some error happened';
export const stateBeforeResponse = Immutable({
[request.id]: request
});
export const stateAfterSuccess = Immutable({
[request.id]: {
...response
}
});
export const stateAfterFailure = Immutable({
[request.id]: {
error
}
});
webpack/assets/javascripts/react_app/redux/reducers/hosts/powerStatus/powerStatus.test.js
import reducer from './index';
import * as types from '../../../consts';
import {
initialState,
request,
stateBeforeResponse,
response,
stateAfterSuccess,
stateAfterFailure,
error
} from './powerStatus.fixtures';
describe('powerStatus reducer', () => {
it('should return the initial state', () => {
expect(reducer(undefined, {})).toEqual(initialState);
});
it('should handle HOST_POWER_STATUS_REQUEST', () => {
expect(
reducer(initialState, {
type: types.HOST_POWER_STATUS_REQUEST,
payload: request
})
).toEqual(stateBeforeResponse);
});
it('should handle HOST_POWER_STATUS_SUCCESS', () => {
expect(
reducer(stateBeforeResponse, {
type: types.HOST_POWER_STATUS_SUCCESS,
payload: response
})
).toEqual(stateAfterSuccess);
});
it('should handle HOST_POWER_STATUS_FAILURE', () => {
expect(
reducer(stateBeforeResponse, {
type: types.HOST_POWER_STATUS_FAILURE,
payload: { error, id: request.id}
})
).toEqual(stateAfterFailure);
});
});

Also available in: Unified diff