@@ -2,6 +2,8 @@ import { allExtensionsState } from 'state/navigation/extensionsAtom';
22import { render } from '@testing-library/react' ;
33import { useGetCRbyPath } from '../useGetCRbyPath.js' ;
44import { RecoilRoot } from 'recoil' ;
5+ import { Provider as JotaiProvider } from 'jotai' ;
6+ import { useHydrateAtoms } from 'jotai/utils' ;
57
68let mockNamespaceId = 'namespaceId' ;
79let mockCrds = [ ] ;
@@ -25,21 +27,33 @@ vi.mock('shared/components/Dropdown/Dropdown', () => {
2527 } ;
2628} ) ;
2729
30+ // Component to initialize Jotai atoms with test data
31+ const JotaiHydrator = ( { children, initialValues } ) => {
32+ useHydrateAtoms ( initialValues ) ;
33+ return children ;
34+ } ;
35+
2836const TestComponent = ( ) => {
2937 const value = useGetCRbyPath ( ) ;
3038
3139 return < p data-testid = "value" > { JSON . stringify ( value ) } </ p > ;
3240} ;
3341
42+ const renderWithProviders = mockCrds => {
43+ return render (
44+ < RecoilRoot >
45+ < JotaiProvider >
46+ < JotaiHydrator initialValues = { [ [ allExtensionsState , mockCrds ] ] } >
47+ < TestComponent />
48+ </ JotaiHydrator >
49+ </ JotaiProvider >
50+ </ RecoilRoot > ,
51+ ) ;
52+ } ;
53+
3454describe ( 'useGetCRbyPath' , ( ) => {
3555 it ( 'Returns nothing for an empty list' , ( ) => {
36- const { queryByTestId } = render (
37- < RecoilRoot
38- initializeState = { snapshot => snapshot . set ( allExtensionsState , mockCrds ) }
39- >
40- < TestComponent />
41- </ RecoilRoot > ,
42- ) ;
56+ const { queryByTestId } = renderWithProviders ( mockCrds ) ;
4357
4458 expect ( queryByTestId ( 'value' ) ) . toHaveTextContent ( '' ) ;
4559 } ) ;
@@ -65,13 +79,7 @@ describe('useGetCRbyPath', () => {
6579 } ,
6680 ] ;
6781
68- const { queryByTestId } = render (
69- < RecoilRoot
70- initializeState = { snapshot => snapshot . set ( allExtensionsState , mockCrds ) }
71- >
72- < TestComponent />
73- </ RecoilRoot > ,
74- ) ;
82+ const { queryByTestId } = renderWithProviders ( mockCrds ) ;
7583 expect ( queryByTestId ( 'value' ) ) . toHaveTextContent (
7684 JSON . stringify ( mockCrds [ 1 ] ) ,
7785 ) ;
@@ -99,13 +107,7 @@ describe('useGetCRbyPath', () => {
99107 } ,
100108 ] ;
101109
102- const { queryByTestId } = render (
103- < RecoilRoot
104- initializeState = { snapshot => snapshot . set ( allExtensionsState , mockCrds ) }
105- >
106- < TestComponent />
107- </ RecoilRoot > ,
108- ) ;
110+ const { queryByTestId } = renderWithProviders ( mockCrds ) ;
109111
110112 expect ( queryByTestId ( 'value' ) ) . toHaveTextContent (
111113 JSON . stringify ( mockCrds [ 1 ] ) ,
@@ -133,13 +135,7 @@ describe('useGetCRbyPath', () => {
133135 } ,
134136 ] ;
135137
136- const { queryByTestId } = render (
137- < RecoilRoot
138- initializeState = { snapshot => snapshot . set ( allExtensionsState , mockCrds ) }
139- >
140- < TestComponent />
141- </ RecoilRoot > ,
142- ) ;
138+ const { queryByTestId } = renderWithProviders ( mockCrds ) ;
143139 expect ( queryByTestId ( 'value' ) ) . toHaveTextContent (
144140 JSON . stringify ( mockCrds [ 1 ] ) ,
145141 ) ;
0 commit comments