Skip to content

Commit

Permalink
fixing middleware tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexreardon committed Jun 19, 2018
1 parent 3a012dc commit 8db4a5e
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 174 deletions.
5 changes: 4 additions & 1 deletion src/state/create-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import autoScroll from './middleware/auto-scroll';
import pendingDrop from './middleware/pending-drop';
import type { DimensionMarshal } from './dimension-marshal/dimension-marshal-types';
import type { StyleMarshal } from '../view/style-marshal/style-marshal-types';
import type { AutoScroller } from '../state/auto-scroller/auto-scroller-types';
import type { Store, Hooks, Announce } from '../types';

// We are checking if window is available before using it.
Expand All @@ -26,13 +27,15 @@ type Args = {|
styleMarshal: StyleMarshal,
getHooks: () => Hooks,
announce: Announce,
getScroller: () => AutoScroller,
|}

export default ({
getDimensionMarshal,
styleMarshal,
getHooks,
announce,
getScroller,
}: Args): Store => createStore(
reducer,
composeEnhancers(
Expand Down Expand Up @@ -70,7 +73,7 @@ export default ({
// When a drop animation finishes - fire a drop complete
dropAnimationFinish,
pendingDrop,
autoScroll(getDimensionMarshal),
autoScroll(getScroller),
// Fire hooks for consumers
hooks(getHooks, announce),
),
Expand Down
19 changes: 4 additions & 15 deletions src/state/middleware/auto-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,8 @@ import type {
} from '../../types';

// TODO: this is broken - good times
export default (getMarshal: () => DimensionMarshal) =>
export default (getScroller: () => AutoScroller) =>
(store: Store) => (next: (Action) => mixed) => {
const scroller: AutoScroller = createAutoScroller({
...bindActionCreators({
// TODO: using next to avoid recursive calls to auto scrolling..
move,
}, store.dispatch),
scrollDroppable: (id: DraggableId, change: Position): void => {
getMarshal().scrollDroppable(id, change);
},
scrollWindow,
});

const shouldCancel = (action: Action) =>
// Need to cancel any pending auto scrolling when drag is ending
action.type === 'CANCEL' ||
Expand All @@ -40,7 +29,7 @@ export default (getMarshal: () => DimensionMarshal) =>

return (action: Action): mixed => {
if (shouldCancel(action)) {
scroller.cancel();
getScroller().cancel();
next(action);
return;
}
Expand All @@ -57,14 +46,14 @@ export default (getMarshal: () => DimensionMarshal) =>
}

if (state.autoScrollMode === 'FLUID') {
scroller.fluidScroll(state);
getScroller().fluidScroll(state);
return;
}

if (!state.scrollJumpRequest) {
return;
}

scroller.jumpScroll(state);
getScroller().jumpScroll(state);
};
};
8 changes: 4 additions & 4 deletions src/state/middleware/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import type { StyleMarshal } from '../../view/style-marshal/style-marshal-types'

export default (marshal: StyleMarshal) =>
() => (next: (Action) => mixed) => (action: Action): mixed => {
if (action.type === 'COLLECTION_STARTING') {
marshal.collecting();
}

if (action.type === 'INITIAL_PUBLISH' || action.type === 'PUBLISH') {
marshal.dragging();
}

if (action.type === 'COLLECTION_STARTING') {
marshal.collecting();
}

if (action.type === 'DROP_ANIMATE') {
marshal.dropping(action.payload.result.reason);
}
Expand Down
3 changes: 2 additions & 1 deletion src/view/drag-drop-context/drag-drop-context.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export default class DragDropContext extends React.Component<Props> {
onDragUpdate: this.props.onDragUpdate,
}),
announce: this.announcer.announce,
getScroller: () => this.autoScroller,
});
const callbacks: DimensionMarshalCallbacks = bindActionCreators({
collectionStarting,
Expand All @@ -104,7 +105,7 @@ export default class DragDropContext extends React.Component<Props> {
scrollDroppable: this.dimensionMarshal.scrollDroppable,
...bindActionCreators({
move,
}),
}, this.store.dispatch),
});
}
// Need to declare childContextTypes without flow
Expand Down
74 changes: 73 additions & 1 deletion test/unit/state/middleware/auto-scroll.spec.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,74 @@

// @flow
throw new Error('TODO');
import type { Action, Store } from '../../../../src/types';
import type { AutoScroller } from '../../../../src/state/auto-scroller/auto-scroller-types';
import createStore from './util/create-store';
import middleware from '../../../../src/state/middleware/auto-scroll';
import { animateDropArgs, userCancelArgs, completeDropArgs, initialPublishArgs } from '../../../utils/preset-action-args';
import { animateDrop, drop, completeDrop, collectionStarting, prepare, initialPublish, moveDown, type InitialPublishArgs } from '../../../../src/state/action-creators';

const shouldCancel: Action[] = [
animateDrop(animateDropArgs),
animateDrop(userCancelArgs),
drop({ reason: 'DROP' }),
drop({ reason: 'CANCEL' }),
completeDrop(completeDropArgs),
collectionStarting(),
];

const getScrollerStub = (): AutoScroller => ({
cancel: jest.fn(),
fluidScroll: jest.fn(),
jumpScroll: jest.fn(),
});

shouldCancel.forEach((action: Action) => {
it(`should cancel a pending scroll when a ${action.type} is fired`, () => {
const scroller: AutoScroller = getScrollerStub();
const store: Store = createStore(middleware(() => scroller));

store.dispatch(prepare());
store.dispatch(initialPublish(initialPublishArgs));
expect(store.getState().phase).toBe('DRAGGING');

expect(scroller.cancel).not.toHaveBeenCalled();
store.dispatch(action);
expect(scroller.cancel).toHaveBeenCalled();
});
});

it('should fire a fluid scroll when in the FLUID auto scrolling mode', () => {
const scroller: AutoScroller = getScrollerStub();
const store: Store = createStore(middleware(() => scroller));

store.dispatch(prepare());
expect(scroller.fluidScroll).not.toHaveBeenCalled();

store.dispatch(initialPublish(initialPublishArgs));
expect(scroller.fluidScroll).toHaveBeenCalledWith(store.getState());

store.dispatch(moveDown());
expect(scroller.fluidScroll).toHaveBeenCalledWith(store.getState());
expect(scroller.jumpScroll).not.toHaveBeenCalled();
});

it('should fire a jump scroll when in the JUMP auto scrolling mode and there is a scroll jump request', () => {
const customInitial: InitialPublishArgs = {
...initialPublishArgs,
autoScrollMode: 'JUMP',
};
const scroller: AutoScroller = getScrollerStub();
const store: Store = createStore(middleware(() => scroller));
store.dispatch(prepare());

store.dispatch(initialPublish(customInitial));
expect(scroller.jumpScroll).not.toHaveBeenCalled();
expect(scroller.fluidScroll).not.toHaveBeenCalled();

store.dispatch(moveDown());
expect(scroller.jumpScroll).not.toHaveBeenCalled();

// Currently no way to poke the state through an action to create a scrollJumpRequest
// TODO: investigate mechanism
});

Loading

0 comments on commit 8db4a5e

Please sign in to comment.