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

Commit 9db6116

Browse files
committed
Added e2e tests simple & $destroy(able) inner directives.
1 parent 484a2dc commit 9db6116

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

test/karma.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = function(config) {
2222
'bower_components/angular-mocks/angular-mocks.js',
2323
'src/sortable.js',
2424
'test/sortable.test-helper.js',
25+
'test/sortable.test-directives.js',
2526
'test/*.spec.js'
2627
],
2728

test/sortable.e2e.directives.spec.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
'use strict';
2+
3+
describe('uiSortable', function() {
4+
5+
// Ensure the sortable angular module is loaded
6+
beforeEach(module('ui.sortable'));
7+
beforeEach(module('ui.sortable.testHelper'));
8+
beforeEach(module('ui.sortable.testDirectives'));
9+
10+
var EXTRA_DY_PERCENTAGE, listContent, listInnerContent;
11+
12+
beforeEach(inject(function (sortableTestHelper) {
13+
EXTRA_DY_PERCENTAGE = sortableTestHelper.EXTRA_DY_PERCENTAGE;
14+
listContent = sortableTestHelper.listContent;
15+
listInnerContent = sortableTestHelper.listInnerContent;
16+
}));
17+
18+
describe('Inner directives related', function() {
19+
20+
var host;
21+
22+
beforeEach(inject(function() {
23+
host = $('<div id="test-host"></div>');
24+
$('body').append(host);
25+
}));
26+
27+
afterEach(function() {
28+
host.remove();
29+
host = null;
30+
});
31+
32+
it('should work when inner directives are used', function() {
33+
inject(function($compile, $rootScope) {
34+
var element;
35+
element = $compile(''.concat(
36+
'<ul ui-sortable="opts" ng-model="items">',
37+
'<li ng-repeat="item in items" id="s-{{$index}}" class="sortable-item">',
38+
'<ui-sortable-simple-test-directive ng-model="item"></ui-sortable-simple-test-directive>',
39+
'</li>',
40+
'</ul>'))($rootScope);
41+
42+
$rootScope.$apply(function() {
43+
$rootScope.opts = { };
44+
$rootScope.items = ['One', 'Two', 'Three'];
45+
});
46+
47+
host.append(element);
48+
49+
var li = element.find('> :eq(1)');
50+
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
51+
li.simulate('drag', { dy: dy });
52+
expect($rootScope.items).toEqual(['One', 'Three', 'Two']);
53+
expect($rootScope.items).toEqual(listInnerContent(element));
54+
55+
li = element.find('> :eq(1)');
56+
dy = -(1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
57+
li.simulate('drag', { dy: dy });
58+
expect($rootScope.items).toEqual(['Three', 'One', 'Two']);
59+
expect($rootScope.items).toEqual(listInnerContent(element));
60+
61+
$(element).remove();
62+
});
63+
});
64+
65+
it('should not $destroy direcives after sorting.', function() {
66+
inject(function($compile, $rootScope) {
67+
var element;
68+
element = $compile(''.concat(
69+
'<ul ui-sortable="opts" ng-model="items">',
70+
'<li ng-repeat="item in items" id="s-{{$index}}" class="sortable-item">',
71+
'<ui-sortable-destroyable-test-directive ng-model="item"></ui-sortable-destroyable-test-directive>',
72+
'</li>',
73+
'</ul>'))($rootScope);
74+
75+
$rootScope.$apply(function() {
76+
$rootScope.opts = { };
77+
$rootScope.items = ['One', 'Two', 'Three'];
78+
});
79+
80+
host.append(element);
81+
82+
var li = element.find('> :eq(1)');
83+
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
84+
li.simulate('drag', { dy: dy });
85+
expect($rootScope.items).toEqual(['One', 'Three', 'Two']);
86+
expect($rootScope.items).toEqual(listInnerContent(element));
87+
88+
li = element.find('> :eq(1)');
89+
dy = -(1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
90+
li.simulate('drag', { dy: dy });
91+
expect($rootScope.items).toEqual(['Three', 'One', 'Two']);
92+
expect($rootScope.items).toEqual(listInnerContent(element));
93+
94+
$(element).remove();
95+
});
96+
});
97+
98+
});
99+
100+
});

test/sortable.test-directives.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
angular.module('ui.sortable.testDirectives', [])
4+
.directive('uiSortableSimpleTestDirective',
5+
function() {
6+
return {
7+
restrict: 'AE',
8+
scope: true,
9+
require: '?ngModel',
10+
template: '<div>Directive: <span class="itemContent" ng-bind="text"></span> !!!</div>',
11+
link: function(scope, element, attrs, ngModel) {
12+
scope.$watch(attrs.ngModel, function(value) {
13+
scope.text = value;
14+
});
15+
}
16+
};
17+
}
18+
)
19+
.directive('uiSortableDestroyableTestDirective',
20+
function() {
21+
return {
22+
restrict: 'AE',
23+
scope: true,
24+
require: '?ngModel',
25+
template: '<div>$destroy(able) Directive: <span class="itemContent" ng-bind="text"></span> !!!</div>',
26+
link: function(scope, element, attrs, ngModel) {
27+
scope.$watch(attrs.ngModel, function(value) {
28+
scope.text = value;
29+
});
30+
31+
element.bind('$destroy', function() {
32+
element.html('');
33+
});
34+
}
35+
};
36+
}
37+
);
38+

0 commit comments

Comments
 (0)