diff --git a/src/components/SearchResultEntry/SearchResultEntry.tsx b/src/components/SearchResultEntry/SearchResultEntry.tsx index 305b9c17..c4683554 100644 --- a/src/components/SearchResultEntry/SearchResultEntry.tsx +++ b/src/components/SearchResultEntry/SearchResultEntry.tsx @@ -1,4 +1,4 @@ -import { FC, useEffect, useState } from 'react'; +import { FC, useState } from 'react'; import { Link } from 'react-router-dom'; import classNames from 'classnames'; import { FormattedMessage, useIntl } from 'react-intl'; @@ -58,7 +58,7 @@ const instancesListHeader: Row = { export const SearchResultEntry: FC = ({ instances, ...restOfWork }) => { const { formatMessage } = useIntl(); const { navigateToEditPage } = useNavigateToEditPage(); - const { navigationState, selectedInstances, setSelectedInstances, resetSelectedInstances } = useSearchState(); + const { navigationState, selectedInstances, setSelectedInstances } = useSearchState(); const [isOpen, setIsOpen] = useState(true); const { setIsLoading } = useLoadingState(); const { addStatusMessagesItem } = useStatusState(); @@ -67,13 +67,6 @@ export const SearchResultEntry: FC = ({ instances, ...restOfW const toggleIsOpen = () => setIsOpen(!isOpen); const { fetchRecord } = useRecordControls(); - useEffect(() => { - return () => { - resetFullDisplayComponentType(); - resetSelectedInstances(); - }; - }, []); - const handleOpenPreview = async (id: string) => { try { setIsLoading(true); diff --git a/src/test/__tests__/components/SearchResultEntry.test.tsx b/src/test/__tests__/components/SearchResultEntry.test.tsx index 59142add..f80934f0 100644 --- a/src/test/__tests__/components/SearchResultEntry.test.tsx +++ b/src/test/__tests__/components/SearchResultEntry.test.tsx @@ -1,7 +1,5 @@ import { fireEvent, render, screen } from '@testing-library/react'; import { BrowserRouter } from 'react-router-dom'; -import { setInitialGlobalState } from '@src/test/__mocks__/store'; -import { useSearchStore, useUIStore } from '@src/store'; import { SearchResultEntry } from '@components/SearchResultEntry'; import { itemSearchMockData } from './ItemSearch.test'; @@ -66,37 +64,4 @@ describe('SearchResultEntry', () => { expect(getByText('ld.noInstancesAvailable')).toBeInTheDocument(); }); }); - - describe('component unmount', () => { - test('clears fullDisplayComponentType and selectedInstances on unmount', () => { - const mockResetFullDisplayComponentType = jest.fn(); - const mockResetSelectedInstances = jest.fn(); - - setInitialGlobalState([ - { - store: useUIStore, - state: { - resetFullDisplayComponentType: mockResetFullDisplayComponentType, - }, - }, - { - store: useSearchStore, - state: { - resetSelectedInstances: mockResetSelectedInstances, - }, - }, - ]); - - const { unmount } = render( - - - , - ); - - unmount(); - - expect(mockResetFullDisplayComponentType).toHaveBeenCalled(); - expect(mockResetSelectedInstances).toHaveBeenCalled(); - }); - }); }); diff --git a/src/test/__tests__/views/Search.test.tsx b/src/test/__tests__/views/Search.test.tsx index fde20ce5..fa557074 100644 --- a/src/test/__tests__/views/Search.test.tsx +++ b/src/test/__tests__/views/Search.test.tsx @@ -1,6 +1,8 @@ import '@src/test/__mocks__/common/helpers/pageScrolling.helper.mock'; import { render, screen } from '@testing-library/react'; import { BrowserRouter } from 'react-router-dom'; +import { setInitialGlobalState } from '@src/test/__mocks__/store'; +import { useSearchStore, useUIStore } from '@src/store'; import { Search } from '@views'; jest.mock('@common/constants/build.constants', () => ({ IS_EMBEDDED_MODE: false })); @@ -21,4 +23,37 @@ describe('Search', () => { test('renders child ItemSearch component', () => { expect(screen.getByTestId('id-search')).toBeInTheDocument(); }); + + describe('component unmount', () => { + test('clears fullDisplayComponentType and selectedInstances on unmount', () => { + const mockResetFullDisplayComponentType = jest.fn(); + const mockResetSelectedInstances = jest.fn(); + + setInitialGlobalState([ + { + store: useUIStore, + state: { + resetFullDisplayComponentType: mockResetFullDisplayComponentType, + }, + }, + { + store: useSearchStore, + state: { + resetSelectedInstances: mockResetSelectedInstances, + }, + }, + ]); + + const { unmount } = render( + + + , + ); + + unmount(); + + expect(mockResetFullDisplayComponentType).toHaveBeenCalled(); + expect(mockResetSelectedInstances).toHaveBeenCalled(); + }); + }); }); diff --git a/src/views/Search/Search.tsx b/src/views/Search/Search.tsx index f892991d..97243d59 100644 --- a/src/views/Search/Search.tsx +++ b/src/views/Search/Search.tsx @@ -1,4 +1,4 @@ -import { useCallback, useMemo } from 'react'; +import { useCallback, useEffect, useMemo } from 'react'; import { FormattedMessage } from 'react-intl'; import { Search } from '@components/Search'; import { SearchResultList } from '@components/SearchResultList'; @@ -24,20 +24,27 @@ import './Search.scss'; export const SearchView = () => { const { navigateToEditPage } = useNavigateToEditPage(); const { dispatchDropNavigateToOriginEvent } = useContainerEvents(); - const { selectedInstances } = useSearchState(); + const { selectedInstances, resetSelectedInstances } = useSearchState(); const { setIsLoading } = useLoadingState(); const { fetchRecord } = useRecordControls(); const { addStatusMessagesItem } = useStatusState(); - const { setFullDisplayComponentType } = useUIState(); + const { setFullDisplayComponentType, resetFullDisplayComponentType } = useUIState(); const { resetPreviewContent } = useInputsState(); - + + useEffect(() => { + return () => { + resetFullDisplayComponentType(); + resetSelectedInstances(); + }; + }, []); + dispatchDropNavigateToOriginEvent(); const handlePreviewMultiple = async () => { try { setIsLoading(true); resetPreviewContent(); - setFullDisplayComponentType(FullDisplayType.Comparison) + setFullDisplayComponentType(FullDisplayType.Comparison); for (const id of selectedInstances.toReversed()) { await fetchRecord(id, {});