11// Copyright (C) 2022-2025 Intel Corporation
22// LIMITED EDGE SOFTWARE DISTRIBUTION LICENSE
33
4- import { QueryClient } from '@tanstack/react-query' ;
4+ import { useQueryClient } from '@tanstack/react-query' ;
55import { waitFor } from '@testing-library/react' ;
66
77import { getMockedWorkspaceIdentifier } from '../../../test-utils/mocked-items-factory/mocked-identifiers' ;
@@ -36,32 +36,39 @@ describe('Use jobs hook utils', () => {
3636 } ) ;
3737
3838 it ( 'Should not invalidate balance if feature flag is disabled' , async ( ) => {
39- const queryClient = new QueryClient ( ) ;
40- queryClient . invalidateQueries = jest . fn ( ) ;
39+ const queryClient = renderHookWithProviders ( useQueryClient ) ;
40+ queryClient . result . current . invalidateQueries = jest . fn ( ) ;
41+
4142 renderHookWithProviders (
42- ( ) =>
43- useInvalidateBalanceOnNewJob (
43+ ( ) => {
44+ return useInvalidateBalanceOnNewJob (
4445 workspaceIdentifier ,
4546 getMockedResponse ( [ getMockedJob ( { cost : { leaseId : '123' , requests : [ ] , consumed : [ ] } } ) ] ) ,
4647 { jobState : JobState . SCHEDULED }
47- ) ,
48+ ) ;
49+ } ,
4850 {
49- providerProps : { queryClient , featureFlags : { FEATURE_FLAG_CREDIT_SYSTEM : false } } ,
51+ providerProps : { featureFlags : { FEATURE_FLAG_CREDIT_SYSTEM : false } } ,
5052 }
5153 ) ;
5254
5355 await waitFor ( ( ) => {
54- expect ( queryClient . invalidateQueries ) . not . toHaveBeenCalled ( ) ;
56+ expect ( queryClient . result . current . invalidateQueries ) . not . toHaveBeenCalled ( ) ;
5557 } ) ;
5658 } ) ;
5759
5860 it ( 'Should invalidate balance if feature flag is enabled' , async ( ) => {
59- const queryClient = new QueryClient ( ) ;
60- queryClient . invalidateQueries = jest . fn ( ) ;
61+ let invalidateSpy : jest . SpyInstance | undefined ;
62+
6163 const { rerender } = renderHookWithProviders (
62- ( { jobs } ) => useInvalidateBalanceOnNewJob ( workspaceIdentifier , getMockedResponse ( jobs ) , { } ) ,
64+ ( { jobs } ) => {
65+ const queryClient = useQueryClient ( ) ;
66+ invalidateSpy = jest . spyOn ( queryClient , 'invalidateQueries' ) ;
67+
68+ return useInvalidateBalanceOnNewJob ( workspaceIdentifier , getMockedResponse ( jobs ) , { } ) ;
69+ } ,
6370 {
64- providerProps : { queryClient , featureFlags : { FEATURE_FLAG_CREDIT_SYSTEM : true } } ,
71+ providerProps : { featureFlags : { FEATURE_FLAG_CREDIT_SYSTEM : true } } ,
6572 initialProps : {
6673 jobs : [
6774 getMockedJob ( {
@@ -87,54 +94,57 @@ describe('Use jobs hook utils', () => {
8794 } ) ;
8895
8996 await waitFor ( ( ) => {
90- expect ( queryClient . invalidateQueries ) . toHaveBeenCalledTimes ( 2 ) ;
97+ expect ( invalidateSpy ) . toHaveBeenCalledTimes ( 2 ) ;
9198 } ) ;
9299 } ) ;
93100
94101 it ( 'Should not invalidate balance if there are no jobs' , async ( ) => {
95- const queryClient = new QueryClient ( ) ;
96- queryClient . invalidateQueries = jest . fn ( ) ;
102+ const queryClient = renderHookWithProviders ( useQueryClient ) ;
103+ queryClient . result . current . invalidateQueries = jest . fn ( ) ;
97104
98105 renderHookWithProviders (
99106 ( ) =>
100107 useInvalidateBalanceOnNewJob ( workspaceIdentifier , getMockedResponse ( [ ] ) , {
101108 jobState : JobState . SCHEDULED ,
102109 } ) ,
103- { providerProps : { queryClient , featureFlags : { FEATURE_FLAG_CREDIT_SYSTEM : true } } }
110+ { providerProps : { featureFlags : { FEATURE_FLAG_CREDIT_SYSTEM : true } } }
104111 ) ;
105112
106113 await waitFor ( ( ) => {
107- expect ( queryClient . invalidateQueries ) . not . toHaveBeenCalled ( ) ;
114+ expect ( queryClient . result . current . invalidateQueries ) . not . toHaveBeenCalled ( ) ;
108115 } ) ;
109116 } ) ;
110117
111118 it ( 'Should not invalidate balance if there are no jobs with cost' , async ( ) => {
112- const queryClient = new QueryClient ( ) ;
113- queryClient . invalidateQueries = jest . fn ( ) ;
119+ const queryClient = renderHookWithProviders ( useQueryClient ) ;
120+ queryClient . result . current . invalidateQueries = jest . fn ( ) ;
114121 renderHookWithProviders (
115122 ( ) =>
116123 useInvalidateBalanceOnNewJob ( workspaceIdentifier , getMockedResponse ( [ getMockedJob ( ) ] ) , {
117124 jobState : JobState . SCHEDULED ,
118125 } ) ,
119- { providerProps : { queryClient , featureFlags : { FEATURE_FLAG_CREDIT_SYSTEM : true } } }
126+ { providerProps : { featureFlags : { FEATURE_FLAG_CREDIT_SYSTEM : true } } }
120127 ) ;
121128
122129 await waitFor ( ( ) => {
123- expect ( queryClient . invalidateQueries ) . not . toHaveBeenCalled ( ) ;
130+ expect ( queryClient . result . current . invalidateQueries ) . not . toHaveBeenCalled ( ) ;
124131 } ) ;
125132 } ) ;
126133
127134 it ( 'Should invalidate balance if there is a job with new id or a new job' , async ( ) => {
128- const queryClient = new QueryClient ( ) ;
129- queryClient . invalidateQueries = jest . fn ( ) ;
135+ let invalidateSpy : jest . SpyInstance | undefined ;
136+
130137 const { rerender } = renderHookWithProviders (
131- ( { jobs } ) =>
132- useInvalidateBalanceOnNewJob ( workspaceIdentifier , getMockedResponse ( jobs ) , {
138+ ( { jobs } ) => {
139+ const queryClient = useQueryClient ( ) ;
140+ invalidateSpy = jest . spyOn ( queryClient , 'invalidateQueries' ) ;
141+
142+ return useInvalidateBalanceOnNewJob ( workspaceIdentifier , getMockedResponse ( jobs ) , {
133143 jobState : JobState . SCHEDULED ,
134- } ) ,
144+ } ) ;
145+ } ,
135146 {
136147 providerProps : {
137- queryClient,
138148 featureFlags : { FEATURE_FLAG_CREDIT_SYSTEM : true } ,
139149 } ,
140150 initialProps : {
@@ -149,7 +159,7 @@ describe('Use jobs hook utils', () => {
149159 ) ;
150160
151161 await waitFor ( ( ) => {
152- expect ( queryClient . invalidateQueries ) . toHaveBeenCalledTimes ( 1 ) ;
162+ expect ( invalidateSpy ) . toHaveBeenCalledTimes ( 1 ) ;
153163 } ) ;
154164
155165 rerender ( {
@@ -163,7 +173,7 @@ describe('Use jobs hook utils', () => {
163173 } ) ;
164174
165175 await waitFor ( ( ) => {
166- expect ( queryClient . invalidateQueries ) . toHaveBeenCalledTimes ( 2 ) ;
176+ expect ( invalidateSpy ) . toHaveBeenCalledTimes ( 2 ) ;
167177 } ) ;
168178
169179 rerender ( {
@@ -178,7 +188,7 @@ describe('Use jobs hook utils', () => {
178188 } ) ;
179189
180190 await waitFor ( ( ) => {
181- expect ( queryClient . invalidateQueries ) . toHaveBeenCalledTimes ( 3 ) ;
191+ expect ( invalidateSpy ) . toHaveBeenCalledTimes ( 3 ) ;
182192 } ) ;
183193 } ) ;
184194} ) ;
0 commit comments