Skip to content

Commit

Permalink
Resizable: add some event tests (#12606)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksercs authored Apr 9, 2020
1 parent 6cb5169 commit 40115d5
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 10 deletions.
34 changes: 24 additions & 10 deletions js/ui/resizable.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ const Resizable = DOMComponent.inherit({
},

_renderHandles: function() {
this._handles = [];
const handles = this.option('handles');

if(handles === 'none') {
Expand All @@ -114,21 +115,19 @@ const Resizable = DOMComponent.inherit({
inArray('bottom', directions) + 1 && inArray('left', directions) + 1 && this._renderHandle('corner-bottom-left');
inArray('top', directions) + 1 && inArray('right', directions) + 1 && this._renderHandle('corner-top-right');
inArray('top', directions) + 1 && inArray('left', directions) + 1 && this._renderHandle('corner-top-left');
this._attachEventHandlers();
},

_renderHandle: function(handleName) {
const $element = this.$element();
const $handle = $('<div>');

$handle
const $handle = $('<div>')
.addClass(RESIZABLE_HANDLE_CLASS)
.addClass(RESIZABLE_HANDLE_CLASS + '-' + handleName)
.appendTo($element);
.appendTo(this.$element());

this._attachEventHandlers($handle);
this._handles.push($handle);
},

_attachEventHandlers: function($handle) {
_attachEventHandlers: function() {
if(this.option('disabled')) {
return;
}
Expand All @@ -138,12 +137,24 @@ const Resizable = DOMComponent.inherit({
handlers[DRAGSTART_EVENT_NAME] = this._dragHandler.bind(this);
handlers[DRAGSTART_END_EVENT_NAME] = this._dragEndHandler.bind(this);

eventsEngine.on($handle, handlers, {
direction: 'both',
immediate: true
this._handles.forEach(function(handleElement) {
eventsEngine.on(handleElement, handlers, {
direction: 'both',
immediate: true
});
});
},

_detachEventHandlers: function() {
this._handles.forEach(function(handleElement) {
eventsEngine.off(handleElement);
});
},

_toggleEventHandlers: function(shouldAttachEvents) {
shouldAttachEvents ? this._attachEventHandlers() : this._detachEventHandlers();
},

_dragStartHandler: function(e) {
const $element = this.$element();
if($element.is('.dx-state-disabled, .dx-state-disabled *')) {
Expand Down Expand Up @@ -412,6 +423,9 @@ const Resizable = DOMComponent.inherit({
_optionChanged: function(args) {
switch(args.name) {
case 'disabled':
this._toggleEventHandlers(!args.value);
this.callBase(args);
break;
case 'handles':
this._invalidate();
break;
Expand Down
76 changes: 76 additions & 0 deletions testing/tests/DevExpress.ui.widgets/resizable.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,32 @@ QUnit.module('actions', () => {
pointer.dragStart();
});

QUnit.test('onResizeStart - subscription by "on" method', function(assert) {
assert.expect(3);

const resizeStartHandler = function(e) {
assert.ok(true, 'onResizeStart action fired');

assert.equal(e.width, $resizable.outerWidth(), 'width passed as event argument');
assert.equal(e.height, $resizable.outerHeight(), 'height passed as event argument');
};

const resize = () => {
const $handle = $resizable.find('.' + RESIZABLE_HANDLE_CORNER_CLASS + '-bottom-right');
const pointer = pointerMock($handle).start();
pointer.dragStart();
};

const $resizable = $('#resizable').dxResizable();
const instance = $resizable.dxResizable('instance');

instance.on('resizeStart', resizeStartHandler);
resize();

instance.off('resizeStart', resizeStartHandler);
resize();
});

QUnit.test('onResize action should be fired during resize', function(assert) {
assert.expect(3);

Expand All @@ -758,6 +784,32 @@ QUnit.module('actions', () => {
pointer.dragStart().drag(10, 0);
});

QUnit.test('onResize action - subscription by "on" method', function(assert) {
assert.expect(3);

const resizeHandler = function(e) {
assert.ok(true, 'onResize action fired');

assert.equal(e.width, $resizable.outerWidth(), 'width passed as event argument');
assert.equal(e.height, $resizable.outerHeight(), 'height passed as event argument');
};

const resize = () => {
const $handle = $resizable.find('.' + RESIZABLE_HANDLE_CORNER_CLASS + '-bottom-right');
const pointer = pointerMock($handle).start();
pointer.dragStart().drag(10, 0);
};

const $resizable = $('#resizable').dxResizable();
const instance = $resizable.dxResizable('instance');

instance.on('resize', resizeHandler);
resize();

instance.off('resize', resizeHandler);
resize();
});

QUnit.test('dxresize event should be fired during resize', function(assert) {
const $resizable = $('#resizable').dxResizable(); const $handle = $resizable.find('.' + RESIZABLE_HANDLE_CORNER_CLASS + '-bottom-right'); const pointer = pointerMock($handle).start();

Expand Down Expand Up @@ -813,5 +865,29 @@ QUnit.module('actions', () => {

pointer.dragStart().drag(10, 0).dragEnd();
});

QUnit.test('onResizeEnd action - subscription by "on" method', function(assert) {
const resizeEndHandler = function(e) {
assert.ok(true, 'onResizeEnd action fired');

assert.equal(e.width, $resizable.outerWidth(), 'width passed as event argument');
assert.equal(e.height, $resizable.outerHeight(), 'height passed as event argument');
};

const resize = () => {
const $handle = $resizable.find('.' + RESIZABLE_HANDLE_CORNER_CLASS + '-bottom-right');
const pointer = pointerMock($handle).start();
pointer.dragStart().drag(10, 0).dragEnd();
};

const $resizable = $('#resizable').dxResizable();
const instance = $resizable.dxResizable('instance');

instance.on('resizeEnd', resizeEndHandler);
resize();

instance.off('resizeEnd', resizeEndHandler);
resize();
});
});

0 comments on commit 40115d5

Please sign in to comment.