Skip to content

Commit

Permalink
more dimension marshal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexreardon committed May 23, 2018
1 parent 3bb4f07 commit 966b2c6
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 42 deletions.
10 changes: 7 additions & 3 deletions src/state/dimension-marshal/dimension-marshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,13 @@ export default (callbacks: Callbacks) => {
};

const updateDroppableIsEnabled = (id: DroppableId, isEnabled: boolean) => {
invariant(entries.droppables[id], `Cannot update the scroll on Droppable ${id} as it is not registered`);

// no need to update the application state if a collection is not occurring
if (!collection) {
return;
}

invariant(entries.droppables[id], `Cannot update the scroll on Droppable ${id} as it is not registered`);

// At this point a non primary droppable dimension might not yet be published
// but may have its enabled state changed. For now we still publish this change
// and let the reducer exit early if it cannot find the dimension in the state.
Expand All @@ -243,13 +243,14 @@ export default (callbacks: Callbacks) => {
if (!collection) {
return;
}

callbacks.updateDroppableScroll(id, newScroll);
};

const scrollDroppable = (id: DroppableId, change: Position) => {
const entry: ?DroppableEntry = entries.droppables[id];

invariant(entry, 'Cannot scroll Droppable if not in entries');
invariant(entry, `Cannot scroll Droppable ${id} as it is not registered`);

if (!collection) {
return;
Expand Down Expand Up @@ -325,12 +326,15 @@ export default (callbacks: Callbacks) => {
};

const marshal: DimensionMarshal = {
// dimension registration
registerDraggable,
updateDraggable,
unregisterDraggable,
registerDroppable,
updateDroppable,
unregisterDroppable,

// droppable changes
updateDroppableIsEnabled,
scrollDroppable,
updateDroppableScroll,
Expand Down
19 changes: 8 additions & 11 deletions test/unit/view/dimension-marshal/collection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,12 @@ import type {
import {
populateMarshal,
resetWatcher,
type DimensionWatcher,
getCallbacksStub,
type DimensionWatcher, withExpectedAdvancedUsageWarning,
} from './util';

const preset = getPreset();

const getCallbacksStub = (): Callbacks => ({
bulkReplace: jest.fn(),
updateDroppableScroll: jest.fn(),
updateDroppableIsEnabled: jest.fn(),
bulkCollectionStarting: jest.fn(),
});

const critical: Critical = {
droppable: preset.home.descriptor,
draggable: preset.inHome1.descriptor,
Expand Down Expand Up @@ -159,7 +153,6 @@ describe('collect', () => {
});

it('should let the system know a bulk collection is starting', () => {
jest.spyOn(console, 'warn').mockImplementation(() => { });
const callbacks: Callbacks = getCallbacksStub();
const marshal: DimensionMarshal = createDimensionMarshal(callbacks);
populateMarshal(marshal);
Expand Down Expand Up @@ -221,7 +214,9 @@ describe('collect', () => {
marshal.startPublishing(defaultRequest, preset.windowScroll);
// watcher has been called with critical dimensions
resetWatcher(watcher);
marshal.collect({ includeCritical: true });
withExpectedAdvancedUsageWarning(() => {
marshal.collect({ includeCritical: true });
});

// need to wait an animation frame
expect(callbacks.bulkReplace).not.toHaveBeenCalled();
Expand Down Expand Up @@ -408,7 +403,9 @@ describe('collect', () => {
expect(callbacks.bulkReplace).not.toHaveBeenCalled();

// another collection - returning to first phase
marshal.collect({ includeCritical: true });
withExpectedAdvancedUsageWarning(() => {
marshal.collect({ includeCritical: true });
});
expect(watcher.draggable.getDimension).not.toHaveBeenCalled();
expect(callbacks.bulkReplace).not.toHaveBeenCalled();

Expand Down
178 changes: 178 additions & 0 deletions test/unit/view/dimension-marshal/droppable-passthrough.spec.js
Original file line number Diff line number Diff line change
@@ -1 +1,179 @@
// @flow
import createDimensionMarshal from '../../../../src/state/dimension-marshal/dimension-marshal';
import { getPreset } from '../../../utils/dimension';
import type {
Callbacks,
DimensionMarshal,
StartPublishingResult,
} from '../../../../src/state/dimension-marshal/dimension-marshal-types';
import getViewport from '../../../../src/view/window/get-viewport';
import type {
Critical,
LiftRequest,
DimensionMap,
DraggableId,
DroppableId,
DraggableDimension,
DroppableDimension,
DraggableDimensionMap,
DroppableDimensionMap,
} from '../../../../src/types';
import {
populateMarshal,
resetWatcher,
getCallbacksStub,
type DimensionWatcher,
} from './util';

const preset = getPreset();

const critical: Critical = {
draggable: preset.inHome1.descriptor,
droppable: preset.home.descriptor,
};

const criticalDimensions: DimensionMap = {
draggables: {
[preset.inHome1.descriptor.id]: preset.inHome1,
},
droppables: {
[preset.home.descriptor.id]: preset.home,
},
};

const defaultRequest: LiftRequest = {
draggableId: critical.draggable.id,
scrollOptions: {
shouldPublishImmediately: false,
},
};

describe('force scrolling a droppable', () => {
it('should scroll the droppable', () => {
const callbacks: Callbacks = getCallbacksStub();
const marshal: DimensionMarshal = createDimensionMarshal(callbacks);
const watcher: DimensionWatcher = populateMarshal(marshal);

// initial lift
marshal.startPublishing(defaultRequest, preset.windowScroll);
marshal.collect({ includeCritical: false });
requestAnimationFrame.flush();
expect(watcher.droppable.scroll).not.toHaveBeenCalled();

// scroll
marshal.scrollDroppable(critical.droppable.id, { x: 10, y: 20 });
expect(watcher.droppable.scroll)
.toHaveBeenCalledWith(critical.droppable.id, { x: 10, y: 20 });
});

it('should throw if the droppable cannot be found', () => {
const callbacks: Callbacks = getCallbacksStub();
const marshal: DimensionMarshal = createDimensionMarshal(callbacks);
populateMarshal(marshal, criticalDimensions);

// initial lift
marshal.startPublishing(defaultRequest, preset.windowScroll);
marshal.collect({ includeCritical: false });
requestAnimationFrame.flush();

// scroll
expect(() => {
marshal.scrollDroppable(preset.foreign.descriptor.id, { x: 10, y: 20 });
}).toThrow(`Cannot scroll Droppable ${preset.foreign.descriptor.id} as it is not registered`);
});

it('should not scroll the droppable if no collection is occurring', () => {
const callbacks: Callbacks = getCallbacksStub();
const marshal: DimensionMarshal = createDimensionMarshal(callbacks);
const watcher: DimensionWatcher = populateMarshal(marshal);

marshal.scrollDroppable(critical.droppable.id, { x: 10, y: 20 });
expect(watcher.droppable.scroll).not.toHaveBeenCalled();
});
});

describe('responding to scroll changes', () => {
it('should let consumers know', () => {
const callbacks: Callbacks = getCallbacksStub();
const marshal: DimensionMarshal = createDimensionMarshal(callbacks);
const watcher: DimensionWatcher = populateMarshal(marshal);

// initial lift
marshal.startPublishing(defaultRequest, preset.windowScroll);
marshal.collect({ includeCritical: false });
requestAnimationFrame.flush();
expect(watcher.droppable.scroll).not.toHaveBeenCalled();

marshal.updateDroppableScroll(critical.droppable.id, { x: 10, y: 20 });
expect(callbacks.updateDroppableScroll).toHaveBeenCalledWith(
critical.droppable.id, { x: 10, y: 20 }
);
});

it('should throw if the droppable cannot be found', () => {
const callbacks: Callbacks = getCallbacksStub();
const marshal: DimensionMarshal = createDimensionMarshal(callbacks);
populateMarshal(marshal, criticalDimensions);

// initial lift
marshal.startPublishing(defaultRequest, preset.windowScroll);
marshal.collect({ includeCritical: false });
requestAnimationFrame.flush();
expect(callbacks.updateDroppableScroll).not.toHaveBeenCalled();

expect(() => {
marshal.updateDroppableScroll(preset.foreign.descriptor.id, { x: 10, y: 20 });
}).toThrow(`Cannot update the scroll on Droppable ${preset.foreign.descriptor.id} as it is not registered`);
});

it('should not let consumers know if know drag is occurring', () => {
const callbacks: Callbacks = getCallbacksStub();
const marshal: DimensionMarshal = createDimensionMarshal(callbacks);
populateMarshal(marshal, criticalDimensions);

marshal.updateDroppableScroll(critical.droppable.id, { x: 10, y: 20 });
expect(callbacks.updateDroppableScroll).not.toHaveBeenCalled();
});
});

describe('is enabled changes', () => {
it('should let consumers know', () => {
const callbacks: Callbacks = getCallbacksStub();
const marshal: DimensionMarshal = createDimensionMarshal(callbacks);
populateMarshal(marshal);

// initial lift
marshal.startPublishing(defaultRequest, preset.windowScroll);
marshal.collect({ includeCritical: false });
requestAnimationFrame.flush();
expect(callbacks.updateDroppableIsEnabled).not.toHaveBeenCalled();

marshal.updateDroppableIsEnabled(critical.droppable.id, false);
expect(callbacks.updateDroppableIsEnabled)
.toHaveBeenCalledWith(critical.droppable.id, false);
});

it('should throw if the droppable cannot be found', () => {
const callbacks: Callbacks = getCallbacksStub();
const marshal: DimensionMarshal = createDimensionMarshal(callbacks);
populateMarshal(marshal, criticalDimensions);

// initial lift
marshal.startPublishing(defaultRequest, preset.windowScroll);
marshal.collect({ includeCritical: false });
requestAnimationFrame.flush();
expect(callbacks.updateDroppableIsEnabled).not.toHaveBeenCalled();

expect(() => marshal.updateDroppableIsEnabled(preset.foreign.descriptor.id, false))
.toThrow(`Cannot update the scroll on Droppable ${preset.foreign.descriptor.id} as it is not registered`);
});

it('should not let consumers know if no collection is occurring', () => {
const callbacks: Callbacks = getCallbacksStub();
const marshal: DimensionMarshal = createDimensionMarshal(callbacks);
populateMarshal(marshal, criticalDimensions);

marshal.updateDroppableIsEnabled(critical.droppable.id, false);
expect(callbacks.updateDroppableIsEnabled).not.toHaveBeenCalled();
});
});
30 changes: 4 additions & 26 deletions test/unit/view/dimension-marshal/registration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,13 @@ import type {
} from '../../../../src/types';
import {
populateMarshal,
resetWatcher,
type DimensionWatcher,
getDroppableCallbacks,
withExpectedAdvancedUsageWarning,
getCallbacksStub,
} from './util';

const preset = getPreset();

const getCallbacksStub = (): Callbacks => ({
bulkReplace: jest.fn(),
updateDroppableScroll: jest.fn(),
updateDroppableIsEnabled: jest.fn(),
bulkCollectionStarting: jest.fn(),
});

const getDroppableCallbacks = (dimension: DroppableDimension): DroppableCallbacks => ({
getDimensionAndWatchScroll: jest.fn().mockReturnValue(dimension),
scroll: jest.fn(),
unwatchScroll: jest.fn(),
hidePlaceholder: jest.fn(),
showPlaceholder: jest.fn(),
});

const withExpectedAdvancedUsageWarning = (fn: Function) => {
jest.spyOn(console, 'warn').mockImplementation(() => { });
fn();
expect(console.warn).toHaveBeenCalledWith(expect.stringContaining('Advanced usage warning'));
console.warn.mockRestore();
};

const critical: Critical = {
draggable: preset.inHome1.descriptor,
droppable: preset.home.descriptor,
Expand Down Expand Up @@ -368,8 +347,7 @@ describe('draggable', () => {
marshal.registerDraggable(preset.inHome3.descriptor, () => preset.inHome3);
marshal.unregisterDraggable(preset.inHome3.descriptor);

const droppableCallbacks: DroppableCallbacks = getDroppableCallbacks();
droppableCallbacks.getDimensionAndWatchScroll.mockReturnValue(preset.home);
const droppableCallbacks: DroppableCallbacks = getDroppableCallbacks(preset.home);
marshal.registerDroppable(preset.home.descriptor, droppableCallbacks);

const result: StartPublishingResult =
Expand Down
29 changes: 27 additions & 2 deletions test/unit/view/dimension-marshal/util.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// @flow
import { type Position } from 'css-box-model';
import { getPreset } from '../../../utils/dimension';
import type {
DimensionMarshal,
DroppableCallbacks,
Callbacks,
} from '../../../../src/state/dimension-marshal/dimension-marshal-types';
import type {
DimensionMap,
Expand Down Expand Up @@ -59,8 +61,8 @@ export const populateMarshal = (
watcher.droppable.getDimensionAndWatchScroll(id);
return droppable;
},
scroll: () => {
watcher.droppable.scroll(id);
scroll: (change: Position) => {
watcher.droppable.scroll(id, change);
},
unwatchScroll: () => {
watcher.droppable.unwatchScroll(id);
Expand All @@ -87,3 +89,26 @@ export const populateMarshal = (

return watcher;
};

export const getDroppableCallbacks = (dimension: DroppableDimension): DroppableCallbacks => ({
getDimensionAndWatchScroll: jest.fn().mockReturnValue(dimension),
scroll: jest.fn(),
unwatchScroll: jest.fn(),
hidePlaceholder: jest.fn(),
showPlaceholder: jest.fn(),
});

export const withExpectedAdvancedUsageWarning = (fn: Function) => {
jest.spyOn(console, 'warn').mockImplementation(() => { });
fn();
expect(console.warn).toHaveBeenCalledWith(expect.stringContaining('Advanced usage warning'));
console.warn.mockRestore();
};

export const getCallbacksStub = (): Callbacks => ({
bulkReplace: jest.fn(),
updateDroppableScroll: jest.fn(),
updateDroppableIsEnabled: jest.fn(),
bulkCollectionStarting: jest.fn(),
});

0 comments on commit 966b2c6

Please sign in to comment.