Skip to content

Commit 7c8ee0a

Browse files
authored
chore: migrate allextensionsstate to jotai (#4120)
1 parent c4966ad commit 7c8ee0a

File tree

3 files changed

+40
-42
lines changed

3 files changed

+40
-42
lines changed

src/components/Extensibility/tests/useGetCRDByPath.test.js

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { allExtensionsState } from 'state/navigation/extensionsAtom';
22
import { render } from '@testing-library/react';
33
import { useGetCRbyPath } from '../useGetCRbyPath.js';
44
import { RecoilRoot } from 'recoil';
5+
import { Provider as JotaiProvider } from 'jotai';
6+
import { useHydrateAtoms } from 'jotai/utils';
57

68
let mockNamespaceId = 'namespaceId';
79
let 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+
2836
const 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+
3454
describe('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
);

src/components/Extensibility/useGetCRbyPath.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { columnLayoutState } from 'state/columnLayoutAtom';
1010

1111
export const useGetCRbyPath = resourceType => {
1212
const { namespaceId } = useParams();
13-
const extensions = useRecoilValue(allExtensionsState);
13+
const extensions = useAtomValue(allExtensionsState);
1414
const { name: clusterName } = useRecoilValue(clusterState) || {};
1515
const layoutState = useAtomValue(columnLayoutState);
1616
const resource = useMemo(() => {

src/state/navigation/extensionsAtom.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import jsyaml from 'js-yaml';
33
import { mapValues, partial } from 'lodash';
44
import { useEffect } from 'react';
55
import { ExtInjectionConfig, ExtResource } from '../types';
6-
import { atom, RecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
6+
import { atom, useSetAtom } from 'jotai';
7+
import {
8+
atom as recoilAtom,
9+
RecoilState,
10+
useRecoilValue,
11+
useSetRecoilState,
12+
} from 'recoil';
713
import { clusterState } from '../clusterAtom';
814
import { authDataState } from '../authDataAtom';
915
import { getFetchFn } from '../utils/getFetchFn';
@@ -404,7 +410,7 @@ export const useGetExtensions = () => {
404410
const auth = useRecoilValue(authDataState);
405411
const setExtensions = useSetRecoilState(extensionsState);
406412
const setStatics = useSetRecoilState(staticsState);
407-
const setAllExtensions = useSetRecoilState(allExtensionsState);
413+
const setAllExtensions = useSetAtom(allExtensionsState);
408414
const setInjections = useSetRecoilState(injectionsState);
409415
const setWizard = useSetRecoilState(wizardState);
410416
const fetchFn = getFetchFn(useRecoilValue);
@@ -573,35 +579,31 @@ export const useGetExtensions = () => {
573579
// null for defaultValue,
574580
// empty array for value or error
575581
const defaultValue = null;
576-
export const extensionsState: RecoilState<ExtResource[] | null> = atom<
582+
export const extensionsState: RecoilState<ExtResource[] | null> = recoilAtom<
577583
ExtResource[] | null
578584
>({
579585
key: 'extensionsState',
580586
default: defaultValue,
581587
});
582588

583-
export const staticsState: RecoilState<ExtResource[] | null> = atom<
589+
export const staticsState: RecoilState<ExtResource[] | null> = recoilAtom<
584590
ExtResource[] | null
585591
>({
586592
key: 'staticsState',
587593
default: defaultValue,
588594
});
589595

590-
export const allExtensionsState: RecoilState<ExtResource[] | null> = atom<
591-
ExtResource[] | null
592-
>({
593-
key: 'allExtensionsState',
594-
default: defaultValue,
595-
});
596+
export const allExtensionsState = atom<ExtResource[] | null>(defaultValue);
597+
allExtensionsState.debugLabel = 'allExtensionsState';
596598

597-
export const injectionsState: RecoilState<ExtInjectionConfig[] | null> = atom<
599+
export const injectionsState: RecoilState<
598600
ExtInjectionConfig[] | null
599-
>({
601+
> = recoilAtom<ExtInjectionConfig[] | null>({
600602
key: 'injectionsState',
601603
default: defaultValue,
602604
});
603605

604-
export const wizardState: RecoilState<ExtWizardConfig[] | null> = atom<
606+
export const wizardState: RecoilState<ExtWizardConfig[] | null> = recoilAtom<
605607
ExtWizardConfig[] | null
606608
>({
607609
key: 'wizardState',

0 commit comments

Comments
 (0)