@@ -4,6 +4,7 @@ import { useGit } from './useGit'
44import * as gitApi from '../api/git'
55import * as toast from '../lib/toast'
66import { QueryClient , QueryClientProvider } from '@tanstack/react-query'
7+ import type { GitStatusResponse } from '../types/git'
78
89vi . mock ( '../api/git' , ( ) => ( {
910 gitFetch : vi . fn ( ) ,
@@ -14,6 +15,7 @@ vi.mock('../api/git', () => ({
1415 gitUnstageFiles : vi . fn ( ) ,
1516 gitDiscardFiles : vi . fn ( ) ,
1617 gitReset : vi . fn ( ) ,
18+ fetchGitStatus : vi . fn ( ) ,
1719 fetchGitLog : vi . fn ( ) ,
1820 fetchGitDiff : vi . fn ( ) ,
1921 createBranch : vi . fn ( ) ,
@@ -34,6 +36,15 @@ vi.mock('../lib/toast', () => ({
3436
3537const mockInvalidateQueries = vi . fn ( )
3638const mockSetQueryData = vi . fn ( )
39+ const mockSetQueriesData = vi . fn ( )
40+
41+ const mockGitStatus : GitStatusResponse = {
42+ branch : 'main' ,
43+ ahead : 0 ,
44+ behind : 0 ,
45+ files : [ ] ,
46+ hasChanges : false ,
47+ }
3748
3849vi . mock ( '@tanstack/react-query' , async ( ) => {
3950 const actual = await vi . importActual ( '@tanstack/react-query' )
@@ -42,6 +53,7 @@ vi.mock('@tanstack/react-query', async () => {
4253 useQueryClient : vi . fn ( ( ) => ( {
4354 invalidateQueries : mockInvalidateQueries ,
4455 setQueryData : mockSetQueryData ,
56+ setQueriesData : mockSetQueriesData ,
4557 } ) )
4658 }
4759} )
@@ -62,8 +74,40 @@ describe('useGit', () => {
6274 beforeEach ( ( ) => {
6375 vi . clearAllMocks ( )
6476 mockInvalidateQueries . mockClear ( )
77+ vi . mocked ( gitApi . gitFetch ) . mockResolvedValue ( mockGitStatus )
78+ vi . mocked ( gitApi . gitPull ) . mockResolvedValue ( mockGitStatus )
79+ vi . mocked ( gitApi . gitPush ) . mockResolvedValue ( mockGitStatus )
80+ vi . mocked ( gitApi . gitCommit ) . mockResolvedValue ( mockGitStatus )
81+ vi . mocked ( gitApi . gitStageFiles ) . mockResolvedValue ( mockGitStatus )
82+ vi . mocked ( gitApi . gitUnstageFiles ) . mockResolvedValue ( mockGitStatus )
83+ vi . mocked ( gitApi . gitDiscardFiles ) . mockResolvedValue ( mockGitStatus )
84+ vi . mocked ( gitApi . gitReset ) . mockResolvedValue ( mockGitStatus )
85+ vi . mocked ( gitApi . fetchGitStatus ) . mockResolvedValue ( mockGitStatus )
6586 } )
6687
88+ const expectTargetedStatusCacheUpdate = ( ) => {
89+ expect ( mockSetQueryData ) . toHaveBeenCalledWith ( [ 'gitStatus' , 1 ] , mockGitStatus )
90+ expect ( mockSetQueriesData ) . toHaveBeenCalledWith (
91+ expect . objectContaining ( {
92+ queryKey : [ 'reposGitStatus' ] ,
93+ predicate : expect . any ( Function ) ,
94+ } ) ,
95+ expect . any ( Function ) ,
96+ )
97+
98+ const [ filters , updater ] = mockSetQueriesData . mock . calls [ 0 ]
99+ expect ( filters . predicate ( { queryKey : [ 'reposGitStatus' , [ 1 , 2 ] ] } ) ) . toBe ( true )
100+ expect ( filters . predicate ( { queryKey : [ 'reposGitStatus' , [ 2 , 3 ] ] } ) ) . toBe ( false )
101+ expect ( filters . predicate ( { queryKey : [ 'other' , [ 1 ] ] } ) ) . toBe ( false )
102+
103+ const otherStatus = { ...mockGitStatus , branch : 'dev' }
104+ const oldData = new Map < number , GitStatusResponse > ( [ [ 1 , otherStatus ] , [ 2 , otherStatus ] ] )
105+ const updated = updater ( oldData )
106+ expect ( updated ) . not . toBe ( oldData )
107+ expect ( updated . get ( 1 ) ) . toBe ( mockGitStatus )
108+ expect ( updated . get ( 2 ) ) . toBe ( otherStatus )
109+ }
110+
67111 it ( 'returns all mutations' , ( ) => {
68112 const { result } = renderHook ( ( ) => useGit ( 1 ) , { wrapper : createWrapper ( ) } )
69113
@@ -86,7 +130,9 @@ describe('useGit', () => {
86130 } )
87131
88132 expect ( gitApi . gitFetch ) . toHaveBeenCalledWith ( 1 )
89- expect ( mockInvalidateQueries ) . toHaveBeenCalledWith ( { queryKey : [ 'gitStatus' , 1 ] } )
133+ expectTargetedStatusCacheUpdate ( )
134+ expect ( mockInvalidateQueries ) . not . toHaveBeenCalledWith ( { queryKey : [ 'reposGitStatus' ] } )
135+ expect ( mockInvalidateQueries ) . not . toHaveBeenCalledWith ( { queryKey : [ 'gitStatus' , 1 ] } )
90136 expect ( mockInvalidateQueries ) . toHaveBeenCalledWith ( { queryKey : [ 'fileDiff' , 1 ] } )
91137 expect ( mockInvalidateQueries ) . toHaveBeenCalledWith ( { queryKey : [ 'gitLog' , 1 ] } )
92138 } )
@@ -113,7 +159,9 @@ describe('useGit', () => {
113159 } )
114160
115161 expect ( gitApi . gitPull ) . toHaveBeenCalledWith ( 1 )
116- expect ( mockInvalidateQueries ) . toHaveBeenCalledWith ( { queryKey : [ 'gitStatus' , 1 ] } )
162+ expectTargetedStatusCacheUpdate ( )
163+ expect ( mockInvalidateQueries ) . not . toHaveBeenCalledWith ( { queryKey : [ 'reposGitStatus' ] } )
164+ expect ( mockInvalidateQueries ) . not . toHaveBeenCalledWith ( { queryKey : [ 'gitStatus' , 1 ] } )
117165 expect ( mockInvalidateQueries ) . toHaveBeenCalledWith ( { queryKey : [ 'fileDiff' , 1 ] } )
118166 expect ( mockInvalidateQueries ) . toHaveBeenCalledWith ( { queryKey : [ 'gitLog' , 1 ] } )
119167 } )
@@ -136,10 +184,12 @@ describe('useGit', () => {
136184 const { result } = renderHook ( ( ) => useGit ( 1 ) , { wrapper : createWrapper ( ) } )
137185
138186 await waitFor ( ( ) => {
139- result . current . push . mutateAsync ( )
187+ result . current . push . mutateAsync ( undefined )
140188 } )
141189
142190 expect ( gitApi . gitPush ) . toHaveBeenCalledWith ( 1 , false )
191+ expectTargetedStatusCacheUpdate ( )
192+ expect ( mockInvalidateQueries ) . not . toHaveBeenCalledWith ( { queryKey : [ 'reposGitStatus' ] } )
143193 expect ( mockInvalidateQueries ) . toHaveBeenCalledWith ( { queryKey : [ 'fileDiff' , 1 ] } )
144194 expect ( mockInvalidateQueries ) . toHaveBeenCalledWith ( { queryKey : [ 'gitLog' , 1 ] } )
145195 } )
@@ -150,7 +200,7 @@ describe('useGit', () => {
150200 const { result } = renderHook ( ( ) => useGit ( 1 ) , { wrapper : createWrapper ( ) } )
151201
152202 await waitFor ( ( ) => {
153- result . current . push . mutateAsync ( ) . catch ( ( ) => { } )
203+ result . current . push . mutateAsync ( undefined ) . catch ( ( ) => { } )
154204 } )
155205
156206 expect ( toast . showToast . error ) . toHaveBeenCalledWith ( 'Push failed' )
@@ -166,7 +216,9 @@ describe('useGit', () => {
166216 } )
167217
168218 expect ( gitApi . gitCommit ) . toHaveBeenCalledWith ( 1 , 'test commit' , undefined )
169- expect ( mockInvalidateQueries ) . toHaveBeenCalledWith ( { queryKey : [ 'gitStatus' , 1 ] } )
219+ expectTargetedStatusCacheUpdate ( )
220+ expect ( mockInvalidateQueries ) . not . toHaveBeenCalledWith ( { queryKey : [ 'reposGitStatus' ] } )
221+ expect ( mockInvalidateQueries ) . not . toHaveBeenCalledWith ( { queryKey : [ 'gitStatus' , 1 ] } )
170222 expect ( mockInvalidateQueries ) . toHaveBeenCalledWith ( { queryKey : [ 'fileDiff' , 1 ] } )
171223 expect ( mockInvalidateQueries ) . toHaveBeenCalledWith ( { queryKey : [ 'gitLog' , 1 ] } )
172224 } )
@@ -193,7 +245,9 @@ describe('useGit', () => {
193245 } )
194246
195247 expect ( gitApi . gitStageFiles ) . toHaveBeenCalledWith ( 1 , [ 'file.txt' ] )
196- expect ( mockInvalidateQueries ) . toHaveBeenCalledWith ( { queryKey : [ 'gitStatus' , 1 ] } )
248+ expectTargetedStatusCacheUpdate ( )
249+ expect ( mockInvalidateQueries ) . not . toHaveBeenCalledWith ( { queryKey : [ 'reposGitStatus' ] } )
250+ expect ( mockInvalidateQueries ) . not . toHaveBeenCalledWith ( { queryKey : [ 'gitStatus' , 1 ] } )
197251 expect ( mockInvalidateQueries ) . toHaveBeenCalledWith ( { queryKey : [ 'fileDiff' , 1 ] } )
198252 expect ( mockInvalidateQueries ) . toHaveBeenCalledWith ( { queryKey : [ 'gitLog' , 1 ] } )
199253 } )
@@ -220,7 +274,9 @@ describe('useGit', () => {
220274 } )
221275
222276 expect ( gitApi . gitUnstageFiles ) . toHaveBeenCalledWith ( 1 , [ 'file.txt' ] )
223- expect ( mockInvalidateQueries ) . toHaveBeenCalledWith ( { queryKey : [ 'gitStatus' , 1 ] } )
277+ expectTargetedStatusCacheUpdate ( )
278+ expect ( mockInvalidateQueries ) . not . toHaveBeenCalledWith ( { queryKey : [ 'reposGitStatus' ] } )
279+ expect ( mockInvalidateQueries ) . not . toHaveBeenCalledWith ( { queryKey : [ 'gitStatus' , 1 ] } )
224280 expect ( mockInvalidateQueries ) . toHaveBeenCalledWith ( { queryKey : [ 'fileDiff' , 1 ] } )
225281 expect ( mockInvalidateQueries ) . toHaveBeenCalledWith ( { queryKey : [ 'gitLog' , 1 ] } )
226282 } )
0 commit comments