@@ -36,20 +36,26 @@ jest.mock('@actions/github', () => ({
3636} ) ) ;
3737jest . mock ( '@actions/core' ) ;
3838
39+ process . env . GITHUB_EVENT_PATH = 'event.json' ;
40+ jest . mock ( 'event.json' , ( ) => ( {
41+ // __esModule: true,
42+ default : { before : 'sha' } ,
43+ } ) , { virtual : true } ) ;
44+
3945describe ( 'blogpub' , ( ) => {
4046 beforeEach ( jest . clearAllMocks ) ;
4147
4248 ( getOctokit as jest . Mock ) . mockReturnValue ( octokitMock ) ;
4349
4450 const fileData = {
45- data : {
46- files : [
47- {
48- filename : 'blogs/blog-01.md' ,
49- } ,
50- ] ,
51- } ,
52- } ;
51+ data : {
52+ files : [
53+ {
54+ filename : 'blogs/blog-01.md' ,
55+ } ,
56+ ] ,
57+ } ,
58+ } ;
5359
5460 it ( 'should set failed when fails to get PR info' , async ( ) => {
5561 const err = new Error ( 'github' ) ;
@@ -99,8 +105,26 @@ describe('blogpub', () => {
99105 expect ( promises . readFile ) . toHaveBeenCalledWith ( './blogs/blog-01.md' , 'utf8' ) ;
100106 expect ( core . setFailed ) . toHaveBeenCalledWith ( err ) ;
101107 } ) ;
108+
109+ it ( 'should skip if file already exists in repository' , async ( ) => {
110+ ( core . getInput as jest . Mock ) . mockImplementation ( ( key : string ) => {
111+ return key === 'articles_folder' ? 'blogs' : '' ;
112+ } ) ;
113+ octokitMock . request . mockResolvedValue ( {
114+ data : {
115+ files : [
116+ {
117+ filename : 'readme.txt' ,
118+ } ,
119+ ] ,
120+ } ,
121+ } ) ;
122+ octokitMock . request . mockResolvedValueOnce ( { status : 200 } ) ;
123+ await run ( ) ;
124+ expect ( parseArticle ) . not . toHaveBeenCalled ( ) ;
125+ } ) ;
102126
103- it ( 'should set failed if fails to parse article' , async ( ) => {
127+ it ( 'should set failed if fails to parse file into article' , async ( ) => {
104128 ( core . getInput as jest . Mock ) . mockImplementation ( ( key : string ) => {
105129 return key === 'articles_folder' ? 'blogs' : '' ;
106130 } ) ;
@@ -111,7 +135,8 @@ describe('blogpub', () => {
111135 ( parseArticle as jest . Mock ) . mockImplementation ( ( ) => {
112136 throw err ;
113137 } ) ;
114- octokitMock . request . mockResolvedValue ( fileData ) ;
138+ octokitMock . request . mockResolvedValueOnce ( fileData ) ;
139+ octokitMock . request . mockResolvedValueOnce ( { status : 404 } ) ;
115140
116141 await run ( ) ;
117142
@@ -143,7 +168,8 @@ describe('blogpub', () => {
143168 content : 'parsed' ,
144169 } ) ;
145170 ( medium . createArticle as jest . Mock ) . mockRejectedValue ( err ) ;
146- octokitMock . request . mockResolvedValue ( fileData ) ;
171+ octokitMock . request . mockResolvedValueOnce ( fileData ) ;
172+ octokitMock . request . mockResolvedValueOnce ( { status : 404 } ) ;
147173
148174 await run ( ) ;
149175
@@ -168,7 +194,8 @@ describe('blogpub', () => {
168194 } ) ;
169195 ( devto . createArticle as jest . Mock ) . mockRejectedValue ( new Error ( '' ) ) ;
170196 ( medium . createArticle as jest . Mock ) . mockResolvedValue ( { url : 'medium.com/new' } ) ;
171- octokitMock . request . mockResolvedValue ( fileData ) ;
197+ octokitMock . request . mockResolvedValueOnce ( fileData ) ;
198+ octokitMock . request . mockResolvedValueOnce ( { status : 404 } ) ;
172199
173200 await run ( ) ;
174201
@@ -203,7 +230,8 @@ describe('blogpub', () => {
203230 } ) ;
204231 ( medium . createArticle as jest . Mock ) . mockResolvedValue ( { url : 'medium.com/new' } ) ;
205232 ( devto . createArticle as jest . Mock ) . mockRejectedValue ( err ) ;
206- octokitMock . request . mockResolvedValue ( fileData ) ;
233+ octokitMock . request . mockResolvedValueOnce ( fileData ) ;
234+ octokitMock . request . mockResolvedValueOnce ( { status : 404 } ) ;
207235
208236 await run ( ) ;
209237
@@ -236,7 +264,8 @@ describe('blogpub', () => {
236264 } ) ;
237265 ( medium . createArticle as jest . Mock ) . mockResolvedValue ( { url : 'medium.com/new' } ) ;
238266 ( devto . createArticle as jest . Mock ) . mockResolvedValue ( { url : 'dev.to/new' } ) ;
239- octokitMock . request . mockResolvedValue ( fileData ) ;
267+ octokitMock . request . mockResolvedValueOnce ( fileData ) ;
268+ octokitMock . request . mockResolvedValueOnce ( { status : 404 } ) ;
240269
241270 await run ( ) ;
242271
0 commit comments