Skip to content

Commit

Permalink
Close comparison modal and clear selected checkboxes on Search page u…
Browse files Browse the repository at this point in the history
…nmount
  • Loading branch information
SKarolFolio committed Jan 14, 2025
1 parent a17f056 commit dbcbe48
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 49 deletions.
11 changes: 2 additions & 9 deletions src/components/SearchResultEntry/SearchResultEntry.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -58,7 +58,7 @@ const instancesListHeader: Row = {
export const SearchResultEntry: FC<SearchResultEntry> = ({ 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();
Expand All @@ -67,13 +67,6 @@ export const SearchResultEntry: FC<SearchResultEntry> = ({ instances, ...restOfW
const toggleIsOpen = () => setIsOpen(!isOpen);
const { fetchRecord } = useRecordControls();

useEffect(() => {
return () => {
resetFullDisplayComponentType();
resetSelectedInstances();
};
}, []);

const handleOpenPreview = async (id: string) => {
try {
setIsLoading(true);
Expand Down
35 changes: 0 additions & 35 deletions src/test/__tests__/components/SearchResultEntry.test.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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(
<BrowserRouter>
<SearchResultEntry {...(mockProps as unknown as WorkAsSearchResultDTO)} />
</BrowserRouter>,
);

unmount();

expect(mockResetFullDisplayComponentType).toHaveBeenCalled();
expect(mockResetSelectedInstances).toHaveBeenCalled();
});
});
});
35 changes: 35 additions & 0 deletions src/test/__tests__/views/Search.test.tsx
Original file line number Diff line number Diff line change
@@ -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 }));
Expand All @@ -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(
<BrowserRouter>
<Search />
</BrowserRouter>,
);

unmount();

expect(mockResetFullDisplayComponentType).toHaveBeenCalled();
expect(mockResetSelectedInstances).toHaveBeenCalled();
});
});
});
17 changes: 12 additions & 5 deletions src/views/Search/Search.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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, {});
Expand Down

0 comments on commit dbcbe48

Please sign in to comment.