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

fix(highlight): Prevent changing staged during creator pending #645

Merged
merged 2 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions src/highlight/HighlightAnnotations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Props = {
annotations: AnnotationHighlight[];
createHighlight?: (arg: CreateArg) => void;
isCreating: boolean;
isPending: boolean;
isPromoting: boolean;
isSelecting: boolean;
location: number;
Expand All @@ -34,6 +35,7 @@ const HighlightAnnotations = (props: Props): JSX.Element => {
activeAnnotationId,
annotations = [],
isCreating = false,
isPending = false,
isPromoting = false,
isSelecting = false,
selection,
Expand Down Expand Up @@ -83,7 +85,7 @@ const HighlightAnnotations = (props: Props): JSX.Element => {
};

React.useEffect(() => {
if (!isSelecting) {
if (!isSelecting || isPending) {
return;
}

Expand All @@ -92,7 +94,7 @@ const HighlightAnnotations = (props: Props): JSX.Element => {
}, [isSelecting]); // eslint-disable-line react-hooks/exhaustive-deps
mxiao6 marked this conversation as resolved.
Show resolved Hide resolved

React.useEffect(() => {
if (!isCreating || !selection || selection.hasError) {
if (!isCreating || !selection || selection.hasError || isPending) {
return;
}

Expand Down
4 changes: 4 additions & 0 deletions src/highlight/HighlightContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { AnnotationHighlight } from '../@types';
import {
AppState,
CreatorItemHighlight,
CreatorStatus,
getActiveAnnotationId,
getAnnotationMode,
getAnnotationsForLocation,
getCreatorStagedForLocation,
getCreatorStatus,
getIsPromoting,
getIsSelecting,
getSelectionForLocation,
Expand All @@ -28,6 +30,7 @@ export type Props = {
activeAnnotationId: string | null;
annotations: AnnotationHighlight[];
isCreating: boolean;
isPending: boolean;
isPromoting: boolean;
isSelecting: boolean;
selection: SelectionItem | null;
Expand All @@ -41,6 +44,7 @@ export const mapStateToProps = (state: AppState, { location }: { location: numbe
activeAnnotationId: getActiveAnnotationId(state),
annotations: getAnnotationsForLocation(state, location).filter(isHighlight),
isCreating: getAnnotationMode(state) === Mode.HIGHLIGHT,
isPending: getCreatorStatus(state) === CreatorStatus.pending,
isPromoting: getIsPromoting(state),
isSelecting: getIsSelecting(state),
selection: getSelectionForLocation(state, location),
Expand Down
13 changes: 13 additions & 0 deletions src/highlight/__tests__/HighlightAnnotations-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('HighlightAnnotations', () => {
activeAnnotationId: null,
annotations: [],
isCreating: false,
isPending: false,
isPromoting: false,
isSelecting: false,
location: 1,
Expand Down Expand Up @@ -176,6 +177,12 @@ describe('HighlightAnnotations', () => {
expect(defaults.setStaged).not.toHaveBeenCalled();
expect(defaults.setStatus).not.toHaveBeenCalled();
});

test('should not reset staged and status if isPending is true', () => {
getWrapper({ isSelecting: true, isPending: true });
expect(defaults.setStaged).not.toHaveBeenCalled();
expect(defaults.setStatus).not.toHaveBeenCalled();
});
});

describe('Creating a highlight', () => {
Expand All @@ -193,6 +200,12 @@ describe('HighlightAnnotations', () => {
},
);

test('should not call setStaged and setStatus if isPending is true', () => {
getWrapper({ isCreating: true, selection: {}, isPending: true });
expect(defaults.setStaged).not.toHaveBeenCalled();
expect(defaults.setStatus).not.toHaveBeenCalled();
});

test('should call setStaged and setStatus if isCreating=true and selection is not null', () => {
getWrapper({ isCreating: true, selection: selectionMock });
expect(defaults.setStaged).toHaveBeenCalledWith({
Expand Down
14 changes: 13 additions & 1 deletion src/highlight/__tests__/HighlightContainer-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IntlShape } from 'react-intl';
import { ReactWrapper, mount } from 'enzyme';
import HighlightAnnotations from '../HighlightAnnotations';
import HighlightContainer, { Props } from '../HighlightContainer';
import { createStore, CreatorItemHighlight, CreatorItemRegion, Mode } from '../../store';
import { createStore, CreatorItemHighlight, CreatorItemRegion, CreatorStatus, Mode } from '../../store';
import { rect as highlightRect } from '../__mocks__/data';
import { rect as regionRect } from '../../region/__mocks__/data';

Expand Down Expand Up @@ -62,6 +62,18 @@ describe('HighlightContainer', () => {
expect(wrapper.find(HighlightAnnotations).prop('isCreating')).toEqual(isCreating);
});

test.each`
status | isPending
${CreatorStatus.init} | ${false}
${CreatorStatus.pending} | ${true}
${CreatorStatus.staged} | ${false}
`('should pass down isPending as $isPending if status is $status', ({ isPending, status }) => {
const store = createStore({ creator: { status } });
const wrapper = getWrapper({ store });

expect(wrapper.find(HighlightAnnotations).prop('isPending')).toEqual(isPending);
});

test.each`
staged | expectedStaged
${stagedHighlight} | ${stagedHighlight}
Expand Down