|
| 1 | +// @flow |
| 2 | +import React from 'react'; |
| 3 | +import { getRect } from 'css-box-model'; |
| 4 | +import invariant from 'tiny-invariant'; |
| 5 | +import { mount, type ReactWrapper } from 'enzyme'; |
| 6 | +import { DragDropContext, Draggable, Droppable } from '../../../src'; |
| 7 | +import { getComputedSpacing } from '../../utils/dimension'; |
| 8 | +import { withKeyboard } from '../../utils/user-input-util'; |
| 9 | +import * as keyCodes from '../../../src/view/key-codes'; |
| 10 | +import type { Provided as DraggableProvided } from '../../../src/view/draggable/draggable-types'; |
| 11 | +import type { Provided as DroppableProvided } from '../../../src/view/droppable/droppable-types'; |
| 12 | +import type { Hooks } from '../../../src/types'; |
| 13 | + |
| 14 | +const pressSpacebar = withKeyboard(keyCodes.space); |
| 15 | + |
| 16 | +type ItemProps = {| |
| 17 | + provided: DraggableProvided, |
| 18 | + onRender: Function, |
| 19 | +|}; |
| 20 | + |
| 21 | +class Item extends React.Component<ItemProps> { |
| 22 | + render() { |
| 23 | + this.props.onRender(); |
| 24 | + const provided: DraggableProvided = this.props.provided; |
| 25 | + return ( |
| 26 | + <div |
| 27 | + className="drag-handle" |
| 28 | + ref={provided.innerRef} |
| 29 | + {...provided.draggableProps} |
| 30 | + {...provided.dragHandleProps} |
| 31 | + > |
| 32 | + <h4>Draggable</h4> |
| 33 | + </div> |
| 34 | + ); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +jest.useFakeTimers(); |
| 39 | + |
| 40 | +it('should call the onDragStart before any connected components are updated', () => { |
| 41 | + let onDragStartTime: ?DOMHighResTimeStamp = null; |
| 42 | + let renderTime: ?DOMHighResTimeStamp = null; |
| 43 | + const hooks: Hooks = { |
| 44 | + onDragStart: jest.fn().mockImplementation(() => { |
| 45 | + invariant(!onDragStartTime, 'onDragStartTime already set'); |
| 46 | + onDragStartTime = performance.now(); |
| 47 | + }), |
| 48 | + onDragEnd: jest.fn(), |
| 49 | + }; |
| 50 | + const onItemRender = jest.fn().mockImplementation(() => { |
| 51 | + invariant(!renderTime, 'renderTime already set'); |
| 52 | + renderTime = performance.now(); |
| 53 | + }); |
| 54 | + // Both list and item will have the same dimensions |
| 55 | + jest |
| 56 | + .spyOn(Element.prototype, 'getBoundingClientRect') |
| 57 | + .mockImplementation(() => |
| 58 | + getRect({ |
| 59 | + top: 0, |
| 60 | + left: 0, |
| 61 | + right: 100, |
| 62 | + bottom: 100, |
| 63 | + }), |
| 64 | + ); |
| 65 | + |
| 66 | + // Stubbing out totally - not including margins in this |
| 67 | + jest |
| 68 | + .spyOn(window, 'getComputedStyle') |
| 69 | + .mockImplementation(() => getComputedSpacing({})); |
| 70 | + const wrapper: ReactWrapper = mount( |
| 71 | + <DragDropContext {...hooks}> |
| 72 | + <Droppable droppableId="droppable"> |
| 73 | + {(droppableProvided: DroppableProvided) => ( |
| 74 | + <div |
| 75 | + ref={droppableProvided.innerRef} |
| 76 | + {...droppableProvided.droppableProps} |
| 77 | + > |
| 78 | + <h2>Droppable</h2> |
| 79 | + <Draggable draggableId="draggable" index={0}> |
| 80 | + {(draggableProvided: DraggableProvided) => ( |
| 81 | + <Item onRender={onItemRender} provided={draggableProvided} /> |
| 82 | + )} |
| 83 | + </Draggable> |
| 84 | + </div> |
| 85 | + )} |
| 86 | + </Droppable> |
| 87 | + </DragDropContext>, |
| 88 | + ); |
| 89 | + |
| 90 | + pressSpacebar(wrapper.find('.drag-handle')); |
| 91 | + |
| 92 | + // clearing the first call |
| 93 | + expect(onItemRender).toHaveBeenCalledTimes(1); |
| 94 | + renderTime = null; |
| 95 | + onItemRender.mockClear(); |
| 96 | + |
| 97 | + // run out prepare phase |
| 98 | + jest.runOnlyPendingTimers(); |
| 99 | + |
| 100 | + // checking values are set |
| 101 | + invariant(onDragStartTime, 'onDragStartTime should be set'); |
| 102 | + invariant(renderTime, 'renderTime should be set'); |
| 103 | + |
| 104 | + // core assertion |
| 105 | + expect(onDragStartTime).toBeLessThan(renderTime); |
| 106 | + |
| 107 | + // validation |
| 108 | + expect(hooks.onDragStart).toHaveBeenCalledTimes(1); |
| 109 | + expect(onItemRender).toHaveBeenCalledTimes(1); |
| 110 | +}); |
0 commit comments