Skip to content

Commit d59c012

Browse files
authored
fix(discoverability): add reset logic to handleStop (#602)
1 parent 92ba5e7 commit d59c012

File tree

7 files changed

+62
-42
lines changed

7 files changed

+62
-42
lines changed

src/region/RegionAnnotations.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Props = {
1414
isDiscoverabilityEnabled: boolean;
1515
isRotated: boolean;
1616
location: number;
17+
resetCreator: () => void;
1718
setActiveAnnotationId: (annotationId: string | null) => void;
1819
setReferenceShape: (rect: DOMRect) => void;
1920
setStaged: (staged: CreatorItemRegion | null) => void;
@@ -45,6 +46,11 @@ export default class RegionAnnotations extends React.PureComponent<Props, State>
4546
}
4647
}
4748

49+
handleAbort = (): void => {
50+
const { resetCreator } = this.props;
51+
resetCreator();
52+
};
53+
4854
handleAnnotationActive = (annotationId: string | null): void => {
4955
const { setActiveAnnotationId } = this.props;
5056

@@ -74,6 +80,7 @@ export default class RegionAnnotations extends React.PureComponent<Props, State>
7480
className={classNames('ba-RegionAnnotations-creator', {
7581
'is-discoverability-enabled': isDiscoverabilityEnabled,
7682
})}
83+
onAbort={this.handleAbort}
7784
onStart={this.handleStart}
7885
onStop={this.handleStop}
7986
/>

src/region/RegionContainer.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
isCreatorStagedRegion,
1212
isFeatureEnabled,
1313
Mode,
14+
resetCreatorAction,
1415
setActiveAnnotationIdAction,
1516
setReferenceShapeAction,
1617
setStagedAction,
@@ -43,6 +44,7 @@ export const mapStateToProps = (state: AppState, { location }: { location: numbe
4344
};
4445

4546
export const mapDispatchToProps = {
47+
resetCreator: resetCreatorAction,
4648
setActiveAnnotationId: setActiveAnnotationIdAction,
4749
setReferenceShape: setReferenceShapeAction,
4850
setStaged: setStagedAction,

src/region/RegionCreator.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import './RegionCreator.scss';
1010

1111
type Props = {
1212
className?: string;
13+
onAbort: () => void;
1314
onStart: () => void;
1415
onStop: (shape: Rect) => void;
1516
};
@@ -18,7 +19,7 @@ const MIN_X = 0; // Minimum region x position must be positive
1819
const MIN_Y = 0; // Minimum region y position must be positive
1920
const MIN_SIZE = 10; // Minimum region size must be large enough to be clickable
2021

21-
export default function RegionCreator({ className, onStart, onStop }: Props): JSX.Element {
22+
export default function RegionCreator({ className, onAbort, onStart, onStop }: Props): JSX.Element {
2223
const [isDrawing, setIsDrawing] = React.useState<boolean>(false);
2324
const [isHovering, setIsHovering] = React.useState<boolean>(false);
2425
const creatorElRef = React.useRef<HTMLDivElement>(null);
@@ -109,6 +110,8 @@ export default function RegionCreator({ className, onStart, onStop }: Props): JS
109110

110111
if (shape) {
111112
onStop(shape);
113+
} else {
114+
onAbort();
112115
}
113116
};
114117
const updateDraw = (x: number, y: number): void => {

src/region/__tests__/RegionAnnotations-test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe('RegionAnnotations', () => {
1818
const defaults = {
1919
activeAnnotationId: null,
2020
location: 1,
21+
resetCreator: jest.fn(),
2122
setActiveAnnotationId: jest.fn(),
2223
setMessage: jest.fn(),
2324
setReferenceShape: jest.fn(),
@@ -49,6 +50,14 @@ describe('RegionAnnotations', () => {
4950
instance = wrapper.instance() as InstanceType<typeof RegionAnnotations>;
5051
});
5152

53+
describe('handleAbort', () => {
54+
test('should call resetCreator', () => {
55+
instance.handleAbort();
56+
57+
expect(defaults.resetCreator).toHaveBeenCalled();
58+
});
59+
});
60+
5261
describe('handleStart', () => {
5362
test('should reset the creator status', () => {
5463
instance.handleStart();
@@ -160,6 +169,7 @@ describe('RegionAnnotations', () => {
160169
/>
161170
<RegionCreator
162171
className="ba-RegionAnnotations-creator"
172+
onAbort={[Function]}
163173
onStart={[Function]}
164174
onStop={[Function]}
165175
/>
@@ -173,6 +183,7 @@ describe('RegionAnnotations', () => {
173183
<Fragment>
174184
<RegionCreator
175185
className="ba-RegionAnnotations-creator is-discoverability-enabled"
186+
onAbort={[Function]}
176187
onStart={[Function]}
177188
onStop={[Function]}
178189
/>

src/region/__tests__/RegionCreator-test.tsx

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ jest.mock('../RegionRect');
1313
jest.mock('../regionUtil');
1414

1515
describe('RegionCreator', () => {
16+
const defaults = {
17+
onAbort: jest.fn(),
18+
onStart: jest.fn(),
19+
onStop: jest.fn(),
20+
};
21+
1622
const getDOMRect = (x = 0, y = 0, height = 1000, width = 1000): DOMRect => ({
1723
bottom: y + height,
1824
top: y,
@@ -26,8 +32,7 @@ describe('RegionCreator', () => {
2632
});
2733

2834
// Render helpers
29-
const getWrapper = (props = {}): ReactWrapper =>
30-
mount(<RegionCreator onStart={jest.fn()} onStop={jest.fn()} {...props} />);
35+
const getWrapper = (props = {}): ReactWrapper => mount(<RegionCreator {...defaults} {...props} />);
3136
const getWrapperRoot = (wrapper: ReactWrapper): ReactWrapper => wrapper.find('[data-testid="ba-RegionCreator"]');
3237

3338
beforeEach(() => {
@@ -90,18 +95,16 @@ describe('RegionCreator', () => {
9095
});
9196

9297
test('should call the onStart and onStop callback when drawing starts and stops', () => {
93-
const onStart = jest.fn();
94-
const onStop = jest.fn();
95-
const wrapper = getWrapper({ onStart, onStop });
98+
const wrapper = getWrapper();
9699

97100
simulateDrawStart(wrapper, 50, 50);
98101
simulateDrawMove(100, 100);
99102
simulateDrawStop();
100103
jest.advanceTimersByTime(1000);
101104
wrapper.update();
102105

103-
expect(onStart).toHaveBeenCalled();
104-
expect(onStop).toHaveBeenCalledWith({
106+
expect(defaults.onStart).toHaveBeenCalled();
107+
expect(defaults.onStop).toHaveBeenCalledWith({
105108
height: 5,
106109
type: 'rect',
107110
width: 5,
@@ -110,10 +113,20 @@ describe('RegionCreator', () => {
110113
});
111114
});
112115

116+
test('should call onStart and onAbort callback when user clicks without dragging', () => {
117+
const wrapper = getWrapper();
118+
119+
simulateDrawStart(wrapper, 50, 50);
120+
simulateDrawStop();
121+
jest.advanceTimersByTime(1000);
122+
wrapper.update();
123+
124+
expect(defaults.onStart).toHaveBeenCalled();
125+
expect(defaults.onAbort).toHaveBeenCalled();
126+
});
127+
113128
test('should do nothing if primary button is not pressed', () => {
114-
const onStart = jest.fn();
115-
const onStop = jest.fn();
116-
const wrapper = getWrapper({ onStart, onStop });
129+
const wrapper = getWrapper();
117130

118131
act(() => {
119132
wrapper.simulate('mousedown', { buttons: 2, clientX: 50, clientY: 50 });
@@ -123,21 +136,19 @@ describe('RegionCreator', () => {
123136
jest.advanceTimersByTime(1000);
124137
wrapper.update();
125138

126-
expect(onStart).not.toHaveBeenCalled();
127-
expect(onStop).not.toHaveBeenCalled();
139+
expect(defaults.onStart).not.toHaveBeenCalled();
140+
expect(defaults.onStop).not.toHaveBeenCalled();
128141
});
129142

130143
test('should do nothing if the mouse is moved without being pressed over the wrapper first', () => {
131-
const onStart = jest.fn();
132-
const onStop = jest.fn();
133-
const wrapper = getWrapper({ onStart, onStop });
144+
const wrapper = getWrapper();
134145

135146
simulateDrawMove(50, 50);
136147
jest.advanceTimersByTime(1000);
137148
wrapper.update();
138149

139-
expect(onStart).not.toHaveBeenCalled();
140-
expect(onStop).not.toHaveBeenCalled();
150+
expect(defaults.onStart).not.toHaveBeenCalled();
151+
expect(defaults.onStop).not.toHaveBeenCalled();
141152
});
142153

143154
test('should prevent click events from surfacing to the parent document', () => {
@@ -218,33 +229,29 @@ describe('RegionCreator', () => {
218229
});
219230

220231
test('should call the onStart and onStop callback when drawing starts and stops', () => {
221-
const onStart = jest.fn();
222-
const onStop = jest.fn();
223-
const wrapper = getWrapper({ onStart, onStop });
232+
const wrapper = getWrapper();
224233

225234
simulateDrawStart(wrapper, 50, 50);
226235
simulateDrawMove(wrapper, 100, 100);
227236
simulateDrawStop(wrapper);
228237
jest.advanceTimersByTime(1000);
229238
wrapper.update();
230239

231-
expect(onStart).toHaveBeenCalled();
232-
expect(onStop).toHaveBeenCalledWith(shape);
240+
expect(defaults.onStart).toHaveBeenCalled();
241+
expect(defaults.onStop).toHaveBeenCalledWith(shape);
233242
});
234243

235244
test('should call the onStart and onStop callback even if drawing is cancelled', () => {
236-
const onStart = jest.fn();
237-
const onStop = jest.fn();
238-
const wrapper = getWrapper({ onStart, onStop });
245+
const wrapper = getWrapper();
239246

240247
simulateDrawStart(wrapper, 50, 50);
241248
simulateDrawMove(wrapper, 100, 100);
242249
simulateDrawCancel(wrapper);
243250
jest.advanceTimersByTime(1000);
244251
wrapper.update();
245252

246-
expect(onStart).toHaveBeenCalled();
247-
expect(onStop).toHaveBeenCalledWith(shape);
253+
expect(defaults.onStart).toHaveBeenCalled();
254+
expect(defaults.onStop).toHaveBeenCalledWith(shape);
248255
});
249256
});
250257

src/store/eventing/__tests__/staged-test.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,11 @@ describe('store/eventing/staged', () => {
9393
});
9494

9595
describe('handleResetCreatorAction()', () => {
96-
test('should not emit event if type is null', () => {
97-
const prevState = createStore().getState();
98-
handleResetCreatorAction(prevState);
99-
100-
expect(eventManager.emit).not.toHaveBeenCalled();
101-
});
102-
10396
test.each`
104-
prev | type
105-
${getCreatorState()} | ${'highlight'}
106-
${getCreatorState(false)} | ${'region'}
97+
prev | type
98+
${createStore().getState()} | ${null}
99+
${getCreatorState()} | ${'highlight'}
100+
${getCreatorState(false)} | ${'region'}
107101
`('should emit cancel event if with type=$type', ({ prev, type }) => {
108102
handleResetCreatorAction(prev);
109103

src/store/eventing/staged.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,5 @@ export const handleResetCreatorAction = (prevState: AppState): void => {
5252
const prevStaged = getCreatorStaged(prevState);
5353
const type = getType(prevStaged);
5454

55-
if (!type) {
56-
return;
57-
}
58-
5955
eventManager.emit(Event.CREATOR_STAGED_CHANGE, { type, status: 'cancel' });
6056
};

0 commit comments

Comments
 (0)