Skip to content

Commit

Permalink
Merge branch 'master' into UIBULKED-497
Browse files Browse the repository at this point in the history
  • Loading branch information
UladzislauKutarkin authored Oct 25, 2024
2 parents eab4591 + ac6c7d9 commit b6576ac
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
import { useSearchParams } from '../../../../hooks';
import { RootContext } from '../../../../context/RootContext';
import { getVisibleColumnsKeys } from '../../../../utils/helpers';
import { useErrorMessages } from '../../../../hooks/useErrorMessages';


export const BulkEditPreviewModalList = ({
Expand All @@ -45,6 +46,7 @@ export const BulkEditPreviewModalList = ({
const intl = useIntl();
const { visibleColumns } = useContext(RootContext);
const { currentRecordType } = useSearchParams();
const { showErrorMessage } = useErrorMessages();
const {
pagination,
changePage,
Expand Down Expand Up @@ -75,6 +77,8 @@ export const BulkEditPreviewModalList = ({
onSuccess: () => setShouldRefetchStatus(false),
queryOptions: {
enabled: shouldFetch,
enabled: isPreviewEnabled && bulkDetails?.status !== JOB_STATUSES.DATA_MODIFICATION_IN_PROGRESS,

Check failure on line 80 in src/components/BulkEditPane/BulkEditListResult/BulkEditInAppPreviewModal/BulkEditPreviewModalList.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Duplicate key 'enabled'

Check failure on line 80 in src/components/BulkEditPane/BulkEditListResult/BulkEditInAppPreviewModal/BulkEditPreviewModalList.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Duplicate key 'enabled'
onSuccess: showErrorMessage,
onError: () => {
callout({
type: 'error',
Expand Down
27 changes: 27 additions & 0 deletions src/hooks/api/useBulkOperationDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { useQuery } from 'react-query';
import { useNamespace, useOkapiKy } from '@folio/stripes/core';
import { useHistory } from 'react-router-dom';
import { buildSearch } from '@folio/stripes-acq-components';
import { JOB_STATUSES } from '../../constants';
import { useErrorMessages } from '../useErrorMessages';

export const BULK_OPERATION_DETAILS_KEY = 'BULK_OPERATION_DETAILS_KEY';

Expand Down Expand Up @@ -44,6 +46,31 @@ export const useBulkOperationDetails = ({
});
};

const { data, isLoading } = useQuery({
queryKey: [BULK_OPERATION_DETAILS_KEY, namespaceKey, id, refetchInterval, ...additionalQueryKeys],
enabled: !!id,
refetchInterval,
queryFn: async () => {
try {
const response = await ky.get(`bulk-operations/${id}`).json();

showErrorMessage(response);

if (response.status === JOB_STATUSES.FAILED || response?.errorMessage) {
clearInterval();
}

return response;
} catch (e) {
showErrorMessage(e);
clearInterval();

return e;
}
},
...options,
});

return {
bulkDetails: data,
isLoading,
Expand Down
82 changes: 82 additions & 0 deletions src/hooks/api/useBulkOperationDetails.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { renderHook, act } from '@testing-library/react-hooks';
import { useQuery } from 'react-query';
import { useNamespace, useOkapiKy } from '@folio/stripes/core';
import { useHistory } from 'react-router-dom';
import { useBulkOperationDetails, BULK_OPERATION_DETAILS_KEY } from './useBulkOperationDetails';
import { useErrorMessages } from '../useErrorMessages';


jest.mock('@folio/stripes-acq-components', () => ({
buildSearch: jest.fn(),
}));

jest.mock('react-query', () => ({
useQuery: jest.fn(),
}));

jest.mock('@folio/stripes/core', () => ({
useNamespace: jest.fn(),
useOkapiKy: jest.fn(),
}));

jest.mock('react-router-dom', () => ({
useHistory: jest.fn(),
}));

jest.mock('../useErrorMessages', () => ({
useErrorMessages: jest.fn(),
}));

describe('useBulkOperationDetails', () => {
let kyMock;
let historyMock;
let showErrorMessageMock;

beforeEach(() => {
kyMock = { get: jest.fn() };
historyMock = { replace: jest.fn(), location: { search: '' } };
showErrorMessageMock = jest.fn();

useOkapiKy.mockReturnValue(kyMock);
useNamespace.mockReturnValue(['namespace-key']);
useHistory.mockReturnValue(historyMock);
useErrorMessages.mockReturnValue({ showErrorMessage: showErrorMessageMock });

// Set a default implementation for useQuery to prevent infinite renders
useQuery.mockReturnValue({ data: null, isLoading: false });
jest.clearAllMocks();
});

it('should initialize with the correct refetch interval and query key', () => {
const id = 'test-id';
const refetchInterval = 5000;

const { result } = renderHook(() => useBulkOperationDetails({ id, interval: refetchInterval }));

expect(result.current.isLoading).toBe(false);
expect(useQuery).toHaveBeenCalledWith(
expect.objectContaining({
queryKey: [BULK_OPERATION_DETAILS_KEY, 'namespace-key', id, refetchInterval],
refetchInterval,
enabled: true,
})
);
});

it('should clear interval and redirect when clearIntervalAndRedirect is called', () => {
const id = 'test-id';
const pathname = '/new-path';
const searchParams = { query: 'test' };

const { result } = renderHook(() => useBulkOperationDetails({ id }));

act(() => {
result.current.clearIntervalAndRedirect(pathname, searchParams);
});

expect(historyMock.replace).toHaveBeenCalledWith({
pathname,
search: undefined,
});
});
});
4 changes: 1 addition & 3 deletions src/hooks/useErrorMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ export const useErrorMessages = () => {
};

const showErrorMessage = (res) => {
const messageInBody = res?.errorMessage;
const messageWhenError = res?.message;
const message = messageInBody || messageWhenError;
const message = res?.errorMessage;

// check if error message should be translated (if it's exist in translations)
const prefixedMessageId = `ui-bulk-edit.${message}`;
Expand Down
12 changes: 0 additions & 12 deletions src/hooks/useErrorMessages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,6 @@ describe('useErrorMessages', () => {
expect(formatMessageMock).not.toHaveBeenCalled();
});

it('should show the message from the response when no error message in body is available', () => {
const { result } = renderHook(() => useErrorMessages());
const { showErrorMessage } = result.current;

showErrorMessage({ message: 'Another error occurred' });

expect(showCalloutMock).toHaveBeenCalledWith({
type: 'error',
message: 'Another error occurred',
});
});

it('should show the default error message when the response is an error object without a message', () => {
const { result } = renderHook(() => useErrorMessages());
const { showErrorMessage } = result.current;
Expand Down

0 comments on commit b6576ac

Please sign in to comment.