Commit e1e50b04 authored by Sheetal Umesh Kumar's avatar Sheetal Umesh Kumar Committed by GitHub

Merge pull request #347 from hypothesis/update-mod-flag-count-after-flag

Update flag count after moderator flags an annotation themselves
parents 54804916 32768db3
...@@ -150,9 +150,20 @@ var update = { ...@@ -150,9 +150,20 @@ var update = {
var annotations = state.annotations.map(function (annot) { var annotations = state.annotations.map(function (annot) {
var match = (annot.id && annot.id === action.id); var match = (annot.id && annot.id === action.id);
if (match) { if (match) {
return Object.assign({}, annot, { if (annot.flagged === action.isFlagged) {
return annot;
}
var newAnn = Object.assign({}, annot, {
flagged: action.isFlagged, flagged: action.isFlagged,
}); });
if (newAnn.moderation) {
var countDelta = action.isFlagged ? 1 : -1;
newAnn.moderation = Object.assign({}, annot.moderation, {
flagCount: annot.moderation.flagCount + countDelta,
});
}
return newAnn;
} else { } else {
return annot; return annot;
} }
......
...@@ -7,6 +7,7 @@ var thunk = require('redux-thunk').default; ...@@ -7,6 +7,7 @@ var thunk = require('redux-thunk').default;
var annotations = require('../annotations'); var annotations = require('../annotations');
var fixtures = require('../../test/annotation-fixtures'); var fixtures = require('../../test/annotation-fixtures');
var util = require('../util'); var util = require('../util');
var unroll = require('../../../shared/test/util').unroll;
var actions = annotations.actions; var actions = annotations.actions;
...@@ -59,8 +60,10 @@ describe('annotations reducer', function () { ...@@ -59,8 +60,10 @@ describe('annotations reducer', function () {
it('sets the `hidden` state to `true`', function () { it('sets the `hidden` state to `true`', function () {
var store = createStore(); var store = createStore();
var ann = fixtures.moderatedAnnotation({ hidden: false }); var ann = fixtures.moderatedAnnotation({ hidden: false });
store.dispatch(actions.addAnnotations([ann])); store.dispatch(actions.addAnnotations([ann]));
store.dispatch(actions.hideAnnotation(ann.id)); store.dispatch(actions.hideAnnotation(ann.id));
var storeAnn = annotations.findAnnotationByID(store.getState(), ann.id); var storeAnn = annotations.findAnnotationByID(store.getState(), ann.id);
assert.equal(storeAnn.hidden, true); assert.equal(storeAnn.hidden, true);
}); });
...@@ -70,10 +73,64 @@ describe('annotations reducer', function () { ...@@ -70,10 +73,64 @@ describe('annotations reducer', function () {
it('sets the `hidden` state to `false`', function () { it('sets the `hidden` state to `false`', function () {
var store = createStore(); var store = createStore();
var ann = fixtures.moderatedAnnotation({ hidden: true }); var ann = fixtures.moderatedAnnotation({ hidden: true });
store.dispatch(actions.addAnnotations([ann])); store.dispatch(actions.addAnnotations([ann]));
store.dispatch(actions.unhideAnnotation(ann.id)); store.dispatch(actions.unhideAnnotation(ann.id));
var storeAnn = annotations.findAnnotationByID(store.getState(), ann.id); var storeAnn = annotations.findAnnotationByID(store.getState(), ann.id);
assert.equal(storeAnn.hidden, false); assert.equal(storeAnn.hidden, false);
}); });
}); });
describe('#updateFlagStatus', function () {
unroll('updates the flagged status of an annotation', function (testCase) {
var store = createStore();
var ann = fixtures.defaultAnnotation();
ann.flagged = testCase.wasFlagged;
ann.moderation = testCase.oldModeration;
store.dispatch(actions.addAnnotations([ann]));
store.dispatch(actions.updateFlagStatus(ann.id, testCase.nowFlagged));
var storeAnn = annotations.findAnnotationByID(store.getState(), ann.id);
assert.equal(storeAnn.flagged, testCase.nowFlagged);
assert.deepEqual(storeAnn.moderation, testCase.newModeration);
}, [{
// Non-moderator flags annotation
wasFlagged: false,
nowFlagged: true,
oldModeration: undefined,
newModeration: undefined,
}, {
// Non-moderator un-flags annotation
wasFlagged: true,
nowFlagged: false,
oldModeration: undefined,
newModeration: undefined,
},{
// Moderator un-flags an already unflagged annotation
wasFlagged: false,
nowFlagged: false,
oldModeration: { flagCount: 1 },
newModeration: { flagCount: 1 },
},{
// Moderator flags an already flagged annotation
wasFlagged: true,
nowFlagged: true,
oldModeration: { flagCount: 1 },
newModeration: { flagCount: 1 },
},{
// Moderator flags annotation
wasFlagged: false,
nowFlagged: true,
oldModeration: { flagCount: 0 },
newModeration: { flagCount: 1 },
},{
// Moderator un-flags annotation
wasFlagged: true,
nowFlagged: false,
oldModeration: { flagCount: 1 },
newModeration: { flagCount: 0 },
}]);
});
}); });
...@@ -507,15 +507,6 @@ describe('annotationUI', function () { ...@@ -507,15 +507,6 @@ describe('annotationUI', function () {
}); });
}); });
describe('#updateFlagStatus', function () {
it('updates the flaged status of an annotation', function () {
var annot = defaultAnnotation();
annotationUI.addAnnotations([annot]);
annotationUI.updateFlagStatus(annot.id, true);
assert.equal(annotationUI.getState().annotations[0].flagged, true);
});
});
describe('selector functions', function () { describe('selector functions', function () {
// The individual state management modules in reducers/*.js define various // The individual state management modules in reducers/*.js define various
// 'selector' functions for extracting data from the app state. These are // 'selector' functions for extracting data from the app state. These are
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment