From 84139989741322c99cb2f708bebd33c7eae8dfa8 Mon Sep 17 00:00:00 2001 From: Arsen Simonyan <103444767+simonyandev@users.noreply.github.com> Date: Mon, 2 May 2022 13:28:23 +0400 Subject: [PATCH] Persist show internal topics switch state (#1832) * persist show internal topics switch state * minor improvements in topics list tests * prevent duplication in topic list test file --- .../src/components/Topics/List/List.tsx | 10 ++++++- .../Topics/List/__tests__/List.spec.tsx | 30 +++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/kafka-ui-react-app/src/components/Topics/List/List.tsx b/kafka-ui-react-app/src/components/Topics/List/List.tsx index 7431dabf63..888fee7677 100644 --- a/kafka-ui-react-app/src/components/Topics/List/List.tsx +++ b/kafka-ui-react-app/src/components/Topics/List/List.tsx @@ -82,7 +82,9 @@ const List: React.FC = ({ React.useContext(ClusterContext); const { clusterName } = useParams<{ clusterName: ClusterName }>(); const { page, perPage, pathname } = usePagination(); - const [showInternal, setShowInternal] = React.useState(true); + const [showInternal, setShowInternal] = React.useState( + !localStorage.getItem('hideInternalTopics') && true + ); const [cachedPage, setCachedPage] = React.useState(null); const history = useHistory(); @@ -141,6 +143,12 @@ const List: React.FC = ({ }; const handleSwitch = React.useCallback(() => { + if (showInternal) { + localStorage.setItem('hideInternalTopics', 'true'); + } else { + localStorage.removeItem('hideInternalTopics'); + } + setShowInternal(!showInternal); history.push(`${pathname}?page=1&perPage=${perPage || PER_PAGE}`); }, [history, pathname, perPage, showInternal]); diff --git a/kafka-ui-react-app/src/components/Topics/List/__tests__/List.spec.tsx b/kafka-ui-react-app/src/components/Topics/List/__tests__/List.spec.tsx index 0bdf6e16cc..039be9ad65 100644 --- a/kafka-ui-react-app/src/components/Topics/List/__tests__/List.spec.tsx +++ b/kafka-ui-react-app/src/components/Topics/List/__tests__/List.spec.tsx @@ -76,11 +76,13 @@ describe('List', () => { describe('when it does not have readonly flag', () => { let fetchTopicsList = jest.fn(); let component: ReactWrapper; + const internalTopicsSwitchName = 'input[name="ShowInternalTopics"]'; jest.useFakeTimers(); beforeEach(() => { fetchTopicsList = jest.fn(); + component = mountComponentWithProviders( { isReadOnly: false }, { fetchTopicsList } @@ -100,13 +102,35 @@ describe('List', () => { expect(setTopicsSearch).toHaveBeenCalledWith(query); }); + it('show internal toggle state should be true if user has not used it yet', () => { + const toggle = component.find(internalTopicsSwitchName); + const { checked } = toggle.props(); + + expect(checked).toEqual(true); + }); + + it('show internal toggle state should match user preference', () => { + localStorage.setItem('hideInternalTopics', 'true'); + component = mountComponentWithProviders( + { isReadOnly: false }, + { fetchTopicsList } + ); + + const toggle = component.find(internalTopicsSwitchName); + const { checked } = toggle.props(); + + expect(checked).toEqual(false); + }); + it('should refetch topics on show internal toggle change', () => { jest.clearAllMocks(); - const toggle = component.find('input[name="ShowInternalTopics"]'); + const toggle = component.find(internalTopicsSwitchName); + const { checked } = toggle.props(); toggle.simulate('change'); + expect(fetchTopicsList).toHaveBeenLastCalledWith({ search: '', - showInternal: false, + showInternal: !checked, sortOrder: SortOrder.ASC, }); }); @@ -120,7 +144,7 @@ describe('List', () => { mockedHistory ); - const toggle = component.find('input[name="ShowInternalTopics"]'); + const toggle = component.find(internalTopicsSwitchName); toggle.simulate('change'); expect(mockedHistory.push).toHaveBeenCalledWith('/?page=1&perPage=25');