diff --git a/app/soapbox/reducers/__tests__/statuses-test.js b/app/soapbox/reducers/__tests__/statuses-test.js index 00687224a..7bcc67c9e 100644 --- a/app/soapbox/reducers/__tests__/statuses-test.js +++ b/app/soapbox/reducers/__tests__/statuses-test.js @@ -1,8 +1,41 @@ import reducer from '../statuses'; -import { Map as ImmutableMap } from 'immutable'; +import { Map as ImmutableMap, fromJS } from 'immutable'; +import { + STATUS_CREATE_REQUEST, + STATUS_CREATE_FAIL, +} from 'soapbox/actions/statuses'; + describe('statuses reducer', () => { it('should return the initial state', () => { expect(reducer(undefined, {})).toEqual(ImmutableMap()); }); + + describe('STATUS_CREATE_REQUEST', () => { + it('increments the replies_count of its parent', () => { + const state = fromJS({ '123': { replies_count: 4 } }); + + const action = { + type: STATUS_CREATE_REQUEST, + params: { in_reply_to_id: '123' }, + }; + + const result = reducer(state, action).getIn(['123', 'replies_count']); + expect(result).toEqual(5); + }); + }); + + describe('STATUS_CREATE_FAIL', () => { + it('decrements the replies_count of its parent', () => { + const state = fromJS({ '123': { replies_count: 5 } }); + + const action = { + type: STATUS_CREATE_FAIL, + params: { in_reply_to_id: '123' }, + }; + + const result = reducer(state, action).getIn(['123', 'replies_count']); + expect(result).toEqual(4); + }); + }); }); diff --git a/app/soapbox/reducers/statuses.js b/app/soapbox/reducers/statuses.js index f8c01ed2b..118d2d7aa 100644 --- a/app/soapbox/reducers/statuses.js +++ b/app/soapbox/reducers/statuses.js @@ -6,6 +6,8 @@ import { FAVOURITE_FAIL, } from '../actions/interactions'; import { + STATUS_CREATE_REQUEST, + STATUS_CREATE_FAIL, STATUS_MUTE_SUCCESS, STATUS_UNMUTE_SUCCESS, STATUS_REVEAL, @@ -33,6 +35,22 @@ const deleteStatus = (state, id, references) => { return state.delete(id); }; +const importPendingStatus = (state, { in_reply_to_id }) => { + if (in_reply_to_id) { + return state.updateIn([in_reply_to_id, 'replies_count'], 0, count => count + 1); + } else { + return state; + } +}; + +const deletePendingStatus = (state, { in_reply_to_id }) => { + if (in_reply_to_id) { + return state.updateIn([in_reply_to_id, 'replies_count'], 0, count => Math.max(0, count - 1)); + } else { + return state; + } +}; + const initialState = ImmutableMap(); export default function statuses(state = initialState, action) { @@ -41,6 +59,10 @@ export default function statuses(state = initialState, action) { return importStatus(state, action.status); case STATUSES_IMPORT: return importStatuses(state, action.statuses); + case STATUS_CREATE_REQUEST: + return importPendingStatus(state, action.params); + case STATUS_CREATE_FAIL: + return deletePendingStatus(state, action.params); case FAVOURITE_REQUEST: return state.update(action.status.get('id'), status => status