@@ -6,8 +6,47 @@ jest.mock("@actions/core");
6
6
jest . mock ( "@actions/github" ) ;
7
7
8
8
describe ( "Single report" , function ( ) {
9
- const comment = jest . fn ( ) ;
10
- const output = jest . fn ( ) ;
9
+ let createComment ;
10
+ let listComments ;
11
+ let updateComment ;
12
+ let output ;
13
+
14
+ function getInput ( key ) {
15
+ switch ( key ) {
16
+ case "paths" :
17
+ return "./__tests__/__fixtures__/report.xml" ;
18
+ case "min-coverage-overall" :
19
+ return 45 ;
20
+ case `min-coverage-changed-files` :
21
+ return 60 ;
22
+ }
23
+ }
24
+
25
+ beforeEach ( ( ) => {
26
+ createComment = jest . fn ( ) ;
27
+ listComments = jest . fn ( ) ;
28
+ updateComment = jest . fn ( ) ;
29
+ output = jest . fn ( ) ;
30
+
31
+ core . getInput = jest . fn ( getInput ) ;
32
+ github . getOctokit = jest . fn ( ( ) => {
33
+ return {
34
+ repos : {
35
+ compareCommits : jest . fn ( ( ) => {
36
+ return compareCommitsResponse ;
37
+ } ) ,
38
+ } ,
39
+ issues : {
40
+ createComment : createComment ,
41
+ listComments : listComments ,
42
+ updateComment : updateComment ,
43
+ } ,
44
+ } ;
45
+ } ) ;
46
+ core . setFailed = jest . fn ( ( c ) => {
47
+ fail ( c ) ;
48
+ } ) ;
49
+ } )
11
50
12
51
const compareCommitsResponse = {
13
52
data : {
@@ -26,32 +65,6 @@ describe("Single report", function () {
26
65
} ,
27
66
} ;
28
67
29
- core . getInput = jest . fn ( ( c ) => {
30
- switch ( c ) {
31
- case "paths" :
32
- return "./__tests__/__fixtures__/report.xml" ;
33
- case "min-coverage-overall" :
34
- return 45 ;
35
- case `min-coverage-changed-files` :
36
- return 60 ;
37
- }
38
- } ) ;
39
- github . getOctokit = jest . fn ( ( ) => {
40
- return {
41
- repos : {
42
- compareCommits : jest . fn ( ( ) => {
43
- return compareCommitsResponse ;
44
- } ) ,
45
- } ,
46
- issues : {
47
- createComment : comment ,
48
- } ,
49
- } ;
50
- } ) ;
51
- core . setFailed = jest . fn ( ( c ) => {
52
- fail ( c ) ;
53
- } ) ;
54
-
55
68
describe ( "Pull Request event" , function ( ) {
56
69
const context = {
57
70
eventName : "pull_request" ,
@@ -75,7 +88,7 @@ describe("Single report", function () {
75
88
76
89
await action . action ( ) ;
77
90
78
- expect ( comment . mock . calls [ 0 ] [ 0 ] . body )
91
+ expect ( createComment . mock . calls [ 0 ] [ 0 ] . body )
79
92
. toEqual ( `|File|Coverage [63.64%]|:green_apple:|
80
93
|:-|:-:|:-:|
81
94
|[StringOp.java](https://github.com/thsaravana/jacoco-playground/blob/77b14eb61efcd211ee93a7d8bac80cf292d207cc/src/main/java/com/madrapps/jacoco/operation/StringOp.java)|100%|:green_apple:|
@@ -85,6 +98,34 @@ describe("Single report", function () {
85
98
|:-|:-:|:-:|` ) ;
86
99
} ) ;
87
100
101
+ it ( "updates a previous comment" , async ( ) => {
102
+ github . context = context ;
103
+
104
+ const title = 'JaCoCo Report'
105
+ core . getInput = jest . fn ( ( c ) => {
106
+ switch ( c ) {
107
+ case "title" :
108
+ return title ;
109
+ case "update-comment" :
110
+ return "true" ;
111
+ default :
112
+ return getInput ( c )
113
+ }
114
+ } ) ;
115
+
116
+ listComments . mockReturnValue ( {
117
+ data : [
118
+ { id : 1 , body : "some comment" } ,
119
+ { id : 2 , body : `### ${ title } \n to update` } ,
120
+ ]
121
+ } )
122
+
123
+ await action . action ( ) ;
124
+
125
+ expect ( updateComment . mock . calls [ 0 ] [ 0 ] . comment_id ) . toEqual ( 2 ) ;
126
+ expect ( createComment ) . toHaveBeenCalledTimes ( 0 ) ;
127
+ } ) ;
128
+
88
129
it ( "set overall coverage output" , async ( ) => {
89
130
github . context = context ;
90
131
core . setOutput = output ;
0 commit comments