77
88const gitBranchIs = require ( '..' ) ;
99
10- const BBPromise = require ( 'bluebird' ) . Promise ;
11- // eslint-disable-next-line no-undef
12- const PPromise = typeof Promise === 'function' ? Promise : BBPromise ;
1310const assert = require ( 'assert' ) ;
1411const path = require ( 'path' ) ;
1512
@@ -201,43 +198,82 @@ describe('gitBranchIs', () => {
201198 } ) ;
202199 } ) ;
203200
204- describe ( 'with global Promise' , ( ) => {
205- let hadPromise , oldPromise ;
201+ it ( 'Promise resolves true for same branch name' , ( ) => {
202+ const promise = gitBranchIs ( BRANCH_CURRENT ) ;
203+ assert ( promise instanceof global . Promise ) ;
204+ return promise . then ( ( result ) => {
205+ assert . strictEqual ( result , true ) ;
206+ } ) ;
207+ } ) ;
206208
207- before ( 'ensure global Promise' , ( ) => {
208- if ( global . Promise !== PPromise ) {
209- hadPromise = hasOwnProperty . call ( global , 'Promise' ) ;
210- oldPromise = global . Promise ;
211- global . Promise = PPromise ;
212- }
209+ it ( 'Promise resolves false for different branch name' , ( ) => {
210+ const promise = gitBranchIs ( 'invalid' ) ;
211+ assert ( promise instanceof global . Promise ) ;
212+ return promise . then ( ( result ) => {
213+ assert . strictEqual ( result , false ) ;
213214 } ) ;
215+ } ) ;
214216
215- after ( 'restore global Promise' , ( ) => {
216- if ( hadPromise === true ) {
217- global . Promise = oldPromise ;
218- } else if ( hadPromise === false ) {
219- delete global . Promise ;
217+ it ( 'Promise rejects on Error' , ( ) => {
218+ const promise = gitBranchIs ( BRANCH_CURRENT , 'opts' ) ;
219+ assert ( promise instanceof global . Promise ) ;
220+ return promise . then (
221+ ( result ) => { throw new Error ( 'expecting Error' ) ; } ,
222+ ( err ) => {
223+ assert ( err instanceof TypeError ) ;
224+ assertMatch ( err . message , / \b o p t i o n s \b / ) ;
220225 }
221- } ) ;
226+ ) ;
227+ } ) ;
222228
223- it ( 'Promise resolves true for same branch name' , ( ) => {
224- const promise = gitBranchIs ( BRANCH_CURRENT ) ;
225- assert ( promise instanceof global . Promise ) ;
226- return promise . then ( ( result ) => {
227- assert . strictEqual ( result , true ) ;
228- } ) ;
229+ it ( 'Promise flattens for function returning Promise' , ( ) => {
230+ function checkBranchName ( branchName ) {
231+ return global . Promise . resolve ( branchName === BRANCH_CURRENT ) ;
232+ }
233+ const promise = gitBranchIs ( checkBranchName ) ;
234+ assert ( promise instanceof global . Promise ) ;
235+ return promise . then ( ( result ) => {
236+ assert . strictEqual ( result , true ) ;
229237 } ) ;
238+ } ) ;
239+
240+ it ( 'Promise rejects for function returning Promise' , ( ) => {
241+ // Note: reject with non-Error to ensure no special handling
242+ function checkBranchName ( branchName ) {
243+ return global . Promise . reject ( branchName === BRANCH_CURRENT ) ;
244+ }
245+ const promise = gitBranchIs ( checkBranchName ) ;
246+ assert ( promise instanceof global . Promise ) ;
247+ return promise . then (
248+ ( result ) => { throw new Error ( 'expecting rejection' ) ; } ,
249+ ( err ) => {
250+ assert . strictEqual ( err , true ) ;
251+ }
252+ ) ;
253+ } ) ;
254+
255+ it ( 'Promise rejects for function throwing Error' , ( ) => {
256+ const errTest = new Error ( 'test' ) ;
257+ function checkBranchName ( branchName ) { throw errTest ; }
258+ const promise = gitBranchIs ( checkBranchName ) ;
259+ assert ( promise instanceof global . Promise ) ;
260+ return promise . then (
261+ ( result ) => { throw new Error ( 'expecting rejection' ) ; } ,
262+ ( err ) => { assert . strictEqual ( err , errTest ) ; }
263+ ) ;
264+ } ) ;
230265
231- it ( 'Promise resolves false for different branch name' , ( ) => {
232- const promise = gitBranchIs ( 'invalid' ) ;
266+ describe ( '.getBranch()' , ( ) => {
267+ it ( 'resolves to the branch name' , ( ) => {
268+ const promise = gitBranchIs . getBranch ( ) ;
233269 assert ( promise instanceof global . Promise ) ;
234270 return promise . then ( ( result ) => {
235- assert . strictEqual ( result , false ) ;
271+ assert . strictEqual ( result , BRANCH_CURRENT ) ;
236272 } ) ;
237273 } ) ;
238274
239- it ( 'Promise rejects on Error' , ( ) => {
240- const promise = gitBranchIs ( BRANCH_CURRENT , 'opts' ) ;
275+ it ( 'rejects on Error' , ( ) => {
276+ const promise = gitBranchIs . getBranch ( BRANCH_CURRENT ) ;
241277 assert ( promise instanceof global . Promise ) ;
242278 return promise . then (
243279 ( result ) => { throw new Error ( 'expecting Error' ) ; } ,
@@ -247,64 +283,5 @@ describe('gitBranchIs', () => {
247283 }
248284 ) ;
249285 } ) ;
250-
251- it ( 'Promise flattens for function returning Promise' , ( ) => {
252- function checkBranchName ( branchName ) {
253- return global . Promise . resolve ( branchName === BRANCH_CURRENT ) ;
254- }
255- const promise = gitBranchIs ( checkBranchName ) ;
256- assert ( promise instanceof global . Promise ) ;
257- return promise . then ( ( result ) => {
258- assert . strictEqual ( result , true ) ;
259- } ) ;
260- } ) ;
261-
262- it ( 'Promise rejects for function returning Promise' , ( ) => {
263- // Note: reject with non-Error to ensure no special handling
264- function checkBranchName ( branchName ) {
265- return global . Promise . reject ( branchName === BRANCH_CURRENT ) ;
266- }
267- const promise = gitBranchIs ( checkBranchName ) ;
268- assert ( promise instanceof global . Promise ) ;
269- return promise . then (
270- ( result ) => { throw new Error ( 'expecting rejection' ) ; } ,
271- ( err ) => {
272- assert . strictEqual ( err , true ) ;
273- }
274- ) ;
275- } ) ;
276-
277- it ( 'Promise rejects for function throwing Error' , ( ) => {
278- const errTest = new Error ( 'test' ) ;
279- function checkBranchName ( branchName ) { throw errTest ; }
280- const promise = gitBranchIs ( checkBranchName ) ;
281- assert ( promise instanceof global . Promise ) ;
282- return promise . then (
283- ( result ) => { throw new Error ( 'expecting rejection' ) ; } ,
284- ( err ) => { assert . strictEqual ( err , errTest ) ; }
285- ) ;
286- } ) ;
287-
288- describe ( '.getBranch()' , ( ) => {
289- it ( 'resolves to the branch name' , ( ) => {
290- const promise = gitBranchIs . getBranch ( ) ;
291- assert ( promise instanceof global . Promise ) ;
292- return promise . then ( ( result ) => {
293- assert . strictEqual ( result , BRANCH_CURRENT ) ;
294- } ) ;
295- } ) ;
296-
297- it ( 'rejects on Error' , ( ) => {
298- const promise = gitBranchIs . getBranch ( BRANCH_CURRENT ) ;
299- assert ( promise instanceof global . Promise ) ;
300- return promise . then (
301- ( result ) => { throw new Error ( 'expecting Error' ) ; } ,
302- ( err ) => {
303- assert ( err instanceof TypeError ) ;
304- assertMatch ( err . message , / \b o p t i o n s \b / ) ;
305- }
306- ) ;
307- } ) ;
308- } ) ;
309286 } ) ;
310287} ) ;
0 commit comments