Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const clock = testUtils.sinon.useFakeTimers();
const spy = sinon.spy();
viewDocument.on( 'selectionChangeDone', spy );
// Change selection.
changeFakeSelectionPressing( keyCodes.arrowdown );
// Wait 100ms.
clock.tick( 100 );
// Check if spy was called.
sinon.assert.notCalled( spy );
// Change selection one more time.
changeFakeSelectionPressing( keyCodes.arrowdown );
// Wait 210ms (debounced function should be called).
clock.tick( 210 );
sinon.assert.calledOnce( spy );
} );
_handleSelectionMove( keyCode ) {
const selection = this.document.selection;
const newSelection = new ViewSelection( selection.getRanges(), { backward: selection.isBackward, fake: false } );
// Left or up arrow pressed - move selection to start.
if ( keyCode == keyCodes.arrowleft || keyCode == keyCodes.arrowup ) {
newSelection.setTo( newSelection.getFirstPosition() );
}
// Right or down arrow pressed - move selection to end.
if ( keyCode == keyCodes.arrowright || keyCode == keyCodes.arrowdown ) {
newSelection.setTo( newSelection.getLastPosition() );
}
const data = {
oldSelection: selection,
newSelection,
domSelection: null
};
// Fire dummy selection change event.
this.document.fire( 'selectionChange', data );
// Call` #_fireSelectionChangeDoneDebounced` every time when `selectionChange` event is fired.
// This function is debounced what means that `selectionChangeDone` event will be fired only when
// defined int the function time will elapse since the last time the function was called.
// So `selectionChangeDone` will be fired when selection will stop changing.
it( 'should fire selectionChange event with new selection when down arrow key is pressed', () => {
return checkSelectionChange(
'foo[<strong>bar</strong>]baz',
keyCodes.arrowdown,
'foo<strong>bar</strong>[]baz'
);
} );
it( 'intercepts the arrow* events and overrides the default toolbar behavior', () => {
const keyEvtData = {
stopPropagation: sinon.spy()
};
keyEvtData.keyCode = keyCodes.arrowdown;
view.keystrokes.press( keyEvtData );
sinon.assert.calledOnce( keyEvtData.stopPropagation );
keyEvtData.keyCode = keyCodes.arrowup;
view.keystrokes.press( keyEvtData );
sinon.assert.calledTwice( keyEvtData.stopPropagation );
keyEvtData.keyCode = keyCodes.arrowleft;
view.keystrokes.press( keyEvtData );
sinon.assert.calledThrice( keyEvtData.stopPropagation );
keyEvtData.keyCode = keyCodes.arrowright;
view.keystrokes.press( keyEvtData );
sinon.assert.callCount( keyEvtData.stopPropagation, 4 );
} );
'[]foo',
{ keyCode: keyCodes.arrowright, altKey: true },
'[]foo'
);
test(
'should work correctly with modifier key: right arrow + shift',
'[]foo',
{ keyCode: keyCodes.arrowright, shiftKey: true },
'[]foo'
);
test(
'should work correctly with modifier key: down arrow + ctrl',
'[]foo',
{ keyCode: keyCodes.arrowdown, ctrlKey: true },
'[]foo'
);
test(
'should work correctly with modifier key: down arrow + alt',
'[]foo',
{ keyCode: keyCodes.arrowdown, altKey: true },
'[]foo'
);
test(
'should work correctly with modifier key: down arrow + shift',
'[]foo',
{ keyCode: keyCodes.arrowdown, shiftKey: true },
'[]foo'
);
it( 'should prevent default for down arrow key', ( ) => {
return checkEventPrevention( keyCodes.arrowdown );
} );
'[]foo',
keyCodes.arrowup,
'[]foo'
);
test(
'do nothing on non objects - right arrow',
'foo[]bar',
keyCodes.arrowright,
'foo[]bar'
);
test(
'do nothing on non objects - down arrow',
'foo[]bar',
keyCodes.arrowdown,
'foo[]bar'
);
test(
'do nothing on non objects - left arrow',
'foo[]bar',
keyCodes.arrowleft,
'foo[]bar'
);
test(
'do nothing on non objects - up arrow',
'foo[]bar',
keyCodes.arrowup,
'foo[]bar'
);
function isArrowKeyCode( keyCode ) {
return keyCode == keyCodes.arrowright ||
keyCode == keyCodes.arrowleft ||
keyCode == keyCodes.arrowup ||
keyCode == keyCodes.arrowdown;
}