Skip to content

Commit

Permalink
Revert "add test for the AppReducer (steemit#2186)" (steemit#2296)
Browse files Browse the repository at this point in the history
This reverts commit c1e7fbb.
  • Loading branch information
gl2748 authored Jan 7, 2018
1 parent c1e7fbb commit f3312a7
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 74 deletions.
57 changes: 30 additions & 27 deletions src/app/redux/AppReducer.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { Map, OrderedMap } from 'immutable';
import tt from 'counterpart';

const STEEM_API_ERROR = 'app/STEEM_API_ERROR';
const FETCH_DATA_BEGIN = 'app/FETCH_DATA_BEGIN';
const FETCH_DATA_END = 'app/FETCH_DATA_END';
const ADD_NOTIFICATION = 'app/ADD_NOTIFICATION';
const REMOVE_NOTIFICATION = 'app/REMOVE_NOTIFICATION';
const UPDATE_NOTIFICOUNTERS = 'app/UPDATE_NOTIFICOUNTERS';
export const SET_USER_PREFERENCES = 'app/SET_USER_PREFERENCES';
export const TOGGLE_NIGHTMODE = 'app/TOGGLE_NIGHTMODE';
export const TOGGLE_BLOGMODE = 'app/TOGGLE_BLOGMODE';
// Action constants
export const appActions = {
STEEM_API_ERROR: 'app/STEEM_API_ERROR',
FETCH_DATA_BEGIN: 'app/FETCH_DATA_BEGIN',
FETCH_DATA_END: 'app/FETCH_DATA_END',
ADD_NOTIFICATION: 'app/ADD_NOTIFICATION',
REMOVE_NOTIFICATION: 'app/REMOVE_NOTIFICATION',
UPDATE_NOTIFICOUNTERS: 'app/UPDATE_NOTIFICOUNTERS',
SET_USER_PREFERENCES: 'app/SET_USER_PREFERENCES',
TOGGLE_NIGHTMODE: 'app/TOGGLE_NIGHTMODE',
TOGGLE_BLOGMODE: 'app/TOGGLE_BLOGMODE',
};

export const defaultState = Map({
loading: false,
Expand Down Expand Up @@ -42,17 +45,17 @@ export default function reducer(state = defaultState, action = {}) {
switch (action.type) {
case '@@router/LOCATION_CHANGE':
return state.set('location', { pathname: action.payload.pathname });
case STEEM_API_ERROR:
case appActions.STEEM_API_ERROR:
// Until we figure out how to better handle these errors, let em slide.
// This action is the only part of the app that marks an error in state.app.error,
// and the only part of the app which pays attn to this part of the state is in App.jsx.
//return state.set('error', action.error).set('loading', false);
return state;
case FETCH_DATA_BEGIN:
case appActions.FETCH_DATA_BEGIN:
return state.set('loading', true);
case FETCH_DATA_END:
case appActions.FETCH_DATA_END:
return state.set('loading', false);
case ADD_NOTIFICATION: {
case appActions.ADD_NOTIFICATION: {
const n = {
action: tt('g.dismiss'),
dismissAfter: 10000,
Expand All @@ -62,11 +65,11 @@ export default function reducer(state = defaultState, action = {}) {
return s ? s.set(n.key, n) : OrderedMap({ [n.key]: n });
});
}
case REMOVE_NOTIFICATION:
case appActions.REMOVE_NOTIFICATION:
return state.update('notifications', s =>
s.delete(action.payload.key)
);
case UPDATE_NOTIFICOUNTERS: {
case appActions.UPDATE_NOTIFICOUNTERS: {
if (action.payload) {
const nc = action.payload;
if (nc.follow > 0) {
Expand All @@ -77,14 +80,14 @@ export default function reducer(state = defaultState, action = {}) {
}
return state;
}
case SET_USER_PREFERENCES:
case appActions.SET_USER_PREFERENCES:
return state.set('user_preferences', Map(action.payload));
case TOGGLE_NIGHTMODE:
case appActions.TOGGLE_NIGHTMODE:
return state.setIn(
['user_preferences', 'nightmode'],
!state.getIn(['user_preferences', 'nightmode'])
);
case TOGGLE_BLOGMODE:
case appActions.TOGGLE_BLOGMODE:
return state.setIn(
['user_preferences', 'blogmode'],
!state.getIn(['user_preferences', 'blogmode'])
Expand All @@ -95,42 +98,42 @@ export default function reducer(state = defaultState, action = {}) {
}

export const steemApiError = error => ({
type: STEEM_API_ERROR,
type: appActions.STEEM_API_ERROR,
error,
});

export const fetchDataBegin = () => ({
type: FETCH_DATA_BEGIN,
type: appActions.FETCH_DATA_BEGIN,
});

export const fetchDataEnd = () => ({
type: FETCH_DATA_END,
type: appActions.FETCH_DATA_END,
});

export const addNotification = payload => ({
type: ADD_NOTIFICATION,
type: appActions.ADD_NOTIFICATION,
payload,
});

export const removeNotification = payload => ({
type: REMOVE_NOTIFICATION,
type: appActions.REMOVE_NOTIFICATION,
payload,
});

export const updateNotificounters = payload => ({
type: UPDATE_NOTIFICOUNTERS,
type: appActions.UPDATE_NOTIFICOUNTERS,
payload,
});

export const setUserPreferences = payload => ({
type: SET_USER_PREFERENCES,
type: appActions.SET_USER_PREFERENCES,
payload,
});

export const toggleNightmode = () => ({
type: TOGGLE_NIGHTMODE,
type: appActions.TOGGLE_NIGHTMODE,
});

export const toggleBlogmode = () => ({
type: TOGGLE_BLOGMODE,
type: appActions.TOGGLE_BLOGMODE,
});
105 changes: 59 additions & 46 deletions src/app/redux/AppReducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,61 @@ import chaiImmutable from 'chai-immutable';

import { Map, OrderedMap, getIn } from 'immutable';

import reducer, {
defaultState,
steemApiError,
fetchDataBegin,
fetchDataEnd,
addNotification,
removeNotification,
updateNotificounters,
setUserPreferences,
toggleNightmode,
toggleBlogmode,
} from './AppReducer';
import reducer, { defaultState, appActions } from './AppReducer';

chai.use(chaiImmutable);

const mockPayloads = {
addNotification: {
key: 'testKey',
const mockActions = {
LOCATION_CHANGE: {
type: '@@router/LOCATION_CHANGE',
payload: {
pathname: 'testPath',
},
},
removeNotification: {
pathname: 'testPath',
[appActions.STEEM_API_ERROR]: {
type: appActions.STEEM_API_ERROR,
},
updateNotificounters: {
follow: 1,
total: 2,
[appActions.FETCH_DATA_BEGIN]: {
type: appActions.FETCH_DATA_BEGIN,
},
updateNotificountersNoFollow: {
follow: 0,
total: 2,
[appActions.FETCH_DATA_END]: {
type: appActions.FETCH_DATA_END,
},
removeNotification: {
key: 'testKey',
[appActions.ADD_NOTIFICATION]: {
type: appActions.ADD_NOTIFICATION,
payload: {
key: 'testKey',
},
},
setUserPreferences: {
cat: 'mymy',
dog: 'polly',
[appActions.REMOVE_NOTIFICATION]: {
type: appActions.REMOVE_NOTIFICATION,
payload: {
key: 'testKey',
},
},
};

const mockActions = {
LOCATION_CHANGE: {
type: '@@router/LOCATION_CHANGE',
[appActions.UPDATE_NOTIFICOUNTERS]: {
type: appActions.UPDATE_NOTIFICOUNTERS,
payload: {
pathname: 'testPath',
follow: 1,
total: 2,
},
},
[appActions.SET_USER_PREFERENCES]: {
type: appActions.SET_USER_PREFERENCES,
payload: {
cat: 'mymy',
dog: 'polly',
},
},
[appActions.TOGGLE_NIGHTMODE]: {
type: appActions.TOGGLE_NIGHTMODE,
},
[appActions.TOGGLE_BLOGMODE]: {
type: appActions.TOGGLE_BLOGMODE,
},
};

const key = mockPayloads.addNotification.key;
const key = mockActions[appActions.ADD_NOTIFICATION].payload.key;
const mockNotification = OrderedMap({
[key]: {
action: 'missing translation: en.g.dismiss',
Expand All @@ -75,26 +81,29 @@ describe('App reducer', () => {
});
it('should return correct state for a STEEM_API_ERROR action', () => {
const initial = reducer();
const out = reducer(initial, steemApiError());
const out = reducer(initial, mockActions[appActions.STEEM_API_ERROR]);
expect(out).to.eql(initial);
});
it('should return correct state for a FETCH_DATA_BEGIN action', () => {
const initial = reducer();
const actual = reducer(initial, fetchDataBegin());
const actual = reducer(
initial,
mockActions[appActions.FETCH_DATA_BEGIN]
);
const out = actual.get('loading');
expect(out).to.eql(true);
});
it('should return correct state for a FETCH_DATA_END action', () => {
const initial = reducer();
const actual = reducer(initial, fetchDataEnd());
const actual = reducer(initial, mockActions[appActions.FETCH_DATA_END]);
const out = actual.get('loading');
expect(out).to.eql(false);
});
it('should return correct state for a ADD_NOTIFICATION action', () => {
const initial = reducer();
const actual = reducer(
initial,
addNotification(mockPayloads.addNotification)
mockActions[appActions.ADD_NOTIFICATION]
);
const out = actual.getIn(['notifications', key]);
expect(out).to.eql(mockNotification.get(key));
Expand All @@ -107,27 +116,31 @@ describe('App reducer', () => {
);
const actual = reducer(
initialWithNotification,
removeNotification(mockPayloads.removeNotification)
mockActions[appActions.REMOVE_NOTIFICATION]
);
const out = actual.get('notifications');
const expected = OrderedMap();
const expected = OrderedMap({});
expect(out).to.eql(expected);
});
it('should return correct state for a UPDATE_NOTIFICOUNTERS action with a follow in payload', () => {
const initial = reducer();
let actual = reducer(
initial,
updateNotificounters(mockPayloads.updateNotificounters)
mockActions[appActions.UPDATE_NOTIFICOUNTERS]
);
let out = actual.get('notificounters');
let expected = Map({ follow: 0, total: 1 });
expect(out).to.eql(expected);
});
it('should return correct state for a UPDATE_NOTIFICOUNTERS action with no follow in payload', () => {
const initial = reducer();
mockActions[appActions.UPDATE_NOTIFICOUNTERS].payload = {
follow: 0,
total: 2,
};
const actual = reducer(
initial,
updateNotificounters(mockPayloads.updateNotificountersNoFollow)
mockActions[appActions.UPDATE_NOTIFICOUNTERS]
);
const out = actual.get('notificounters');
const expected = Map({ follow: 0, total: 2 });
Expand All @@ -137,7 +150,7 @@ describe('App reducer', () => {
const initial = reducer();
let actual = reducer(
initial,
setUserPreferences(mockPayloads.setUserPreferences)
mockActions[appActions.SET_USER_PREFERENCES]
);
let out = actual.get('user_preferences');
let expected = Map({ cat: 'mymy', dog: 'polly' });
Expand All @@ -146,14 +159,14 @@ describe('App reducer', () => {
it('should return correct state for a TOGGLE_NIGHTMODE action', () => {
const initial = reducer();
const before = initial.getIn(['user_preferences', 'nightmode']);
let actual = reducer(initial, toggleNightmode());
let actual = reducer(initial, mockActions[appActions.TOGGLE_NIGHTMODE]);
const after = actual.getIn(['user_preferences', 'nightmode']);
expect(after).to.eql(!before);
});
it('should return correct state for a TOGGLE_BLOGMODE action', () => {
const initial = reducer();
const before = initial.getIn(['user_preferences', 'blogmode']);
let actual = reducer(initial, toggleBlogmode());
let actual = reducer(initial, mockActions[appActions.TOGGLE_BLOGMODE]);
const after = actual.getIn(['user_preferences', 'blogmode']);
expect(after).to.eql(!before);
});
Expand Down
2 changes: 1 addition & 1 deletion src/app/redux/SagaShared.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import tt from 'counterpart';
import { api } from '@steemit/steem-js';

import * as globalActions from './GlobalReducer';
import * as appActions from './AppReducer';
import appActions from './AppReducer';
import * as transactionActions from './TransactionReducer';
import { setUserPreferences } from 'app/utils/ServerApiClient';

Expand Down

0 comments on commit f3312a7

Please sign in to comment.