@@ -11,7 +11,13 @@ import { WebApi } from "azure-devops-node-api";
1111import uuid from "uuid/v4" ;
1212import { Config } from "../../config" ;
1313import { disableVerboseLogging , enableVerboseLogging } from "../../logger" ;
14- import { createPullRequest , GitAPI } from "./azure" ;
14+ import * as gitutils from "../gitutils" ;
15+ import {
16+ createPullRequest ,
17+ generatePRUrl ,
18+ getGitOrigin ,
19+ GitAPI
20+ } from "./azure" ;
1521
1622////////////////////////////////////////////////////////////////////////////////
1723// Tests
@@ -24,52 +30,52 @@ afterAll(() => {
2430 disableVerboseLogging ( ) ;
2531} ) ;
2632
33+ describe ( "test getGitOrigin function" , ( ) => {
34+ it ( "positive test: originPushUrl provided" , async ( ) => {
35+ const res = await getGitOrigin ( "git-url" ) ;
36+ expect ( res ) . toBe ( "git-url" ) ;
37+ } ) ;
38+ it ( "positive test: originPushUrl not provided" , async ( ) => {
39+ jest
40+ . spyOn ( gitutils , "getOriginUrl" )
41+ . mockReturnValueOnce ( Promise . resolve ( "origin-url" ) ) ;
42+ const res = await getGitOrigin ( "" ) ;
43+ expect ( res ) . toBe ( "origin-url" ) ;
44+ } ) ;
45+ it ( "negative test: getOriginUrl throws error" , async ( ) => {
46+ jest
47+ . spyOn ( gitutils , "getOriginUrl" )
48+ . mockReturnValueOnce ( Promise . reject ( new Error ( "Fake" ) ) ) ;
49+ await expect ( getGitOrigin ( "" ) ) . rejects . toThrow ( ) ;
50+ } ) ;
51+ } ) ;
52+
2753describe ( "GitAPI" , ( ) => {
2854 test ( "should fail when PAT not set" , async ( ) => {
29- ( Config as jest . Mock ) . mockReturnValue ( {
55+ ( Config as jest . Mock ) . mockReturnValueOnce ( {
3056 azure_devops : { }
3157 } ) ;
32-
33- let invalidPatError : Error | undefined ;
34- try {
35- await GitAPI ( ) ;
36- } catch ( err ) {
37- invalidPatError = err ;
38- }
39- expect ( invalidPatError ) . toBeDefined ( ) ;
58+ await expect ( GitAPI ( ) ) . rejects . toThrow ( ) ;
4059 } ) ;
4160
4261 test ( "should fail when DevOps org is invalid" , async ( ) => {
43- ( Config as jest . Mock ) . mockReturnValue ( {
62+ ( Config as jest . Mock ) . mockReturnValueOnce ( {
4463 azure_devops : {
4564 access_token : uuid ( )
4665 }
4766 } ) ;
48-
49- let invalidOrgError : Error | undefined ;
50- try {
51- await GitAPI ( ) ;
52- } catch ( err ) {
53- invalidOrgError = err ;
54- }
55- expect ( invalidOrgError ) . toBeDefined ( ) ;
67+ await expect ( GitAPI ( ) ) . rejects . toThrow ( ) ;
5668 } ) ;
5769
5870 test ( "should pass if org url and PAT set" , async ( ) => {
59- ( Config as jest . Mock ) . mockReturnValue ( {
71+ ( Config as jest . Mock ) . mockReturnValueOnce ( {
6072 azure_devops : {
6173 access_token : uuid ( ) ,
6274 org : uuid ( )
6375 }
6476 } ) ;
6577
66- let error : Error | undefined ;
67- try {
68- await GitAPI ( ) ;
69- } catch ( err ) {
70- error = err ;
71- }
72- expect ( error ) . toBeUndefined ( ) ;
78+ await GitAPI ( ) ;
7379 } ) ;
7480} ) ;
7581
@@ -160,4 +166,47 @@ describe("createPullRequest", () => {
160166 expect ( true ) . toBe ( false ) ;
161167 }
162168 } ) ;
169+ it ( "negative test" , async ( ) => {
170+ gitApi . createPullRequest = async ( ) => {
171+ throw new Error ( "fake" ) ;
172+ } ;
173+
174+ await expect (
175+ createPullRequest ( "random title" , "sourceRef" , "targetRef" , {
176+ description : uuid ( ) ,
177+ originPushUrl : "my-git-url"
178+ } )
179+ ) . rejects . toThrow ( ) ;
180+ } ) ;
181+ it ( "negative test: TF401179 error" , async ( ) => {
182+ gitApi . createPullRequest = async ( ) => {
183+ throw new Error ( "TF401179" ) ;
184+ } ;
185+
186+ await expect (
187+ createPullRequest ( "random title" , "sourceRef" , "targetRef" , {
188+ description : uuid ( ) ,
189+ originPushUrl : "my-git-url"
190+ } )
191+ ) . rejects . toThrow ( ) ;
192+ } ) ;
193+ } ) ;
194+
195+ describe ( "test generatePRUrl function" , async ( ) => {
196+ test ( "positive test" , async ( ) => {
197+ const res = await generatePRUrl ( {
198+ pullRequestId : 1 ,
199+ repository : {
200+ id : "test"
201+ }
202+ } ) ;
203+ expect ( res ) . toBe ( "http://foobar.com/pullrequest/1" ) ;
204+ } ) ;
205+ test ( "negative test" , async ( ) => {
206+ await expect (
207+ generatePRUrl ( {
208+ repository : { }
209+ } )
210+ ) . rejects . toThrow ( ) ;
211+ } ) ;
163212} ) ;
0 commit comments