diff --git a/lib/comment.js b/lib/comment.js index 342a964..2fc1df9 100644 --- a/lib/comment.js +++ b/lib/comment.js @@ -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) { diff --git a/test/comment.test.js b/test/comment.test.js index 919bd66..e39bce1 100644 --- a/test/comment.test.js +++ b/test/comment.test.js @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -239,7 +248,7 @@ 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); @@ -247,13 +256,31 @@ describe('comment', () => { .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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -303,7 +330,7 @@ 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); @@ -311,13 +338,22 @@ describe('comment', () => { .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); @@ -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); @@ -335,7 +371,7 @@ 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); @@ -343,6 +379,15 @@ describe('comment', () => { .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); }); }); });