From f67b2a6905381bb81ab4ec945186e19e7ca008d8 Mon Sep 17 00:00:00 2001
From: Siarhei Karol <skarol@ebsco.com>
Date: Wed, 15 Jan 2025 14:55:42 +0300
Subject: [PATCH 1/2] Comparison: resource is shown in full view when removing
 one out of three from the second page of comparison modal

---
 src/components/Comparison/Comparison.tsx      |  6 +++-
 .../__tests__/components/Comparison.test.tsx  | 30 ++++++++++++++++++-
 2 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/src/components/Comparison/Comparison.tsx b/src/components/Comparison/Comparison.tsx
index e4f6066b..64551493 100644
--- a/src/components/Comparison/Comparison.tsx
+++ b/src/components/Comparison/Comparison.tsx
@@ -29,6 +29,10 @@ export const Comparison = () => {
   };
 
   const handleRemoveComparisonItem = (id: string) => {
+    if (currentPage === totalPages - 1) {
+      setCurrentPage(prev => (prev - 1 >= 0 ? prev - 1 : 0));
+    }
+
     setPreviewContent(prev => prev.filter(({ id: prevId }) => prevId !== id));
     setSelectedInstances(prev => prev.filter(prevId => prevId !== id));
   };
@@ -79,7 +83,7 @@ export const Comparison = () => {
               <div className="entry-header">
                 <div className="entry-header-controls">
                   <Button
-                    data-testid="remove-comparison-entry"
+                    data-testid={`remove-comparison-entry-${id}`}
                     type={ButtonType.Icon}
                     onClick={() => handleRemoveComparisonItem(id)}
                     className="nav-close"
diff --git a/src/test/__tests__/components/Comparison.test.tsx b/src/test/__tests__/components/Comparison.test.tsx
index 576d3d8f..c9dcb027 100644
--- a/src/test/__tests__/components/Comparison.test.tsx
+++ b/src/test/__tests__/components/Comparison.test.tsx
@@ -72,12 +72,40 @@ describe('Comparison', () => {
       },
     ]);
 
-    fireEvent.click(getByTestId('remove-comparison-entry'));
+    fireEvent.click(getByTestId('remove-comparison-entry-mockId'));
 
     expect(setPreviewContent).toHaveBeenCalled();
     expect(setSelectedInstances).toHaveBeenCalled();
   });
 
+  test('updates current page when removing last item on last page', () => {
+    const setPreviewContent = jest.fn();
+    const { getByTestId } = renderWithState([
+      {
+        store: useInputsStore,
+        state: {
+          previewContent: [
+            { id: 'mockId_1', title: 'mockTitle 1' },
+            { id: 'mockId_2', title: 'mockTitle 2' },
+            { id: 'mockId_3', title: 'mockTitle 3' },
+          ],
+          setPreviewContent,
+        },
+      },
+      {
+        store: useSearchStore,
+        state: { setSelectedInstances },
+      },
+    ]);
+
+    fireEvent.click(getByTestId('forward-button'));
+    fireEvent.click(getByTestId('remove-comparison-entry-mockId_3'));
+
+    expect(setPreviewContent).toHaveBeenCalled();
+    expect(setSelectedInstances).toHaveBeenCalled();
+    expect(getByTestId('backward-button')).toBeInTheDocument();
+  });
+
   test('closes comparison', async () => {
     const { getByTestId } = renderWithState([
       {

From 9fcb7b88128d6c452f500b83ccee414e75b36f68 Mon Sep 17 00:00:00 2001
From: Siarhei Karol <skarol@ebsco.com>
Date: Wed, 15 Jan 2025 14:58:29 +0300
Subject: [PATCH 2/2] minor

---
 src/components/Comparison/Comparison.tsx | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/components/Comparison/Comparison.tsx b/src/components/Comparison/Comparison.tsx
index 64551493..693e75a8 100644
--- a/src/components/Comparison/Comparison.tsx
+++ b/src/components/Comparison/Comparison.tsx
@@ -30,7 +30,11 @@ export const Comparison = () => {
 
   const handleRemoveComparisonItem = (id: string) => {
     if (currentPage === totalPages - 1) {
-      setCurrentPage(prev => (prev - 1 >= 0 ? prev - 1 : 0));
+      setCurrentPage(prevValue => {
+        const previousPage = prevValue - 1;
+
+        return previousPage >= 0 ? previousPage : 0;
+      });
     }
 
     setPreviewContent(prev => prev.filter(({ id: prevId }) => prevId !== id));