Skip to content

Commit

Permalink
Move flag count retrieval to a helper function
Browse files Browse the repository at this point in the history
Move the flag count to a helper which is easier to test and re-use.
  • Loading branch information
robertknight authored and seanh committed Apr 6, 2017
1 parent 396b9d1 commit b71a6ff
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/sidebar/annotation-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 1 addition & 5 deletions src/sidebar/components/moderation-banner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
13 changes: 13 additions & 0 deletions src/sidebar/test/annotation-metadata-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit b71a6ff

Please sign in to comment.