11// Copyright (C) 2022-2025 Intel Corporation
22// LIMITED EDGE SOFTWARE DISTRIBUTION LICENSE
33
4- import { useQueryClient } from '@tanstack/react-query' ;
4+ import { ReactNode } from 'react' ;
5+
6+ import { QueryClientProvider } from '@tanstack/react-query' ;
57import { waitFor } from '@testing-library/react' ;
68
9+ import { createGetiQueryClient } from '../../../providers/query-client-provider/query-client-provider.component' ;
710import { getMockedWorkspaceIdentifier } from '../../../test-utils/mocked-items-factory/mocked-identifiers' ;
811import { getMockedJob } from '../../../test-utils/mocked-items-factory/mocked-jobs' ;
912import { renderHookWithProviders } from '../../../test-utils/render-hook-with-providers' ;
@@ -29,16 +32,23 @@ const getMockedResponse = (jobs: Job[]) => ({
2932} ) ;
3033
3134const workspaceIdentifier = getMockedWorkspaceIdentifier ( { workspaceId : 'workspaceId' } ) ;
35+ const mockSetInvalidateQueries = jest . fn ( ) ;
36+
37+ const queryClient = createGetiQueryClient ( {
38+ addNotification : jest . fn ( ) ,
39+ } ) ;
40+ queryClient . invalidateQueries = mockSetInvalidateQueries ;
41+
42+ const wrapper = ( { children } : { children : ReactNode } ) => (
43+ < QueryClientProvider client = { queryClient } > { children } </ QueryClientProvider >
44+ ) ;
3245
3346describe ( 'Use jobs hook utils' , ( ) => {
34- beforeAll ( ( ) => {
35- jest . resetAllMocks ( ) ;
47+ beforeEach ( ( ) => {
48+ jest . clearAllMocks ( ) ;
3649 } ) ;
3750
3851 it ( 'Should not invalidate balance if feature flag is disabled' , async ( ) => {
39- const queryClient = renderHookWithProviders ( useQueryClient ) ;
40- queryClient . result . current . invalidateQueries = jest . fn ( ) ;
41-
4252 renderHookWithProviders (
4353 ( ) => {
4454 return useInvalidateBalanceOnNewJob (
@@ -48,26 +58,23 @@ describe('Use jobs hook utils', () => {
4858 ) ;
4959 } ,
5060 {
61+ wrapper,
5162 providerProps : { featureFlags : { FEATURE_FLAG_CREDIT_SYSTEM : false } } ,
5263 }
5364 ) ;
5465
5566 await waitFor ( ( ) => {
56- expect ( queryClient . result . current . invalidateQueries ) . not . toHaveBeenCalled ( ) ;
67+ expect ( mockSetInvalidateQueries ) . not . toHaveBeenCalled ( ) ;
5768 } ) ;
5869 } ) ;
5970
6071 it ( 'Should invalidate balance if feature flag is enabled' , async ( ) => {
61- let invalidateSpy : jest . SpyInstance | undefined ;
62-
6372 const { rerender } = renderHookWithProviders (
6473 ( { jobs } ) => {
65- const queryClient = useQueryClient ( ) ;
66- invalidateSpy = jest . spyOn ( queryClient , 'invalidateQueries' ) ;
67-
6874 return useInvalidateBalanceOnNewJob ( workspaceIdentifier , getMockedResponse ( jobs ) , { } ) ;
6975 } ,
7076 {
77+ wrapper,
7178 providerProps : { featureFlags : { FEATURE_FLAG_CREDIT_SYSTEM : true } } ,
7279 initialProps : {
7380 jobs : [
@@ -94,56 +101,53 @@ describe('Use jobs hook utils', () => {
94101 } ) ;
95102
96103 await waitFor ( ( ) => {
97- expect ( invalidateSpy ) . toHaveBeenCalledTimes ( 2 ) ;
104+ expect ( mockSetInvalidateQueries ) . toHaveBeenCalledTimes ( 2 ) ;
98105 } ) ;
99106 } ) ;
100107
101108 it ( 'Should not invalidate balance if there are no jobs' , async ( ) => {
102- const queryClient = renderHookWithProviders ( useQueryClient ) ;
103- queryClient . result . current . invalidateQueries = jest . fn ( ) ;
104-
105109 renderHookWithProviders (
106110 ( ) =>
107111 useInvalidateBalanceOnNewJob ( workspaceIdentifier , getMockedResponse ( [ ] ) , {
108112 jobState : JobState . SCHEDULED ,
109113 } ) ,
110- { providerProps : { featureFlags : { FEATURE_FLAG_CREDIT_SYSTEM : true } } }
114+ {
115+ wrapper,
116+ providerProps : { featureFlags : { FEATURE_FLAG_CREDIT_SYSTEM : true } } ,
117+ }
111118 ) ;
112119
113120 await waitFor ( ( ) => {
114- expect ( queryClient . result . current . invalidateQueries ) . not . toHaveBeenCalled ( ) ;
121+ expect ( mockSetInvalidateQueries ) . not . toHaveBeenCalled ( ) ;
115122 } ) ;
116123 } ) ;
117124
118125 it ( 'Should not invalidate balance if there are no jobs with cost' , async ( ) => {
119- const queryClient = renderHookWithProviders ( useQueryClient ) ;
120- queryClient . result . current . invalidateQueries = jest . fn ( ) ;
121126 renderHookWithProviders (
122127 ( ) =>
123128 useInvalidateBalanceOnNewJob ( workspaceIdentifier , getMockedResponse ( [ getMockedJob ( ) ] ) , {
124129 jobState : JobState . SCHEDULED ,
125130 } ) ,
126- { providerProps : { featureFlags : { FEATURE_FLAG_CREDIT_SYSTEM : true } } }
131+ {
132+ wrapper,
133+ providerProps : { featureFlags : { FEATURE_FLAG_CREDIT_SYSTEM : true } } ,
134+ }
127135 ) ;
128136
129137 await waitFor ( ( ) => {
130- expect ( queryClient . result . current . invalidateQueries ) . not . toHaveBeenCalled ( ) ;
138+ expect ( mockSetInvalidateQueries ) . not . toHaveBeenCalled ( ) ;
131139 } ) ;
132140 } ) ;
133141
134142 it ( 'Should invalidate balance if there is a job with new id or a new job' , async ( ) => {
135- let invalidateSpy : jest . SpyInstance | undefined ;
136-
137143 const { rerender } = renderHookWithProviders (
138144 ( { jobs } ) => {
139- const queryClient = useQueryClient ( ) ;
140- invalidateSpy = jest . spyOn ( queryClient , 'invalidateQueries' ) ;
141-
142145 return useInvalidateBalanceOnNewJob ( workspaceIdentifier , getMockedResponse ( jobs ) , {
143146 jobState : JobState . SCHEDULED ,
144147 } ) ;
145148 } ,
146149 {
150+ wrapper,
147151 providerProps : {
148152 featureFlags : { FEATURE_FLAG_CREDIT_SYSTEM : true } ,
149153 } ,
@@ -159,7 +163,7 @@ describe('Use jobs hook utils', () => {
159163 ) ;
160164
161165 await waitFor ( ( ) => {
162- expect ( invalidateSpy ) . toHaveBeenCalledTimes ( 1 ) ;
166+ expect ( mockSetInvalidateQueries ) . toHaveBeenCalledTimes ( 1 ) ;
163167 } ) ;
164168
165169 rerender ( {
@@ -173,7 +177,7 @@ describe('Use jobs hook utils', () => {
173177 } ) ;
174178
175179 await waitFor ( ( ) => {
176- expect ( invalidateSpy ) . toHaveBeenCalledTimes ( 2 ) ;
180+ expect ( mockSetInvalidateQueries ) . toHaveBeenCalledTimes ( 2 ) ;
177181 } ) ;
178182
179183 rerender ( {
@@ -188,7 +192,7 @@ describe('Use jobs hook utils', () => {
188192 } ) ;
189193
190194 await waitFor ( ( ) => {
191- expect ( invalidateSpy ) . toHaveBeenCalledTimes ( 3 ) ;
195+ expect ( mockSetInvalidateQueries ) . toHaveBeenCalledTimes ( 3 ) ;
192196 } ) ;
193197 } ) ;
194198} ) ;
0 commit comments