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

Merges #127 and adds Directives e2e tests #128

Merged
merged 5 commits into from
Feb 23, 2014
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 bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-ui-sortable",
"version": "0.12.0",
"version": "0.12.1",
"description": "This directive allows you to jQueryUI Sortable.",
"author": "https://github.com/angular-ui/ui-sortable/graphs/contributors",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-ui-sortable",
"version": "0.12.0",
"version": "0.12.1",
"description": "This directive allows you to jQueryUI Sortable.",
"author": "https://github.com/angular-ui/ui-sortable/graphs/contributors",
"license": "MIT",
Expand Down
8 changes: 3 additions & 5 deletions src/sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,9 @@ angular.module('ui.sortable', [])
// the start and stop of repeat sections and sortable doesn't
// respect their order (even if we cancel, the order of the
// comments are still messed up).
savedNodes.detach();
if (element.sortable('option','helper') === 'clone') {
// first detach all the savedNodes and then restore all of them
// except .ui-sortable-helper element (which is placed last).
// That way it will be garbage collected.
// restore all the savedNodes except .ui-sortable-helper element
// (which is placed last). That way it will be garbage collected.
savedNodes = savedNodes.not(savedNodes.last());
}
savedNodes.appendTo(element);
Expand Down Expand Up @@ -153,7 +151,7 @@ angular.module('ui.sortable', [])
// if the item was not moved, then restore the elements
// so that the ngRepeat's comment are correct.
if((!('dropindex' in ui.item.sortable) || ui.item.sortable.isCanceled()) && element.sortable('option','helper') !== 'clone') {
savedNodes.detach().appendTo(element);
savedNodes.appendTo(element);
}
}
};
Expand Down
1 change: 1 addition & 0 deletions test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = function(config) {
'bower_components/angular-mocks/angular-mocks.js',
'src/sortable.js',
'test/sortable.test-helper.js',
'test/sortable.test-directives.js',
'test/*.spec.js'
],

Expand Down
100 changes: 100 additions & 0 deletions test/sortable.e2e.directives.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
'use strict';

describe('uiSortable', function() {

// Ensure the sortable angular module is loaded
beforeEach(module('ui.sortable'));
beforeEach(module('ui.sortable.testHelper'));
beforeEach(module('ui.sortable.testDirectives'));

var EXTRA_DY_PERCENTAGE, listContent, listInnerContent;

beforeEach(inject(function (sortableTestHelper) {
EXTRA_DY_PERCENTAGE = sortableTestHelper.EXTRA_DY_PERCENTAGE;
listContent = sortableTestHelper.listContent;
listInnerContent = sortableTestHelper.listInnerContent;
}));

describe('Inner directives related', function() {

var host;

beforeEach(inject(function() {
host = $('<div id="test-host"></div>');
$('body').append(host);
}));

afterEach(function() {
host.remove();
host = null;
});

it('should work when inner directives are used', function() {
inject(function($compile, $rootScope) {
var element;
element = $compile(''.concat(
'<ul ui-sortable="opts" ng-model="items">',
'<li ng-repeat="item in items" id="s-{{$index}}" class="sortable-item">',
'<ui-sortable-simple-test-directive ng-model="item"></ui-sortable-simple-test-directive>',
'</li>',
'</ul>'))($rootScope);

$rootScope.$apply(function() {
$rootScope.opts = { };
$rootScope.items = ['One', 'Two', 'Three'];
});

host.append(element);

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

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

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

it('should not $destroy directives after sorting.', function() {
inject(function($compile, $rootScope) {
var element;
element = $compile(''.concat(
'<ul ui-sortable="opts" ng-model="items">',
'<li ng-repeat="item in items" id="s-{{$index}}" class="sortable-item">',
'<ui-sortable-destroyable-test-directive ng-model="item"></ui-sortable-destroyable-test-directive>',
'</li>',
'</ul>'))($rootScope);

$rootScope.$apply(function() {
$rootScope.opts = { };
$rootScope.items = ['One', 'Two', 'Three'];
});

host.append(element);

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

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

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

});

});
38 changes: 38 additions & 0 deletions test/sortable.test-directives.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

angular.module('ui.sortable.testDirectives', [])
.directive('uiSortableSimpleTestDirective',
function() {
return {
restrict: 'AE',
scope: true,
require: '?ngModel',
template: '<div>Directive: <span class="itemContent" ng-bind="text"></span> !!!</div>',
link: function(scope, element, attrs, ngModel) {
scope.$watch(attrs.ngModel, function(value) {
scope.text = value;
});
}
};
}
)
.directive('uiSortableDestroyableTestDirective',
function() {
return {
restrict: 'AE',
scope: true,
require: '?ngModel',
template: '<div>$destroy(able) Directive: <span class="itemContent" ng-bind="text"></span> !!!</div>',
link: function(scope, element, attrs, ngModel) {
scope.$watch(attrs.ngModel, function(value) {
scope.text = value;
});

element.bind('$destroy', function() {
element.html('');
});
}
};
}
);