Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resizable: add some event tests #12606

Merged
merged 5 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions js/ui/resizable.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,21 @@ 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
$('<div>')
.addClass(RESIZABLE_HANDLE_CLASS)
.addClass(RESIZABLE_HANDLE_CLASS + '-' + handleName)
.appendTo($element);
.appendTo(this.$element());
},

this._attachEventHandlers($handle);
_getHandlers: function() {
return this.$element().children('.' + RESIZABLE_HANDLE_CLASS);
},

_attachEventHandlers: function($handle) {
_attachEventHandlers: function() {
if(this.option('disabled')) {
return;
}
Expand All @@ -138,12 +138,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._getHandlers().each(function(index, handleElement) {
ksercs marked this conversation as resolved.
Show resolved Hide resolved
eventsEngine.on(handleElement, handlers, {
direction: 'both',
immediate: true
});
});
},

_detachEventHandlers: function() {
this._getHandlers().each(function(index, 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 +424,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();
});
});