Skip to content

Commit

Permalink
Highlighted collection will no longer be expanded. Fix #546
Browse files Browse the repository at this point in the history
  • Loading branch information
tnajdek committed Aug 7, 2024
1 parent 1a77c52 commit 47deefa
Show file tree
Hide file tree
Showing 5 changed files with 18,906 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/js/common/event.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const isHighlightKeyDown = ev => {
const isWindows = navigator.appVersion.indexOf("Win") >= 0;
const isWindows = navigator.userAgent.indexOf("Windows") >= 0;
return (isWindows && ev.getModifierState('Control')) ||
(!isWindows && ev.getModifierState('Alt'));
}
Expand Down
2 changes: 1 addition & 1 deletion src/js/component/libraries/collection-tree.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ const CollectionTree = props => {
useEffect(() => {
if(!shallowEqual(highlightedCollections, prevHighlightedCollections)) {
const parentsOfHighlighted = highlightedCollections.map(cKey => {
let parentCKeys = [cKey];
let parentCKeys = [];
do {
cKey = cKey in collections ? collections[cKey].parentCollection : null;
if (cKey) {
Expand Down
63 changes: 62 additions & 1 deletion test/collections.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
import '@testing-library/jest-dom';
import { http, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';
import { findByRole, getByRole, screen, waitFor } from '@testing-library/react';
import { act, findByRole, getByRole, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event'

import { renderWithProviders } from './utils/render';
import { JSONtoState, getPatchedState } from './utils/state';
import { MainZotero } from '../src/js/component/main';
import { applyAdditionalJestTweaks, waitForPosition } from './utils/common';
import stateRaw from './fixtures/state/desktop-test-user-item-view.json';
import stateSearchRaw from './fixtures/state/desktop-test-user-search-phrase-selected.json';
import testuserAddCollection from './fixtures/response/test-user-add-collection.json';

const state = JSONtoState(stateRaw);
const stateSearch = JSONtoState(stateSearchRaw);

describe('Test User: Collections', () => {
const handlers = [];
Expand Down Expand Up @@ -200,4 +202,63 @@ describe('Test User: Collections', () => {
expect(screen.queryByRole('treeitem', { name: 'Board Games' })).toBeInTheDocument();
});

["Windows", "MacOS"].forEach((platform) => {
test(`Should highlight collection in which the item is present on ${platform}`, async () => {
let userAgent, key, wrongKey;
switch (platform) {
case "MacOS":
userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:128.0) Gecko/20100101 Firefox/128.0";
key = "Alt";
wrongKey = "Control";
break;
case "Windows":
userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0";
key = "Control";
wrongKey = "Alt";
break;
}

jest.spyOn(window.navigator, 'userAgent', 'get').mockReturnValue(userAgent);
delete window.location;
window.location = new URL('http://localhost/testuser/search/retriever/titleCreatorYear/items/KBFTPTI4/item-list');

renderWithProviders(<MainZotero />, { preloadedState: stateSearch });
await waitForPosition();
const user = userEvent.setup();

act(() => screen.getByRole('row',
{ name: 'Retriever' }
).focus());


expect(screen.getByRole('treeitem', { name: 'Dogs', expanded: false })).toBeInTheDocument();

await user.keyboard(`{${key}>}`);
// Parent collection is automatically expanded
expect(screen.getByRole('treeitem', { name: 'Dogs', expanded: true })).toBeInTheDocument();
expect(screen.getByRole('treeitem', { name: 'Goldens' }).parentNode).toHaveClass('highlighted');
await user.keyboard(`{/${key}}`);
await user.keyboard(`{${wrongKey}>}`);
expect(screen.getByRole('treeitem', { name: 'Goldens' }).parentNode).not.toHaveClass('highlighted');
await user.keyboard(`{/${wrongKey}}`);
});
});

test("Should not expand highlighted collection", async () => {
jest.spyOn(window.navigator, 'userAgent', 'get').mockReturnValue('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:128.0) Gecko/20100101 Firefox/128.0');
renderWithProviders(<MainZotero />, { preloadedState: state });
await waitForPosition();
const user = userEvent.setup();

act(() => screen.getByRole('row',
{ name: 'Effects of diet restriction on life span and age-related changes in dogs' }
).focus()
);

await user.keyboard(`{Alt>}`);
expect(screen.getByRole('treeitem', { name: 'Dogs', expanded: false })).toBeInTheDocument();
expect(screen.getByRole('treeitem', { name: 'Dogs' }).parentNode).toHaveClass('highlighted');
await user.keyboard(`{Alt>}`);
});

});
Loading

0 comments on commit 47deefa

Please sign in to comment.