From c98d6f5c998cf025ee9ced81af5b32bf51b73b58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 14 Jan 2025 11:39:12 +0100 Subject: [PATCH 1/2] Fix: Don't mutate a Filter instance The Filter instance might be in the redux store and therefore must not be mutated. Always ensure a copy is mutated. --- src/web/hooks/usePageFilter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/web/hooks/usePageFilter.js b/src/web/hooks/usePageFilter.js index c44a5eac31..d6e3c4b3f3 100644 --- a/src/web/hooks/usePageFilter.js +++ b/src/web/hooks/usePageFilter.js @@ -134,7 +134,7 @@ const usePageFilter = ( } if (!returnedFilter.has('rows') && isDefined(rowsPerPage)) { - returnedFilter = returnedFilter.set('rows', rowsPerPage); + returnedFilter = returnedFilter.copy().set('rows', rowsPerPage); } const finishedLoading = From 8dc267720b7afd6644ceae6a3be5749078179c66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 14 Jan 2025 15:53:46 +0100 Subject: [PATCH 2/2] Adjust testcase for usePageFilter hook With the latest change the rows will always be set on a filter which doesn't have rows set yet. This should be correct because the backend always applies a row value to the filter. --- src/web/hooks/__tests__/usePageFilter.jsx | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/web/hooks/__tests__/usePageFilter.jsx b/src/web/hooks/__tests__/usePageFilter.jsx index 095665a595..7ea857e484 100644 --- a/src/web/hooks/__tests__/usePageFilter.jsx +++ b/src/web/hooks/__tests__/usePageFilter.jsx @@ -3,8 +3,6 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ - - import {describe, test, expect, testing} from '@gsa/testing'; import Filter, {DEFAULT_FALLBACK_FILTER} from 'gmp/models/filter'; import {vi} from 'vitest'; @@ -15,7 +13,6 @@ import {rendererWith, waitFor} from 'web/utils/testing'; import usePageFilter from '../usePageFilter'; - const mockUseNavigate = testing.fn(); vi.mock('react-router-dom', async () => { @@ -61,6 +58,7 @@ describe('usePageFilter tests', () => { Filter.fromString('location=query rows=42'), ); }); + test('should prefer pageFilter over defaultSettingFilter', async () => { const pFilter = Filter.fromString('page=filter'); const defaultSettingFilter = Filter.fromString('foo=bar'); @@ -86,7 +84,7 @@ describe('usePageFilter tests', () => { const {result} = renderHook(() => usePageFilter('somePage2', 'somePage')); - expect(result.current[0]).toEqual(pFilter); + expect(result.current[0]).toEqual(pFilter.set('rows', 42)); }); test('should use defaultSettingFilter', async () => {