Skip to content

Commit c29dd45

Browse files
committed
UIFC-437-use-generic-hooks undo more for specific requests
1 parent 3aecc5f commit c29dd45

3 files changed

Lines changed: 58 additions & 37 deletions

File tree

src/components/MetadataCollections/CollectionInfo/CollectionInfoView.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ const CollectionInfoView = ({ metadataCollection, stripes }) => {
2222
}
2323
};
2424

25-
console.log(metadataCollection);
26-
2725
const collectionId = get(metadataCollection, 'id', '-');
2826
const selectedInitial = get(metadataCollection, 'selected');
2927
const permittedLabel = getDataLable('permitted');

src/settings/CredentialsSettings.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1+
import { useQuery, useMutation } from 'react-query';
12
import { FormattedMessage } from 'react-intl';
23

4+
import { useOkapiKy } from '@folio/stripes/core';
35
import { Loading, MessageBanner } from '@folio/stripes/components';
4-
import {
5-
useOkapiKyMutation,
6-
useOkapiKyQuery,
7-
} from '@folio/stripes-leipzig-components';
8-
9-
import {
10-
API_EZB_CREDENTIALS,
11-
QK_EZB_CREDENTIALS,
12-
} from '../util/constants';
6+
7+
import { API_EZB_CREDENTIALS } from '../util/constants';
138
import CredentialsSettingsForm from './CredentialsSettingsForm';
149

1510
const CredentialsSettings = () => {
16-
const { data: credentials = {}, isError, isLoading, refetch } = useOkapiKyQuery({
17-
queryKey: [QK_EZB_CREDENTIALS],
18-
api: API_EZB_CREDENTIALS,
11+
const ky = useOkapiKy();
12+
13+
const { data: credentials, error, isLoading, refetch } = useQuery({
14+
queryKey: ['ezbCredentials'],
15+
queryFn: async () => {
16+
const res = await ky.get(API_EZB_CREDENTIALS).json();
17+
return res ?? {};
18+
}
1919
});
2020

21-
const { useUpdate } = useOkapiKyMutation({
22-
mutationKey: [QK_EZB_CREDENTIALS],
23-
api: API_EZB_CREDENTIALS,
21+
const { mutate: updateCredentials } = useMutation({
22+
mutationFn: async (values) => {
23+
return ky.put(API_EZB_CREDENTIALS, { json: values });
24+
},
2425
onSuccess: () => {
2526
refetch();
2627
}
2728
});
2829

29-
const { mutateAsync: updateCredentials } = useUpdate();
30-
3130
const handleSubmit = (values) => {
3231
updateCredentials(values);
3332
};
@@ -36,7 +35,7 @@ const CredentialsSettings = () => {
3635
return <Loading />;
3736
}
3837

39-
if (isError) {
38+
if (error) {
4039
return (
4140
<MessageBanner type="error">
4241
<FormattedMessage id="ui-finc-select.settings.ezbCredentials.error" />

src/settings/CredentialsSettings.test.js

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,36 @@ import { QueryClient, QueryClientProvider } from 'react-query';
22
import { MemoryRouter } from 'react-router-dom';
33

44
import { render, screen, waitFor } from '@folio/jest-config-stripes/testing-library/react';
5-
import { useOkapiKyQuery } from '@folio/stripes-leipzig-components';
65

76
import CredentialsSettings from './CredentialsSettings';
87

98
jest.mock('./CredentialsSettingsForm', () => () => <div>CredentialsSettingsForm</div>);
109

11-
jest.mock('@folio/stripes-leipzig-components', () => ({
12-
...jest.requireActual('@folio/stripes-leipzig-components'),
10+
jest.mock('@folio/stripes/components', () => ({
11+
...jest.requireActual('@folio/stripes/components'),
1312
useOkapiKyQuery: jest.fn(),
13+
Loading: () => <div>Loading</div>,
14+
}));
15+
16+
const mockUseQuery = jest.fn();
17+
const mockKyGet = jest.fn();
18+
19+
jest.mock('react-query', () => {
20+
const actual = jest.requireActual('react-query');
21+
return {
22+
...actual,
23+
useQuery: (...args) => mockUseQuery(...args),
24+
useMutation: () => ({
25+
mutate: jest.fn(),
26+
}),
27+
};
28+
});
29+
30+
jest.mock('@folio/stripes/core', () => ({
31+
useOkapiKy: () => ({
32+
get: mockKyGet,
33+
put: jest.fn(),
34+
}),
1435
}));
1536

1637
const renderComponent = () => {
@@ -26,26 +47,27 @@ const renderComponent = () => {
2647

2748
describe('CredentialsSettings', () => {
2849
beforeEach(() => {
29-
useOkapiKyQuery.mockReset();
50+
mockUseQuery.mockReset();
51+
mockKyGet.mockReset();
3052
});
3153

3254
test('renders loading state', () => {
33-
useOkapiKyQuery.mockReturnValue({
55+
mockUseQuery.mockReturnValue({
3456
data: undefined,
35-
isError: false,
57+
error: null,
3658
isLoading: true,
3759
refetch: jest.fn(),
3860
});
3961

4062
renderComponent();
4163

42-
expect(document.querySelector('.spinner')).toBeInTheDocument();
64+
expect(screen.getByText('Loading')).toBeInTheDocument();
4365
});
4466

4567
test('renders error state', () => {
46-
useOkapiKyQuery.mockReturnValue({
68+
mockUseQuery.mockReturnValue({
4769
data: undefined,
48-
isError: true,
70+
error: { message: 'Network error' },
4971
isLoading: false,
5072
refetch: jest.fn(),
5173
});
@@ -56,9 +78,9 @@ describe('CredentialsSettings', () => {
5678
});
5779

5880
test('renders form when credentials are defined', async () => {
59-
useOkapiKyQuery.mockReturnValue({
81+
mockUseQuery.mockReturnValue({
6082
data: {},
61-
isError: false,
83+
error: null,
6284
isLoading: false,
6385
refetch: jest.fn(),
6486
});
@@ -70,12 +92,14 @@ describe('CredentialsSettings', () => {
7092
});
7193
});
7294

73-
test('renders form when ky.get().json() returns null', async () => {
74-
useOkapiKyQuery.mockReturnValue({
75-
data: null,
76-
isError: false,
77-
isLoading: false,
78-
refetch: jest.fn(),
95+
test('renders form when GET returns null', async () => {
96+
// Use actual useQuery implementation
97+
const { useQuery: actualUseQuery } = jest.requireActual('react-query');
98+
mockUseQuery.mockImplementation(actualUseQuery);
99+
100+
// Mock ky.get().json() to return null
101+
mockKyGet.mockReturnValue({
102+
json: jest.fn().mockResolvedValue(null),
79103
});
80104

81105
renderComponent();

0 commit comments

Comments
 (0)