Unverified Commit b71a6ffa authored by Robert Knight's avatar Robert Knight Committed by Sean Hammond

Move flag count retrieval to a helper function

Move the flag count to a helper which is easier to test and re-use.
parent 396b9d16
......@@ -180,9 +180,21 @@ function location(annotation) {
return Number.POSITIVE_INFINITY;
}
/**
* Return the number of times the annotation has been flagged
* by other users. If moderation data is unavailable, returns 0.
*/
function flagCount(ann) {
if (!ann.moderation) {
return 0;
}
return ann.moderation.flag_count;
}
module.exports = {
documentMetadata: documentMetadata,
domainAndTitle: domainAndTitle,
flagCount: flagCount,
isAnnotation: isAnnotation,
isNew: isNew,
isOrphan: isOrphan,
......
......@@ -7,11 +7,7 @@ function ModerationBannerController(annotationUI, flash, store) {
var self = this;
this.flagCount = function () {
var moderation = self.annotation.moderation;
if (!moderation) {
return 0;
}
return moderation.flag_count;
return annotationMetadata.flagCount(self.annotation);
};
this.isHidden = function () {
......
......@@ -316,4 +316,17 @@ describe('annotation-metadata', function () {
assert.isFalse(isWaitingToAnchor(pending));
});
});
describe('.flagCount', function () {
var flagCount = annotationMetadata.flagCount;
it('returns 0 if the user is not a moderator', function () {
assert.equal(flagCount(fixtures.defaultAnnotation()), 0);
});
it('returns the flag count if present', function () {
var ann = fixtures.moderatedAnnotation({ flagCount: 10});
assert.equal(flagCount(ann), 10);
});
});
});
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