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 de452ca commit c396b92
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 76 deletions.
2 changes: 1 addition & 1 deletion src/state/middleware/dimension-marshal-stopper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { DimensionMarshal } from '../dimension-marshal/dimension-marshal-ty

export default (getMarshal: () => DimensionMarshal) =>
() => (next: (Action) => mixed) => (action: Action): mixed => {
// Not stopping a bulk collection on a 'DROP' as we want that collection to continue
// Not stopping a collection on a 'DROP' as we want that collection to continue
if (action.type === 'DROP_COMPLETE' || action.type === 'CLEAN' || action.type === 'DROP_ANIMATE') {
const marshal: DimensionMarshal = getMarshal();
marshal.stopPublishing();
Expand Down
8 changes: 4 additions & 4 deletions src/view/draggable/draggable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ export default class Draggable extends Component<Props> {
);
}

onMoveEnd = () => {
if (this.props.isDropAnimating) {
this.props.dropAnimationFinished();
onMoveEnd = () => {
if (this.props.isDropAnimating) {
this.props.dropAnimationFinished();
}
}
}

onLift = (options: {clientSelection: Position, autoScrollMode: AutoScrollMode}) => {
timings.start('LIFT');
Expand Down
10 changes: 5 additions & 5 deletions test/unit/state/middleware/dimension-marshal-stopper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { DimensionMarshal } from '../../../../src/state/dimension-marshal/d
import middleware from '../../../../src/state/middleware/dimension-marshal-stopper';
import dropMiddleware from '../../../../src/state/middleware/drop';
import createStore from './util/create-store';
import { clean, prepare, initialPublish, drop, bulkReplace, completeDrop, animateDrop } from '../../../../src/state/action-creators';
import { initialPublishArgs, initialBulkReplaceArgs, getDragStart } from '../../../utils/preset-action-args';
import { clean, prepare, initialPublish, drop, completeDrop, animateDrop, collectionStarting } from '../../../../src/state/action-creators';
import { initialPublishArgs, getDragStart } from '../../../utils/preset-action-args';
import noImpact from '../../../../src/state/no-impact';

const getMarshal = (stopPublishing: Function): DimensionMarshal => {
Expand Down Expand Up @@ -40,7 +40,9 @@ it('should not stop a collection if a drop is pending', () => {

store.dispatch(prepare());
store.dispatch(initialPublish(initialPublishArgs));
expect(store.getState().phase).toBe('BULK_COLLECTING');
expect(store.getState().phase).toBe('DRAGGING');
store.dispatch(collectionStarting());
expect(store.getState().phase).toBe('COLLECTING');
expect(stopPublishing).not.toHaveBeenCalled();

// dropping
Expand All @@ -59,7 +61,6 @@ it('should stop a collection if a drag is complete', () => {

store.dispatch(prepare());
store.dispatch(initialPublish(initialPublishArgs));
store.dispatch(bulkReplace(initialBulkReplaceArgs));
expect(store.getState().phase).toBe('DRAGGING');
expect(stopPublishing).not.toHaveBeenCalled();

Expand All @@ -84,7 +85,6 @@ it('should stop a collection if a drop animation starts', () => {

store.dispatch(prepare());
store.dispatch(initialPublish(initialPublishArgs));
store.dispatch(bulkReplace(initialBulkReplaceArgs));
expect(store.getState().phase).toBe('DRAGGING');
expect(stopPublishing).not.toHaveBeenCalled();

Expand Down
60 changes: 19 additions & 41 deletions test/unit/state/middleware/drop.spec.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
// @flow
import type { Position } from 'css-box-model';
import invariant from 'tiny-invariant';
import middleware from '../../../../src/state/middleware/drop';
import createStore from './util/create-store';
import getHomeLocation from '../../../../src/state/get-home-location';
import { add, patch } from '../../../../src/state/position';
import { getPreset, makeScrollable } from '../../../utils/dimension';
import passThrough from './util/pass-through-middleware';
import {
clean,
drop,
prepare,
initialPublish,
bulkReplace,
animateDrop,
dropPending,
move,
completeDrop,
updateDroppableScroll,
moveByWindowScroll,
type InitialPublishArgs,
type BulkReplaceArgs,
type DropAnimateAction, moveByWindowScroll,
type DropAnimateAction, collectionStarting,
} from '../../../../src/state/action-creators';
import {
initialPublishArgs,
Expand Down Expand Up @@ -60,7 +59,6 @@ it('should throw an error if a drop action occurs while not in a phase where you
store.dispatch(clean());
store.dispatch(prepare());
store.dispatch(initialPublish(initialPublishArgs));
store.dispatch(bulkReplace(initialBulkReplaceArgs));
expect(store.getState().phase).toBe('DRAGGING');

// moving a little bit so that a drop animation will be needed
Expand All @@ -75,22 +73,20 @@ it('should throw an error if a drop action occurs while not in a phase where you
expect(() => store.dispatch(drop({ reason: 'DROP' }))).toThrow();
});

it('should dispatch a DROP_PENDING action if BULK_COLLECTING', () => {
it('should dispatch a DROP_PENDING action if COLLECTING', () => {
const mock = jest.fn();
const passThrough = () => next => (action) => {
mock(action);
next(action);
};
const store: Store = createStore(
passThrough,
passThrough(mock),
middleware,
);

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

// now in the bulk collecting phase
expect(store.getState().phase).toBe('BULK_COLLECTING');
expect(store.getState().phase).toBe('COLLECTING');
mock.mockReset();

// drop
Expand All @@ -102,22 +98,19 @@ it('should dispatch a DROP_PENDING action if BULK_COLLECTING', () => {
expect(store.getState().phase).toBe('DROP_PENDING');
});

it.only('should throw if a drop action is fired and there is DROP_PENDING and it is waiting for a publish', () => {
it('should throw if a drop action is fired and there is DROP_PENDING and it is waiting for a publish', () => {
const mock = jest.fn();
const passThrough = () => next => (action) => {
mock(action);
next(action);
};
const store: Store = createStore(
passThrough,
passThrough(mock),
middleware,
);

store.dispatch(prepare());
store.dispatch(initialPublish(initialPublishArgs));
store.dispatch(collectionStarting());

// now in the bulk collecting phase
expect(store.getState().phase).toBe('BULK_COLLECTING');
expect(store.getState().phase).toBe('COLLECTING');
mock.mockReset();

// drop moving to drop pending
Expand All @@ -142,19 +135,14 @@ describe('no drop animation required', () => {
describe(`with drop reason: ${reason}`, () => {
it('should fire a complete drop action is no drop animation is required', () => {
const mock = jest.fn();
const passThrough = () => next => (action) => {
mock(action);
next(action);
};
const store: Store = createStore(
passThrough,
passThrough(mock),
middleware,
);

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

// no movement yet
Expand Down Expand Up @@ -188,12 +176,8 @@ describe('no drop animation required', () => {

describe('drop animation required', () => {
const withPassThrough = (myMiddleware: mixed, mock: Function): Store => {
const passThrough = () => next => (action) => {
mock(action);
next(action);
};
const store: Store = createStore(
passThrough,
passThrough(mock),
myMiddleware,
);
return store;
Expand All @@ -206,7 +190,6 @@ describe('drop animation required', () => {

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

// moving a little bit so that a drop animation will be needed
Expand Down Expand Up @@ -254,7 +237,6 @@ describe('drop animation required', () => {
store.dispatch(clean());
store.dispatch(prepare());
store.dispatch(initialPublish(customArgs));
store.dispatch(bulkReplace(initialBulkReplaceArgs));
expect(store.getState().phase).toBe('DRAGGING');

// doing a small scroll
Expand Down Expand Up @@ -290,12 +272,12 @@ describe('drop animation required', () => {
const store: Store = withPassThrough(middleware, mock);

const scrollableForeign: DroppableDimension = makeScrollable(preset.foreign);
const customReplace: BulkReplaceArgs = {
...initialBulkReplaceArgs,
const customInitial: InitialPublishArgs = {
...initialPublishArgs,
dimensions: {
...initialBulkReplaceArgs.dimensions,
...initialPublishArgs.dimensions,
droppables: {
...initialBulkReplaceArgs.dimensions.droppables,
...initialPublishArgs.dimensions.droppables,
[scrollableForeign.descriptor.id]: scrollableForeign,
},
},
Expand All @@ -304,8 +286,7 @@ describe('drop animation required', () => {
// getting into a drag
store.dispatch(clean());
store.dispatch(prepare());
store.dispatch(initialPublish(initialPublishArgs));
store.dispatch(bulkReplace(customReplace));
store.dispatch(initialPublish(customInitial));
expect(store.getState().phase).toBe('DRAGGING');

// moving over the foreign droppable
Expand Down Expand Up @@ -356,7 +337,6 @@ describe('drop animation required', () => {
// getting into a drag
store.dispatch(prepare());
store.dispatch(initialPublish(customArgs));
store.dispatch(bulkReplace(initialBulkReplaceArgs));
expect(store.getState().phase).toBe('DRAGGING');

// move after the end of the home droppable
Expand Down Expand Up @@ -421,7 +401,6 @@ describe('drop animation required', () => {
// getting into a drag
store.dispatch(prepare());
store.dispatch(initialPublish(customArgs));
store.dispatch(bulkReplace(initialBulkReplaceArgs));
expect(store.getState().phase).toBe('DRAGGING');

// moving to the top of the foreign droppable
Expand Down Expand Up @@ -474,7 +453,6 @@ describe('drop animation required', () => {
store.dispatch(clean());
store.dispatch(prepare());
store.dispatch(initialPublish(initialPublishArgs));
store.dispatch(bulkReplace(initialBulkReplaceArgs));
expect(store.getState().phase).toBe('DRAGGING');

// scroll the window
Expand Down
32 changes: 8 additions & 24 deletions test/unit/state/middleware/lift.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @flow
import { bindActionCreators } from 'redux';
import type { Store, PendingDrop } from '../../../../src/types';
import type { DimensionMarshal, Callbacks } from '../../../../src/state/dimension-marshal/dimension-marshal-types';
import middleware from '../../../../src/state/middleware/lift';
Expand All @@ -10,34 +9,26 @@ import {
prepare,
lift,
initialPublish,
bulkReplace,
clean,
publish,
animateDrop,
completeDrop,
bulkCollectionStarting,
collectionStarting,
updateDroppableScroll,
updateDroppableIsEnabled,
} from '../../../../src/state/action-creators';
import getDimensionMarshal, { populateMarshal } from '../../../utils/dimension-marshal';
import getHomeLocation from '../../../../src/state/get-home-location';
import {
viewport,
liftArgs,
initialPublishArgs,
initialBulkReplaceArgs,
getDragStart,
critical,
} from '../../../utils/preset-action-args';
import createDimensionMarshal from '../../../../src/state/dimension-marshal/dimension-marshal';
import { populateMarshal } from '../../../utils/dimension-marshal';

const getMarshal = (store: Store): DimensionMarshal => {
const callbacks: Callbacks = bindActionCreators({
bulkCollectionStarting,
bulkReplace,
updateDroppableScroll,
updateDroppableIsEnabled,
}, store.dispatch);
const marshal: DimensionMarshal = createDimensionMarshal(callbacks);
const marshal: DimensionMarshal = getDimensionMarshal(store.dispatch);
populateMarshal(marshal);

return marshal;
Expand Down Expand Up @@ -95,7 +86,6 @@ it('should flush any animating drops', () => {
// start a drag
store.dispatch(prepare());
store.dispatch(initialPublish(initialPublishArgs));
store.dispatch(bulkReplace(initialBulkReplaceArgs));
expect(store.getState().phase).toBe('DRAGGING');

// start a drop
Expand All @@ -104,7 +94,9 @@ it('should flush any animating drops', () => {
impact: {
movement: {
displaced: [],
amount: 0,
amount: {
x: 0, y: 0,
},
isBeyondStartPosition: false,
},
direction: 'vertical',
Expand Down Expand Up @@ -153,7 +145,7 @@ describe('collection phase', () => {
expect(mock).not.toHaveBeenCalled();
});

it('should publish the critical dimensions and then trigger a non-critical dimension collection', () => {
it('should publish the initial dimensions', () => {
const mock = jest.fn();
const store: Store = createStore(
passThrough(mock),
Expand All @@ -170,14 +162,6 @@ describe('collection phase', () => {
mock.mockReset();
jest.runOnlyPendingTimers();
expect(mock).toHaveBeenCalledWith(initialPublish(initialPublishArgs));
expect(mock).toHaveBeenCalledWith(bulkCollectionStarting());
expect(mock).toHaveBeenCalledTimes(2);
expect(store.getState().phase).toBe('BULK_COLLECTING');

// then there is a dimension marshal collection
mock.mockReset();
requestAnimationFrame.flush();
expect(mock).toHaveBeenCalledWith(bulkReplace(initialBulkReplaceArgs));
expect(mock).toHaveBeenCalledTimes(1);
expect(store.getState().phase).toBe('DRAGGING');
});
Expand Down
3 changes: 3 additions & 0 deletions test/unit/state/middleware/style.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @flow

throw new Error('TODO');
4 changes: 3 additions & 1 deletion test/utils/dimension-marshal.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// @flow
import { type Position } from 'css-box-model';
import bindActionCreators from 'redux';
import { bindActionCreators } from 'redux';
import createDimensionMarshal from '../../src/state/dimension-marshal/dimension-marshal';
import {
publish,
updateDroppableScroll,
updateDroppableIsEnabled,
collectionStarting,
} from '../../src/state/action-creators';
import { getPreset } from './dimension';
import type { DimensionMarshal, Callbacks, DroppableCallbacks } from '../../src/state/dimension-marshal/dimension-marshal-types';
Expand All @@ -20,6 +21,7 @@ import type {
export default (dispatch: Function): DimensionMarshal => {
const callbacks: Callbacks = bindActionCreators({
publish,
collectionStarting,
updateDroppableScroll,
updateDroppableIsEnabled,
}, dispatch);
Expand Down

0 comments on commit c396b92

Please sign in to comment.