Skip to content

Commit

Permalink
Use new config to decide if should post new comment
Browse files Browse the repository at this point in the history
  • Loading branch information
angelikatyborska committed Dec 4, 2022
1 parent e5c5eb8 commit 9634267
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 22 deletions.
14 changes: 7 additions & 7 deletions lib/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ function newCommentWouldHaveContent(snippetIds) {
}

function shouldPostNewComment(previousComment, snippetIds, commentConfig) {
return newCommentWouldHaveContent(snippetIds) && (
!previousComment || (
!!previousComment
&& commentConfig.get('onUpdate') === 'recreate'
&& newCommentDifferentThanPreviousComment(previousComment, snippetIds)
)
);
const isNotEmpty = newCommentWouldHaveContent(snippetIds);
const isCreating = !previousComment && commentConfig.get('onCreate') === 'create';
const isUpdating = !!previousComment
&& commentConfig.get('onUpdate') === 'recreate'
&& newCommentDifferentThanPreviousComment(previousComment, snippetIds);

return isNotEmpty && (isCreating || isUpdating);
}

function shouldDeletePreviousComment(previousComment, snippetIds, commentConfig) {
Expand Down
75 changes: 60 additions & 15 deletions test/comment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ describe('comment', () => {
const previousComment = undefined;
const snippetIds = [];

let commentConfig = new Map([['onUpdate', 'recreate']]);
let commentConfig = new Map([['onCreate', 'create'], ['onUpdate', 'recreate']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
Expand All @@ -198,7 +198,7 @@ describe('comment', () => {
expect(comment.shouldEditPreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);

commentConfig = new Map([['onUpdate', 'edit']]);
commentConfig = new Map([['onCreate', 'create'], ['onUpdate', 'edit']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
Expand All @@ -207,7 +207,16 @@ describe('comment', () => {
expect(comment.shouldEditPreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);

commentConfig = new Map([['onUpdate', 'nothing']]);
commentConfig = new Map([['onCreate', 'create'], ['onUpdate', 'nothing']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
expect(comment.shouldDeletePreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
expect(comment.shouldEditPreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);

commentConfig = new Map([['onCreate', 'nothing'], ['onUpdate', 'nothing']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
Expand All @@ -221,7 +230,7 @@ describe('comment', () => {
const previousComment = undefined;
const snippetIds = ['snippet1', 'snippet2'];

let commentConfig = new Map([['onUpdate', 'recreate']]);
let commentConfig = new Map([['onCreate', 'create'], ['onUpdate', 'recreate']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(true);
Expand All @@ -230,7 +239,7 @@ describe('comment', () => {
expect(comment.shouldEditPreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);

commentConfig = new Map([['onUpdate', 'edit']]);
commentConfig = new Map([['onCreate', 'create'], ['onUpdate', 'edit']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(true);
Expand All @@ -239,21 +248,39 @@ describe('comment', () => {
expect(comment.shouldEditPreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);

commentConfig = new Map([['onUpdate', 'nothing']]);
commentConfig = new Map([['onCreate', 'create'], ['onUpdate', 'nothing']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(true);
expect(comment.shouldDeletePreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
expect(comment.shouldEditPreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);

commentConfig = new Map([['onCreate', 'nothing'], ['onUpdate', 'nothing']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
expect(comment.shouldDeletePreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
expect(comment.shouldEditPreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
});

test('a previous comment with exactly the same snippets', () => {
const previousComment = { body: comment.commentMetadata(['snippet1', 'snippet2']) };
const snippetIds = ['snippet1', 'snippet2'];

let commentConfig = new Map([['onUpdate', 'recreate']]);
let commentConfig = new Map([['onCreate', 'create'], ['onUpdate', 'recreate']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
expect(comment.shouldDeletePreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
expect(comment.shouldEditPreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);

commentConfig = new Map([['onCreate', 'create'], ['onUpdate', 'edit']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
Expand All @@ -262,7 +289,7 @@ describe('comment', () => {
expect(comment.shouldEditPreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);

commentConfig = new Map([['onUpdate', 'edit']]);
commentConfig = new Map([['onCreate', 'create'], ['onUpdate', 'nothing']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
Expand All @@ -271,7 +298,7 @@ describe('comment', () => {
expect(comment.shouldEditPreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);

commentConfig = new Map([['onUpdate', 'nothing']]);
commentConfig = new Map([['onCreate', 'nothing'], ['onUpdate', 'recreate']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
Expand All @@ -285,7 +312,7 @@ describe('comment', () => {
const previousComment = { body: comment.commentMetadata(['snippet2']) };
const snippetIds = ['snippet1', 'snippet2'];

let commentConfig = new Map([['onUpdate', 'recreate']]);
let commentConfig = new Map([['onCreate', 'create'], ['onUpdate', 'recreate']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(true);
Expand All @@ -294,7 +321,7 @@ describe('comment', () => {
expect(comment.shouldEditPreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);

commentConfig = new Map([['onUpdate', 'edit']]);
commentConfig = new Map([['onCreate', 'create'], ['onUpdate', 'edit']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
Expand All @@ -303,21 +330,30 @@ describe('comment', () => {
expect(comment.shouldEditPreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(true);

commentConfig = new Map([['onUpdate', 'nothing']]);
commentConfig = new Map([['onCreate', 'create'], ['onUpdate', 'nothing']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
expect(comment.shouldDeletePreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
expect(comment.shouldEditPreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);

commentConfig = new Map([['onCreate', 'nothing'], ['onUpdate', 'recreate']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(true);
expect(comment.shouldDeletePreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(true);
expect(comment.shouldEditPreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
});

test('a previous comment, new comment would have no snippets', () => {
const previousComment = { body: comment.commentMetadata(['snippet2']) };
const snippetIds = [];

let commentConfig = new Map([['onUpdate', 'recreate']]);
let commentConfig = new Map([['onCreate', 'create'], ['onUpdate', 'recreate']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
Expand All @@ -326,7 +362,7 @@ describe('comment', () => {
expect(comment.shouldEditPreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);

commentConfig = new Map([['onUpdate', 'edit']]);
commentConfig = new Map([['onCreate', 'create'], ['onUpdate', 'edit']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
Expand All @@ -335,14 +371,23 @@ describe('comment', () => {
expect(comment.shouldEditPreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);

commentConfig = new Map([['onUpdate', 'nothing']]);
commentConfig = new Map([['onCreate', 'create'], ['onUpdate', 'nothing']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
expect(comment.shouldDeletePreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
expect(comment.shouldEditPreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);

commentConfig = new Map([['onCreate', 'nothing'], ['onUpdate', 'edit']]);

expect(comment.shouldPostNewComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
expect(comment.shouldDeletePreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(true);
expect(comment.shouldEditPreviousComment(previousComment, snippetIds, commentConfig))
.toEqual(false);
});
});
});

0 comments on commit 9634267

Please sign in to comment.