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

Commit 8285a57

Browse files
committed
test(directiveOptions): add test for the ui-model-item option
1 parent c0ca9ce commit 8285a57

File tree

2 files changed

+161
-4
lines changed

2 files changed

+161
-4
lines changed

test/sortable.e2e.directiveoptions.spec.js

Lines changed: 155 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,13 @@ describe('uiSortable', function() {
206206
element = $compile(''.concat(
207207
'<ul ui-sortable="opts" ng-model="items">',
208208
beforeLiElement,
209-
'<li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li>',
209+
'<li ng-repeat="item in items" id="s-{{$index}}" sortable-item>{{ item }}</li>',
210210
afterLiElement +
211211
'</ul>'))($rootScope);
212212
$rootScope.$apply(function() {
213213
$rootScope.opts = {
214-
'ui-floating': true
214+
'ui-floating': true,
215+
'ui-model-items': '> [sortable-item]'
215216
};
216217
$rootScope.items = ['One', 'Two', 'Three'];
217218
});
@@ -240,6 +241,158 @@ describe('uiSortable', function() {
240241
});
241242
});
242243

244+
it('should work when custom "ui-model-items" option is used with an attribute selector', function() {
245+
inject(function($compile, $rootScope) {
246+
var element;
247+
element = $compile(''.concat(
248+
'<ul ui-sortable="opts" ng-model="items">',
249+
beforeLiElement,
250+
'<li ng-repeat="item in items" id="s-{{$index}}" class="sortable-item" ui-sortable-item>{{ item }}</li>',
251+
afterLiElement,
252+
'</ul>'))($rootScope);
253+
254+
var itemsSelector = '[ui-sortable-item]';
255+
$rootScope.$apply(function() {
256+
$rootScope.opts = {
257+
items: '> ' + itemsSelector,
258+
'ui-model-items': '> ' + itemsSelector
259+
};
260+
$rootScope.items = ['One', 'Two', 'Three'];
261+
});
262+
263+
host.append(element).append('<div class="clear"></div>');
264+
265+
var li = element.find(itemsSelector + ':eq(1)');
266+
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
267+
li.simulate('drag', { dy: dy });
268+
expect($rootScope.items).toEqual(['One', 'Three', 'Two']);
269+
expect($rootScope.items).toEqual(listContent(element));
270+
271+
li = element.find(itemsSelector + ':eq(1)');
272+
dy = -(1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
273+
li.simulate('drag', { dy: dy });
274+
expect($rootScope.items).toEqual(['Three', 'One', 'Two']);
275+
expect($rootScope.items).toEqual(listContent(element));
276+
277+
$(element).remove();
278+
});
279+
});
280+
281+
it('should work when custom "ui-model-items" option is used with a class selector', function() {
282+
inject(function($compile, $rootScope) {
283+
var element;
284+
element = $compile(''.concat(
285+
'<ul ui-sortable="opts" ng-model="items">',
286+
beforeLiElement,
287+
'<li ng-repeat="item in items" id="s-{{$index}}" class="sortable-item ui-sortable-item">{{ item }}</li>',
288+
afterLiElement,
289+
'</ul>'))($rootScope);
290+
291+
var itemsSelector = '.ui-sortable-item';
292+
$rootScope.$apply(function() {
293+
$rootScope.opts = {
294+
items: '> ' + itemsSelector,
295+
'ui-model-items': '> ' + itemsSelector
296+
};
297+
$rootScope.items = ['One', 'Two', 'Three'];
298+
});
299+
300+
host.append(element).append('<div class="clear"></div>');
301+
302+
var li = element.find(itemsSelector + ':eq(1)');
303+
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
304+
li.simulate('drag', { dy: dy });
305+
expect($rootScope.items).toEqual(['One', 'Three', 'Two']);
306+
expect($rootScope.items).toEqual(listContent(element));
307+
308+
li = element.find(itemsSelector + ':eq(1)');
309+
dy = -(1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
310+
li.simulate('drag', { dy: dy });
311+
expect($rootScope.items).toEqual(['Three', 'One', 'Two']);
312+
expect($rootScope.items).toEqual(listContent(element));
313+
314+
$(element).remove();
315+
});
316+
});
317+
318+
xit('should work with multiple [ng-repeat] when attribute "ui-model-items" selector', function() {
319+
inject(function($compile, $rootScope) {
320+
var element;
321+
element = $compile(''.concat(
322+
'<ul ui-sortable="opts" ng-model="items">',
323+
beforeLiElement,
324+
beforeLiElement.replace('<li>', '<li ng-repeat="item in items" id="pre-{{$index}}" class="sortable-item">{{ item }}'),
325+
'<li ng-repeat="item in items" id="s-{{$index}}" class="sortable-item" ui-sortable-item>{{ item }}</li>',
326+
afterLiElement.replace('<li>', '<li ng-repeat="item in items" id="after-{{$index}}" class="sortable-item">{{ item }}'),
327+
afterLiElement,
328+
'</ul>'))($rootScope);
329+
330+
var itemsSelector = '[ui-sortable-item]';
331+
$rootScope.$apply(function() {
332+
$rootScope.opts = {
333+
items: '> ' + itemsSelector,
334+
'ui-model-items': '> ' + itemsSelector
335+
};
336+
$rootScope.items = ['One', 'Two', 'Three'];
337+
});
338+
339+
host.append(element).append('<div class="clear"></div>');
340+
341+
var li = element.find(itemsSelector + ':eq(1)');
342+
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
343+
li.simulate('drag', { dy: dy });
344+
expect($rootScope.items).toEqual(['One', 'Three', 'Two']);
345+
expect($rootScope.items).toEqual(listContent(element, itemsSelector));
346+
347+
li = element.find(itemsSelector + ':eq(1)');
348+
dy = -(1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
349+
li.simulate('drag', { dy: dy });
350+
expect($rootScope.items).toEqual(['Three', 'One', 'Two']);
351+
expect($rootScope.items).toEqual(listContent(element, itemsSelector));
352+
353+
$(element).remove();
354+
});
355+
});
356+
357+
xit('should work with multiple [ng-repeat] when class "ui-model-items" selector', function() {
358+
inject(function($compile, $rootScope) {
359+
var element;
360+
element = $compile(''.concat(
361+
'<ul ui-sortable="opts" ng-model="items">',
362+
beforeLiElement,
363+
beforeLiElement.replace('<li>', '<li ng-repeat="item in items" id="pre-{{$index}}" class="sortable-item">{{ item }}'),
364+
'<li ng-repeat="item in items" id="s-{{$index}}" class="sortable-item ui-sortable-item">{{ item }}</li>',
365+
afterLiElement.replace('<li>', '<li ng-repeat="item in items" id="after-{{$index}}" class="sortable-item">{{ item }}'),
366+
afterLiElement,
367+
'</ul>'))($rootScope);
368+
369+
var itemsSelector = '.ui-sortable-item';
370+
$rootScope.$apply(function() {
371+
$rootScope.opts = {
372+
items: '> ' + itemsSelector,
373+
'ui-model-items': '> ' + itemsSelector
374+
};
375+
$rootScope.items = ['One', 'Two', 'Three'];
376+
});
377+
378+
host.append(element).append('<div class="clear"></div>');
379+
380+
var li = element.find(itemsSelector + ':eq(1)');
381+
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
382+
li.simulate('drag', { dy: dy });
383+
expect($rootScope.items).toEqual(['One', 'Three', 'Two']);
384+
expect($rootScope.items).toEqual(listContent(element, itemsSelector));
385+
386+
li = element.find(itemsSelector + ':eq(1)');
387+
dy = -(1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
388+
li.simulate('drag', { dy: dy });
389+
expect($rootScope.items).toEqual(['Three', 'One', 'Two']);
390+
expect($rootScope.items).toEqual(listContent(element, itemsSelector));
391+
392+
$(element).remove();
393+
});
394+
});
395+
243396
}
244397

245398
[0, 1].forEach(function(useExtraElements){

test/sortable.test-helper.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ angular.module('ui.sortable.testHelper', [])
44
.factory('sortableTestHelper', function () {
55
var EXTRA_DY_PERCENTAGE = 0.25;
66

7-
function listContent (list) {
7+
function listContent (list, contentSelector) {
8+
if (!contentSelector) {
9+
contentSelector = '[ng-repeat], [data-ng-repeat], [x-ng-repeat]';
10+
}
11+
812
if (list && list.length) {
9-
return list.children('[ng-repeat], [data-ng-repeat], [x-ng-repeat]').map(function(){ return this.innerHTML; }).toArray();
13+
return list.children(contentSelector).map(function(){ return this.innerHTML; }).toArray();
1014
}
1115
return [];
1216
}

0 commit comments

Comments
 (0)