Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UILD-468: Comparison fix - close comparison modal and clear selected checkboxes on Search page unmount #75

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading