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

Persist show internal topics switch state #1832

Merged
merged 3 commits into from
May 2, 2022
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
10 changes: 9 additions & 1 deletion kafka-ui-react-app/src/components/Topics/List/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ const List: React.FC<TopicsListProps> = ({
React.useContext(ClusterContext);
const { clusterName } = useParams<{ clusterName: ClusterName }>();
const { page, perPage, pathname } = usePagination();
const [showInternal, setShowInternal] = React.useState<boolean>(true);
const [showInternal, setShowInternal] = React.useState<boolean>(
!localStorage.getItem('hideInternalTopics') && true
);
const [cachedPage, setCachedPage] = React.useState<number | null>(null);
const history = useHistory();

Expand Down Expand Up @@ -125,6 +127,12 @@ const List: React.FC<TopicsListProps> = ({
);

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]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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,
});
});
Expand All @@ -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');
Expand Down