Skip to content

Commit dedc3c6

Browse files
committed
chore: enhance comment handling tests
1 parent 1259ab4 commit dedc3c6

2 files changed

Lines changed: 94 additions & 241 deletions

File tree

__tests__/github/handler.test.ts

Lines changed: 93 additions & 240 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ import {
66
handleComment
77
} from '../../src/github/handler'
88
import { CtrfReport, Inputs } from '../../src/types'
9-
import {
10-
listComments,
11-
addCommentToIssue,
12-
updateComment
13-
} from '../../src/client/github'
9+
import * as githubClient from '../../src/client/github'
1410
import { components } from '@octokit/openapi-types'
1511

1612
type IssueComment = components['schemas']['issue-comment']
@@ -28,11 +24,7 @@ jest.mock('@actions/github', () => ({
2824
jest.mock('../../src/client/github/checks', () => ({
2925
createCheckRun: jest.fn()
3026
}))
31-
jest.mock('../../src/client/github', () => ({
32-
listComments: jest.fn(),
33-
addCommentToIssue: jest.fn(),
34-
updateComment: jest.fn()
35-
}))
27+
jest.mock('../../src/client/github')
3628

3729
describe('createStatusCheck', () => {
3830
const mockCore = jest.mocked(core)
@@ -135,7 +127,7 @@ describe('createStatusCheck', () => {
135127
})
136128

137129
describe('findExistingMarkedComment', () => {
138-
const mockListComments = jest.mocked(listComments)
130+
const mockListComments = jest.mocked(githubClient.listComments)
139131

140132
beforeEach(() => {
141133
jest.clearAllMocks()
@@ -305,249 +297,110 @@ describe('findExistingMarkedComment', () => {
305297
})
306298

307299
describe('handleComment', () => {
308-
const mockListComments = jest.mocked(listComments)
309-
const mockAddComment = jest.mocked(addCommentToIssue)
310-
const mockUpdateComment = jest.mocked(updateComment)
300+
const mockAddComment = jest.fn()
301+
const mockUpdateComment = jest.fn()
302+
const mockListComments = jest.fn()
311303

312304
beforeEach(() => {
313-
jest.clearAllMocks()
305+
jest.resetAllMocks()
306+
;(githubClient.addCommentToIssue as jest.Mock) = mockAddComment
307+
;(githubClient.updateComment as jest.Mock) = mockUpdateComment
308+
;(githubClient.listComments as jest.Mock) = mockListComments
314309
})
315310

316-
it('should create new comment when no existing comment exists', async () => {
317-
mockListComments.mockResolvedValue([])
318-
319-
await handleComment('owner', 'repo', 1, 'New comment', 'MARKER', {
320-
shouldUpdate: false,
321-
shouldOverwrite: false,
322-
alwaysLatestComment: false
311+
describe('New PR - All flags disabled', () => {
312+
it('should create new comment when no comments exist', async () => {
313+
mockListComments.mockResolvedValue([])
314+
315+
await handleComment('owner', 'repo', 1, 'New comment', 'MARKER', {
316+
shouldUpdate: false,
317+
shouldOverwrite: false,
318+
alwaysLatestComment: false
319+
})
320+
321+
expect(mockAddComment).toHaveBeenCalledWith(
322+
'owner',
323+
'repo',
324+
1,
325+
'New comment\nMARKER'
326+
)
327+
expect(mockUpdateComment).not.toHaveBeenCalled()
323328
})
324-
325-
expect(mockAddComment).toHaveBeenCalledWith(
326-
'owner',
327-
'repo',
328-
1,
329-
'New comment\nMARKER'
330-
)
331-
expect(mockUpdateComment).not.toHaveBeenCalled()
332329
})
333330

334-
it('should append to existing comment when shouldUpdate is true and shouldOverwrite is false', async () => {
335-
const existingComment = {
336-
id: 1,
337-
node_id: 'node1',
338-
url: 'url1',
339-
html_url: 'html1',
340-
body: 'Existing content\nMARKER',
341-
user: null,
342-
created_at: new Date().toISOString(),
343-
updated_at: new Date().toISOString()
344-
} as IssueComment
345-
mockListComments.mockResolvedValue([existingComment])
346-
347-
await handleComment('owner', 'repo', 1, 'New content', 'MARKER', {
348-
shouldUpdate: true,
349-
shouldOverwrite: false,
350-
alwaysLatestComment: false
331+
describe('New PR - updateComment enabled', () => {
332+
it('should create new comment when no comment exists and updateComment enabled', async () => {
333+
mockListComments.mockResolvedValue([])
334+
335+
await handleComment('owner', 'repo', 1, 'New comment', 'MARKER', {
336+
shouldUpdate: true,
337+
shouldOverwrite: false,
338+
alwaysLatestComment: false
339+
})
340+
341+
expect(mockAddComment).toHaveBeenCalledWith(
342+
'owner',
343+
'repo',
344+
1,
345+
'New comment\nMARKER'
346+
)
347+
expect(mockUpdateComment).not.toHaveBeenCalled()
351348
})
352349

353-
expect(mockUpdateComment).toHaveBeenCalledWith(
354-
1,
355-
'owner',
356-
'repo',
357-
1,
358-
'Existing content\nMARKER\n\n---\n\nNew content\nMARKER'
359-
)
360-
expect(mockAddComment).not.toHaveBeenCalled()
361-
})
362-
363-
it('should overwrite existing comment when shouldUpdate and shouldOverwrite are true', async () => {
364-
const existingComment = {
365-
id: 1,
366-
node_id: 'node1',
367-
url: 'url1',
368-
html_url: 'html1',
369-
body: 'Old content\nMARKER',
370-
user: null,
371-
created_at: new Date().toISOString(),
372-
updated_at: new Date().toISOString()
373-
} as IssueComment
374-
mockListComments.mockResolvedValue([existingComment])
375-
376-
await handleComment('owner', 'repo', 1, 'New content', 'MARKER', {
377-
shouldUpdate: true,
378-
shouldOverwrite: true,
379-
alwaysLatestComment: false
380-
})
381-
382-
expect(mockUpdateComment).toHaveBeenCalledWith(
383-
1,
384-
'owner',
385-
'repo',
386-
1,
387-
'New content\n\n🔄 This comment has been updated\nMARKER'
388-
)
389-
expect(mockAddComment).not.toHaveBeenCalled()
390-
})
391-
392-
it('should create new comment when alwaysLatestComment is true and existing comment is not latest', async () => {
393-
const comments = [
394-
{
395-
id: 1,
396-
node_id: 'node1',
397-
url: 'url1',
398-
html_url: 'html1',
399-
body: 'Tagged comment\nMARKER',
400-
user: null,
401-
created_at: new Date().toISOString(),
402-
updated_at: new Date().toISOString()
403-
},
404-
{
405-
id: 2,
406-
node_id: 'node2',
407-
url: 'url2',
408-
html_url: 'html2',
409-
body: 'Latest comment',
410-
user: null,
411-
created_at: new Date().toISOString(),
412-
updated_at: new Date().toISOString()
413-
}
414-
] as IssueComment[]
415-
mockListComments.mockResolvedValue(comments)
416-
417-
await handleComment('owner', 'repo', 1, 'New content', 'MARKER', {
418-
shouldUpdate: false,
419-
shouldOverwrite: false,
420-
alwaysLatestComment: true
421-
})
422-
423-
expect(mockAddComment).toHaveBeenCalledWith(
424-
'owner',
425-
'repo',
426-
1,
427-
'New content\nMARKER'
428-
)
429-
expect(mockUpdateComment).not.toHaveBeenCalled()
430-
})
431-
432-
it('should update existing comment when alwaysLatestComment is true but comment is already latest', async () => {
433-
const comments = [
434-
{
435-
id: 1,
436-
node_id: 'node1',
437-
url: 'url1',
438-
html_url: 'html1',
439-
body: 'First comment',
440-
user: null,
441-
created_at: new Date().toISOString(),
442-
updated_at: new Date().toISOString()
443-
},
444-
{
445-
id: 2,
446-
node_id: 'node2',
447-
url: 'url2',
448-
html_url: 'html2',
449-
body: 'Tagged comment\nMARKER',
450-
user: null,
451-
created_at: new Date().toISOString(),
452-
updated_at: new Date().toISOString()
453-
}
454-
] as IssueComment[]
455-
mockListComments.mockResolvedValue(comments)
456-
457-
await handleComment('owner', 'repo', 1, 'New content', 'MARKER', {
458-
shouldUpdate: true,
459-
shouldOverwrite: false,
460-
alwaysLatestComment: true
350+
it('should create new comment when no comment exists and updateComment and alwaysLatest enabled', async () => {
351+
mockListComments.mockResolvedValue([])
352+
353+
await handleComment('owner', 'repo', 1, 'New comment', 'MARKER', {
354+
shouldUpdate: true,
355+
shouldOverwrite: false,
356+
alwaysLatestComment: true
357+
})
358+
359+
expect(mockAddComment).toHaveBeenCalledWith(
360+
'owner',
361+
'repo',
362+
1,
363+
'New comment\nMARKER'
364+
)
365+
expect(mockUpdateComment).not.toHaveBeenCalled()
461366
})
462-
463-
expect(mockUpdateComment).toHaveBeenCalledWith(
464-
2,
465-
'owner',
466-
'repo',
467-
1,
468-
'Tagged comment\nMARKER\n\n---\n\nNew content\nMARKER'
469-
)
470-
expect(mockAddComment).not.toHaveBeenCalled()
471367
})
472368

473-
it('should create new comment when alwaysLatestComment is true, comment is not latest and shouldOverwrite is true', async () => {
474-
const comments = [
475-
{
476-
id: 1,
477-
node_id: 'node1',
478-
url: 'url1',
479-
html_url: 'html1',
480-
body: 'Tagged comment\nMARKER',
481-
user: null,
482-
created_at: new Date().toISOString(),
483-
updated_at: new Date().toISOString()
484-
},
485-
{
486-
id: 2,
487-
node_id: 'node2',
488-
url: 'url2',
489-
html_url: 'html2',
490-
body: 'Latest comment',
491-
user: null,
492-
created_at: new Date().toISOString(),
493-
updated_at: new Date().toISOString()
494-
}
495-
] as IssueComment[]
496-
mockListComments.mockResolvedValue(comments)
497-
498-
await handleComment('owner', 'repo', 1, 'New content', 'MARKER', {
499-
shouldUpdate: true,
500-
shouldOverwrite: true,
501-
alwaysLatestComment: true
369+
describe('New PR - overwriteComment enabled', () => {
370+
it('should create new comment when no comment exists and overwriteComment enabled', async () => {
371+
mockListComments.mockResolvedValue([])
372+
373+
await handleComment('owner', 'repo', 1, 'New comment', 'MARKER', {
374+
shouldUpdate: false,
375+
shouldOverwrite: true,
376+
alwaysLatestComment: false
377+
})
378+
379+
expect(mockAddComment).toHaveBeenCalledWith(
380+
'owner',
381+
'repo',
382+
1,
383+
'New comment\nMARKER'
384+
)
385+
expect(mockUpdateComment).not.toHaveBeenCalled()
502386
})
503387

504-
expect(mockAddComment).toHaveBeenCalledWith(
505-
'owner',
506-
'repo',
507-
1,
508-
'New content\nMARKER'
509-
)
510-
expect(mockUpdateComment).not.toHaveBeenCalled()
511-
})
512-
513-
it('should overwrite existing comment when alwaysLatestComment is true but comment is already latest and shouldOverwrite is true', async () => {
514-
const comments = [
515-
{
516-
id: 1,
517-
node_id: 'node1',
518-
url: 'url1',
519-
html_url: 'html1',
520-
body: 'First comment',
521-
user: null,
522-
created_at: new Date().toISOString(),
523-
updated_at: new Date().toISOString()
524-
},
525-
{
526-
id: 2,
527-
node_id: 'node2',
528-
url: 'url2',
529-
html_url: 'html2',
530-
body: 'Tagged comment\nMARKER',
531-
user: null,
532-
created_at: new Date().toISOString(),
533-
updated_at: new Date().toISOString()
534-
}
535-
] as IssueComment[]
536-
mockListComments.mockResolvedValue(comments)
537-
538-
await handleComment('owner', 'repo', 1, 'New content', 'MARKER', {
539-
shouldUpdate: true,
540-
shouldOverwrite: true,
541-
alwaysLatestComment: true
388+
it('should create new comment when no comment exists and overwriteComment and alwaysLatest enabled', async () => {
389+
mockListComments.mockResolvedValue([])
390+
391+
await handleComment('owner', 'repo', 1, 'New comment', 'MARKER', {
392+
shouldUpdate: false,
393+
shouldOverwrite: true,
394+
alwaysLatestComment: true
395+
})
396+
397+
expect(mockAddComment).toHaveBeenCalledWith(
398+
'owner',
399+
'repo',
400+
1,
401+
'New comment\nMARKER'
402+
)
403+
expect(mockUpdateComment).not.toHaveBeenCalled()
542404
})
543-
544-
expect(mockUpdateComment).toHaveBeenCalledWith(
545-
2,
546-
'owner',
547-
'repo',
548-
1,
549-
'New content\n\n🔄 This comment has been updated\nMARKER'
550-
)
551-
expect(mockAddComment).not.toHaveBeenCalled()
552405
})
553406
})

badges/coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)