Skip to content

Commit

Permalink
Sensor issue (#1702)
Browse files Browse the repository at this point in the history
Fixes issue with SensorAPI | tryReleaseLock
  • Loading branch information
VV-YY committed Jan 8, 2020
2 parents 9806068 + b636396 commit 0ae79d0
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/view/use-sensor-marshal/use-sensor-marshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
drop as dropAction,
lift as liftAction,
type LiftArgs as LiftActionArgs,
flush,
} from '../../state/action-creators';
import type {
Registry,
Expand Down Expand Up @@ -450,7 +451,20 @@ export default function useSensorMarshal({
[registry.draggable],
);

const tryReleaseLock = useCallback(lockAPI.tryAbandon, [lockAPI]);
const tryReleaseLock = useCallback(
function tryReleaseLock() {
if (!lockAPI.isClaimed()) {
return;
}

lockAPI.tryAbandon();

if (store.getState().phase !== 'IDLE') {
store.dispatch(flush());
}
},
[lockAPI, store],
);
const isLockClaimed = useCallback(lockAPI.isClaimed, [lockAPI]);

const api: SensorAPI = useMemo(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// @flow
import React from 'react';
import { render } from '@testing-library/react';
import type { SensorAPI, Sensor } from '../../../../../src/types';
import { forEachSensor, type Control, simpleLift } from '../../util/controls';
import { isDragging } from '../../util/helpers';
import App from '../../util/app';
import { invariant } from '../../../../../src/invariant';

forEachSensor((control: Control) => {
it('should cleanup a drag if a lock is forceably released mid drag', () => {
let api: SensorAPI;
const sensor: Sensor = (value: SensorAPI) => {
api = value;
};

const { getByText } = render(<App sensors={[sensor]} />);
const handle: HTMLElement = getByText('item: 0');
invariant(api);

simpleLift(control, handle);

expect(api.isLockClaimed()).toBe(true);
expect(isDragging(handle)).toBe(true);

api.tryReleaseLock();

expect(api.isLockClaimed()).toBe(false);
expect(isDragging(handle)).toBe(false);

// allowing reclaiming after
simpleLift(control, handle);

expect(api.isLockClaimed()).toBe(true);
expect(isDragging(handle)).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// @flow
import React from 'react';
import { render } from '@testing-library/react';
import type { SensorAPI, Sensor } from '../../../../../src/types';
import { forEachSensor, type Control, simpleLift } from '../../util/controls';
import { isDragging } from '../../util/helpers';
import App from '../../util/app';
import { invariant } from '../../../../../src/invariant';

forEachSensor((control: Control) => {
// keyboard has no pre lift
if (control.name === 'keyboard') {
return;
}

it('should cleanup a drag if a lock is forceably released mid drag', () => {
let api: SensorAPI;
const sensor: Sensor = (value: SensorAPI) => {
api = value;
};

const { getByText } = render(<App sensors={[sensor]} />);
const handle: HTMLElement = getByText('item: 0');
invariant(api);

control.preLift(handle);

// lock is claimed but not dragging yet
expect(api.isLockClaimed()).toBe(true);
expect(isDragging(handle)).toBe(false);

api.tryReleaseLock();

expect(isDragging(handle)).toBe(false);
expect(api.isLockClaimed()).toBe(false);

// a lift after a released lock can get the lock all good
simpleLift(control, handle);
expect(api.isLockClaimed()).toBe(true);
expect(isDragging(handle)).toBe(true);
});
});

0 comments on commit 0ae79d0

Please sign in to comment.