Skip to content

Commit

Permalink
force defaults for undefined configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Hartz committed Feb 26, 2022
1 parent 95ca939 commit f4e3aae
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
31 changes: 31 additions & 0 deletions __tests__/useSwipeable.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,37 @@ describe("useSwipeable", () => {
expect(onTap).not.toHaveBeenCalled();
});

it("handles touch events and fires correct props with undefined values for config", () => {
const swipeFuncs = getMockedSwipeFunctions();
const undefinedConfigOptions: SwipeableProps = {
delta: undefined,
preventDefaultTouchmoveEvent: undefined,
rotationAngle: undefined,
trackMouse: undefined,
trackTouch: undefined,
};
const { getByText } = render(
<SwipeableUsingHook
{...swipeFuncs}
{...undefinedConfigOptions}
/>
);

const touchArea = getByText(TESTING_TEXT);

fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
fireEvent[TM](touchArea, cte({ x: 100, y: 125 }));
fireEvent[TM](touchArea, cte({ x: 100, y: 150 }));
fireEvent[TE](touchArea, cte({}));

expect(swipeFuncs.onSwiped).toHaveBeenCalled();
expect(swipeFuncs.onSwipedDown).toHaveBeenCalled();
expect(swipeFuncs.onSwipedUp).not.toHaveBeenCalled();
expect(swipeFuncs.onSwipedLeft).not.toHaveBeenCalled();
expect(swipeFuncs.onSwipedRight).not.toHaveBeenCalled();
expect(swipeFuncs.onSwiping).toHaveBeenCalledTimes(2);
});

it("handles mouse events with trackMouse prop and fires correct props", () => {
const swipeFuncs = getMockedSwipeFunctions();
const { getByText } = render(
Expand Down
22 changes: 18 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
LEFT,
RIGHT,
Setter,
ConfigurationOptions,
SwipeableCallbacks,
SwipeableHandlers,
SwipeableProps,
Expand All @@ -34,7 +35,7 @@ export {
Vector2,
};

const defaultProps = {
const defaultProps: ConfigurationOptions = {
delta: 10,
preventDefaultTouchmoveEvent: false,
rotationAngle: 0,
Expand Down Expand Up @@ -253,7 +254,7 @@ function getHandlers(
// if new DOM el clean up old DOM and reset cleanUpTouch
if (state.el && state.el !== el && state.cleanUpTouch) {
state.cleanUpTouch();
addState.cleanUpTouch = undefined;
addState.cleanUpTouch = void 0;
}
// only attach if we want to track touch
if (props.trackTouch && el) {
Expand Down Expand Up @@ -290,7 +291,7 @@ function updateTransientState(
// clean up touch handlers if no longer tracking touches
if (!props.trackTouch && state.cleanUpTouch) {
state.cleanUpTouch();
addState.cleanUpTouch = undefined;
addState.cleanUpTouch = void 0;
} else if (props.trackTouch && !state.cleanUpTouch) {
// attach/re-attach touch handlers
if (state.el) {
Expand All @@ -309,7 +310,20 @@ export function useSwipeable(options: SwipeableProps): SwipeableHandlers {
const transientProps = React.useRef<SwipeablePropsWithDefaultOptions>({
...defaultProps,
});
transientProps.current = { ...defaultProps, ...options };
transientProps.current = {
...defaultProps,
...options,
// Force defaults for config properties
delta: options.delta === void 0 ? defaultProps.delta : options.delta,
rotationAngle:
options.rotationAngle === void 0
? defaultProps.rotationAngle
: options.rotationAngle,
trackTouch:
options.trackTouch === void 0
? defaultProps.trackTouch
: options.trackTouch,
};

const [handlers, attachTouch] = React.useMemo(
() =>
Expand Down

0 comments on commit f4e3aae

Please sign in to comment.