Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Testing if sortable works after a reverted (not dropped) drag. #80

Merged
merged 1 commit into from
Dec 19, 2013
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion gruntFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = function(grunt) {
}
},
jshint: {
files: ['src/**/*.js', 'test/**/*.js', 'demo/**/*.js'],
files: ['src/**/*.js', 'test/**/*.js', 'demo/**/*.js', '!test/libs/*.js'],
options: {
curly: true,
eqeqeq: true,
Expand Down
60 changes: 60 additions & 0 deletions test/libs/jquery.simulate.dragandrevert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
;(function($, undefined) {
function findCenter(elem) {
var offset,
document = $(elem.ownerDocument);
elem = $(elem);
offset = elem.offset();

return {
x: offset.left + elem.outerWidth() / 2 - document.scrollLeft(),
y: offset.top + elem.outerHeight() / 2 - document.scrollTop()
};
}

$.extend($.simulate.prototype, {
simulateDragAndRevert: function() {
var i = 0,
target = this.target,
options = this.options,
center = findCenter(target),
x = Math.floor(center.x),
y = Math.floor(center.y),
dx = options.dx || 0,
dy = options.dy || 0,
moves = options.moves || 3,
coord = {
clientX: x,
clientY: y
};

this.simulateEvent(target, "mousedown", coord);

for (; i < moves; i++) {
x += dx / moves;
y += dy / moves;

coord = {
clientX: Math.round(x),
clientY: Math.round(y)
};

this.simulateEvent(document, "mousemove", coord);
}

for (i = 0; i < moves; i++) {
x -= dx / moves;
y -= dy / moves;

coord = {
clientX: Math.round(x),
clientY: Math.round(y)
};

this.simulateEvent(document, "mousemove", coord);
}

this.simulateEvent(target, "mouseup", coord);
this.simulateEvent(target, "click", coord);
}
});
})(jQuery);
41 changes: 41 additions & 0 deletions test/sortable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,47 @@ describe('uiSortable', function() {
});
});

it('should continue to work after a drag is reverted', function() {
inject(function($compile, $rootScope) {
var element;
element = $compile('<ul ui-sortable ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}" class="sortable-item">{{ item }}</li></ul>')($rootScope);
$rootScope.$apply(function() {
$rootScope.opts = {
placeholder: "sortable-item"
};
$rootScope.items = ["One", "Two", "Three"];
});

host.append(element);

var li = element.find(':eq(0)');
var dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
li.simulate('dragAndRevert', { dy: dy });
expect($rootScope.items).toEqual(["One", "Two", "Three"]);
expect($rootScope.items).toEqualListContent(element);

li = element.find(':eq(0)');
dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
li.simulate('drag', { dy: dy });
expect($rootScope.items).toEqual(["Two", "One", "Three"]);
expect($rootScope.items).toEqualListContent(element);

li = element.find(':eq(1)');
dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
li.simulate('drag', { dy: dy });
expect($rootScope.items).toEqual(["Two", "Three", "One"]);
expect($rootScope.items).toEqualListContent(element);

li = element.find(':eq(1)');
dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
li.simulate('drag', { dy: dy });
expect($rootScope.items).toEqual(["Two", "One", "Three"]);
expect($rootScope.items).toEqualListContent(element);

$(element).remove();
});
});

});

describe('Multiple sortables related', function() {
Expand Down
4 changes: 3 additions & 1 deletion test/test.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ files = [
JASMINE_ADAPTER,
'bower_components/jquery/jquery.js',
'bower_components/jquery-simulate/jquery.simulate.js',
'test/libs/jquery.simulate.dragandrevert.js',
'bower_components/jquery-ui/ui/jquery-ui.js',
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
Expand All @@ -13,9 +14,10 @@ files = [
singleRun = true;
autoWatch = false;
browsers = [ 'Chrome' ];
reporters = [ 'dots' ];

if (singleRun) {
reporters = [ 'coverage' ];
reporters.push('coverage');
preprocessors = { '**/src/*.js': 'coverage' };
coverageReporter = {
type : 'html',
Expand Down