Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should cancel when pressing escape', () => {
const onDragEnd = jest.fn();
const { getByText } = render();
const handle: HTMLElement = getByText('item: 0');
simpleLift(control, handle);
expect(isDragging(handle)).toBe(true);
// cancel
const event: Event = createEvent.keyDown(handle, {
keyCode: keyCodes.escape,
});
fireEvent(handle, event);
// event consumed
expect(event.defaultPrevented).toBe(true);
// drag ended
expect(isDragging(handle)).toBe(false);
expect(onDragEnd.mock.calls[0][0].reason).toBe('CANCEL');
});
it('should move up when pressing the up arrow', () => {
const onDragUpdate = jest.fn();
const { getByText } = render();
const handle: HTMLElement = getByText('item: 1');
simpleLift(keyboard, handle);
const event: Event = createEvent.keyDown(handle, {
keyCode: keyCodes.arrowUp,
});
fireEvent(handle, event);
// flush async responder
jest.runOnlyPendingTimers();
expect(onDragUpdate).toHaveBeenCalled();
expect(onDragUpdate.mock.calls[0][0].destination.index).toBe(0);
// event consumed
expect(event.defaultPrevented).toBe(true);
});
it('should prevent default on the event that causes a drop', () => {
const onDragEnd = jest.fn();
const { getByText } = render();
const handle: HTMLElement = getByText('item: 0');
simpleLift(keyboard, handle);
expect(isDragging(handle)).toBe(true);
const event: Event = createEvent.keyDown(handle, { keyCode: keyCodes.space });
fireEvent(handle, event);
expect(event.defaultPrevented).toBe(true);
expect(getDropReason(onDragEnd)).toBe('DROP');
});
it('should prevent default on an escape press', () => {
const onDragEnd = jest.fn();
const { getByText } = render();
const handle: HTMLElement = getByText('item: 0');
simpleLift(mouse, handle);
const event: Event = createEvent.keyDown(handle, {
keyCode: keyCodes.escape,
});
fireEvent(handle, event);
expect(event.defaultPrevented).toBe(true);
expect(getDropReason(onDragEnd)).toBe('CANCEL');
});
it('should prevent default on an escape press', () => {
const onDragEnd = jest.fn();
const { getByText } = render();
const handle: HTMLElement = getByText('item: 0');
simpleLift(touch, handle);
const event: Event = createEvent.keyDown(handle, {
keyCode: keyCodes.escape,
});
fireEvent(handle, event);
expect(event.defaultPrevented).toBe(true);
expect(getDropReason(onDragEnd)).toBe('CANCEL');
});
it('should prevent the default keyboard action when lifting', () => {
const { getByText } = render();
const handle: HTMLElement = getByText('item: 0');
const event: Event = createEvent.keyDown(handle, { keyCode: keyCodes.space });
fireEvent(handle, event);
expect(isDragging(handle)).toBe(true);
expect(event.defaultPrevented).toBe(true);
});
[keyCodes.enter, keyCodes.tab].forEach((keyCode: number) => {
const event: Event = createEvent.keyDown(handle, { keyCode });
fireEvent(handle, event);
expect(event.defaultPrevented).toBe(true);
expect(isDragging(handle)).toBe(true);
});
});
[keyCodes.enter, keyCodes.tab].forEach((keyCode: number) => {
const event: Event = createEvent.keyDown(handle, { keyCode });
fireEvent(handle, event);
expect(event.defaultPrevented).toBe(true);
});
});
keys.forEach((keyCode: number) => {
const event: Event = createEvent.keyDown(handle, { keyCode });
fireEvent(handle, event);
expect(event.defaultPrevented).toBe(true);
expect(isDragging(handle)).toBe(true);
});
});