@@ -4,13 +4,24 @@ import path from "node:path";
44import process from "node:process" ;
55
66import branchName from "./index" ;
7- import { afterAll , beforeAll , describe , expect , it } from "vitest" ;
7+ import {
8+ afterAll ,
9+ beforeAll ,
10+ beforeEach ,
11+ describe ,
12+ expect ,
13+ it ,
14+ vi ,
15+ } from "vitest" ;
16+ import { execSync } from "node:child_process" ;
817
918const cwd = process . cwd ( ) ;
1019const fixtures = path . join ( cwd , "test" , "fixtures" ) ;
1120
1221const folders = [ "feat_test" , "master" ] ;
1322
23+ vi . mock ( "node:child_process" , { spy : true } ) ;
24+
1425describe ( "branchName" , ( ) => {
1526 beforeAll ( ( ) => {
1627 folders . map ( ( folder ) =>
@@ -21,6 +32,10 @@ describe("branchName", () => {
2132 ) ;
2233 } ) ;
2334
35+ beforeEach ( ( ) => {
36+ vi . clearAllMocks ( ) ;
37+ } ) ;
38+
2439 afterAll ( ( ) => {
2540 folders . map ( ( folder ) =>
2641 fs . renameSync (
@@ -30,6 +45,25 @@ describe("branchName", () => {
3045 ) ;
3146 } ) ;
3247
48+ it ( "check the default" , ( ) => {
49+ branchName ( ) ;
50+
51+ expect ( execSync ) . toHaveBeenCalledWith ( "git branch" , { cwd } ) ;
52+ } ) ;
53+
54+ it ( "check if values are properly ignored" , ( ) => {
55+ branchName ( { cwd : path . join ( fixtures , "master" ) , branchOptions : [ ] } ) ;
56+ branchName ( { branchOptions : [ null , 0 , "--no-color" ] } ) ;
57+
58+ expect ( execSync ) . toHaveBeenCalledTimes ( 2 ) ;
59+ expect ( execSync ) . toHaveBeenNthCalledWith ( 1 , "git branch" , {
60+ cwd : path . join ( fixtures , "master" ) ,
61+ } ) ;
62+ expect ( execSync ) . toHaveBeenNthCalledWith ( 2 , "git branch --no-color" , {
63+ cwd,
64+ } ) ;
65+ } ) ;
66+
3367 it ( "check if the given directory is the branch master" , ( ) => {
3468 expect (
3569 branchName ( {
@@ -53,4 +87,8 @@ describe("branchName", () => {
5387 false ,
5488 ) ;
5589 } ) ;
90+
91+ it ( "check any non existing dir" , ( ) => {
92+ expect ( branchName ( { cwd : "ʕっ•ᴥ•ʔっ" } ) ) . toBe ( false ) ;
93+ } ) ;
5694} ) ;
0 commit comments