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

Commit 9e63103

Browse files
committed
tests(sortable): add tests for the properties of ui.item.sortable
1 parent 35ee5f2 commit 9e63103

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed

test/sortable.e2e.callbacks.spec.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,78 @@ describe('uiSortable', function() {
189189
});
190190
});
191191

192+
it('should properly set ui.item.sortable properties', function() {
193+
inject(function($compile, $rootScope) {
194+
var element, updateCallbackExpectations;
195+
element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>')($rootScope);
196+
$rootScope.$apply(function() {
197+
$rootScope.opts = {
198+
update: function(e, ui) {
199+
if (ui.item.scope().item === 'Two') {
200+
ui.item.sortable.cancel();
201+
}
202+
updateCallbackExpectations(ui.item.sortable);
203+
}
204+
};
205+
$rootScope.items = ['One', 'Two', 'Three'];
206+
});
207+
208+
host.append(element);
209+
210+
$rootScope.$apply(function() {
211+
});
212+
var li = element.find(':eq(1)');
213+
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
214+
updateCallbackExpectations = function(uiItemSortable) {
215+
expect(uiItemSortable.model).toEqual('Two');
216+
expect(uiItemSortable.index).toEqual(1);
217+
expect(uiItemSortable.source.length).toEqual(1);
218+
expect(uiItemSortable.source[0]).toBe(host.children()[0]);
219+
expect(uiItemSortable.sourceModel).toBe($rootScope.items);
220+
expect(uiItemSortable.isCanceled()).toBe(true);
221+
expect(uiItemSortable.isCustomHelperUsed()).toBe(false);
222+
};
223+
li.simulate('drag', { dy: dy });
224+
expect($rootScope.items).toEqual(['One', 'Two', 'Three']);
225+
expect($rootScope.items).toEqual(listContent(element));
226+
updateCallbackExpectations = undefined;
227+
228+
li = element.find(':eq(0)');
229+
dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
230+
updateCallbackExpectations = function(uiItemSortable) {
231+
expect(uiItemSortable.model).toEqual('One');
232+
expect(uiItemSortable.index).toEqual(0);
233+
expect(uiItemSortable.source.length).toEqual(1);
234+
expect(uiItemSortable.source[0]).toBe(host.children()[0]);
235+
expect(uiItemSortable.sourceModel).toBe($rootScope.items);
236+
expect(uiItemSortable.isCanceled()).toBe(false);
237+
expect(uiItemSortable.isCustomHelperUsed()).toBe(false);
238+
};
239+
li.simulate('drag', { dy: dy });
240+
expect($rootScope.items).toEqual(['Two', 'Three', 'One']);
241+
expect($rootScope.items).toEqual(listContent(element));
242+
updateCallbackExpectations = undefined;
243+
244+
li = element.find(':eq(2)');
245+
dy = -(2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
246+
updateCallbackExpectations = function(uiItemSortable) {
247+
expect(uiItemSortable.model).toEqual('One');
248+
expect(uiItemSortable.index).toEqual(2);
249+
expect(uiItemSortable.source.length).toEqual(1);
250+
expect(uiItemSortable.source[0]).toBe(host.children()[0]);
251+
expect(uiItemSortable.sourceModel).toBe($rootScope.items);
252+
expect(uiItemSortable.isCanceled()).toBe(false);
253+
expect(uiItemSortable.isCustomHelperUsed()).toBe(false);
254+
};
255+
li.simulate('drag', { dy: dy });
256+
expect($rootScope.items).toEqual(['One', 'Two', 'Three']);
257+
expect($rootScope.items).toEqual(listContent(element));
258+
updateCallbackExpectations = undefined;
259+
260+
$(element).remove();
261+
});
262+
});
263+
192264
it('should properly free ui.item.sortable object', function() {
193265
inject(function($compile, $rootScope) {
194266
var element, uiItem, uiItemSortable_Destroy;

test/sortable.e2e.multi.spec.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,145 @@ describe('uiSortable', function() {
471471
});
472472
});
473473

474+
it('should properly set ui.item.sortable properties', function() {
475+
inject(function($compile, $rootScope) {
476+
var elementTop, elementBottom, updateCallbackExpectations, stopCallbackExpectations;
477+
elementTop = $compile('<ul ui-sortable="opts" class="cross-sortable" ng-model="itemsTop"><li ng-repeat="item in itemsTop" id="s-top-{{$index}}">{{ item }}</li></ul>')($rootScope);
478+
elementBottom = $compile('<ul ui-sortable="opts" class="cross-sortable" ng-model="itemsBottom"><li ng-repeat="item in itemsBottom" id="s-bottom-{{$index}}">{{ item }}</li></ul>')($rootScope);
479+
$rootScope.$apply(function() {
480+
$rootScope.itemsTop = ['Top One', 'Top Two', 'Top Three'];
481+
$rootScope.itemsBottom = ['Bottom One', 'Bottom Two', 'Bottom Three'];
482+
$rootScope.opts = {
483+
connectWith: '.cross-sortable',
484+
update: function(e, ui) {
485+
if (ui.item.scope() &&
486+
(typeof ui.item.scope().item === 'string') &&
487+
ui.item.scope().item.indexOf('Two') >= 0) {
488+
ui.item.sortable.cancel();
489+
}
490+
updateCallbackExpectations(ui.item.sortable);
491+
},
492+
stop: function(e, ui) {
493+
stopCallbackExpectations(ui.item.sortable);
494+
}
495+
};
496+
});
497+
498+
host.append(elementTop).append(elementBottom).append('<div class="clear"></div>');
499+
500+
var li1 = elementTop.find(':eq(1)');
501+
var li2 = elementBottom.find(':eq(0)');
502+
updateCallbackExpectations = function(uiItemSortable) {
503+
expect(uiItemSortable.model).toEqual('Top Two');
504+
expect(uiItemSortable.index).toEqual(1);
505+
expect(uiItemSortable.source.length).toEqual(1);
506+
expect(uiItemSortable.source[0]).toBe(host.children()[0]);
507+
expect(uiItemSortable.sourceModel).toBe($rootScope.itemsTop);
508+
expect(uiItemSortable.isCanceled()).toBe(true);
509+
expect(uiItemSortable.isCustomHelperUsed()).toBe(false);
510+
511+
expect(uiItemSortable.dropindex).toEqual(1);
512+
expect(uiItemSortable.droptarget.length).toBe(1);
513+
expect(uiItemSortable.droptarget[0]).toBe(host.children()[1]);
514+
expect(uiItemSortable.droptargetModel).toBe($rootScope.itemsBottom);
515+
};
516+
stopCallbackExpectations = function(uiItemSortable) {
517+
expect(uiItemSortable.received).toBe(true);
518+
expect(uiItemSortable.moved).toBe(undefined);
519+
};
520+
simulateElementDrag(li1, li2, { place: 'below', extradx: -20, extrady: -10 });
521+
expect($rootScope.itemsTop).toEqual(['Top One', 'Top Two', 'Top Three']);
522+
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Bottom Two', 'Bottom Three']);
523+
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
524+
expect($rootScope.itemsBottom).toEqual(listContent(elementBottom));
525+
updateCallbackExpectations = stopCallbackExpectations = undefined;
526+
527+
li1 = elementBottom.find(':eq(1)');
528+
li2 = elementTop.find(':eq(1)');
529+
updateCallbackExpectations = function(uiItemSortable) {
530+
expect(uiItemSortable.model).toEqual('Bottom Two');
531+
expect(uiItemSortable.index).toEqual(1);
532+
expect(uiItemSortable.source.length).toEqual(1);
533+
expect(uiItemSortable.source[0]).toBe(host.children()[1]);
534+
expect(uiItemSortable.sourceModel).toBe($rootScope.itemsBottom);
535+
expect(uiItemSortable.isCanceled()).toBe(true);
536+
expect(uiItemSortable.isCustomHelperUsed()).toBe(false);
537+
538+
expect(uiItemSortable.dropindex).toEqual(1);
539+
expect(uiItemSortable.droptarget.length).toBe(1);
540+
expect(uiItemSortable.droptarget[0]).toBe(host.children()[0]);
541+
expect(uiItemSortable.droptargetModel).toBe($rootScope.itemsTop);
542+
};
543+
stopCallbackExpectations = function(uiItemSortable) {
544+
expect(uiItemSortable.received).toBe(true);
545+
expect(uiItemSortable.moved).toBe(undefined);
546+
};
547+
simulateElementDrag(li1, li2, { place: 'above', extradx: -20, extrady: -10 });
548+
expect($rootScope.itemsTop).toEqual(['Top One', 'Top Two', 'Top Three']);
549+
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Bottom Two', 'Bottom Three']);
550+
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
551+
expect($rootScope.itemsBottom).toEqual(listContent(elementBottom));
552+
updateCallbackExpectations = stopCallbackExpectations = undefined;
553+
554+
li1 = elementTop.find(':eq(0)');
555+
li2 = elementBottom.find(':eq(0)');
556+
updateCallbackExpectations = function(uiItemSortable) {
557+
expect(uiItemSortable.model).toEqual('Top One');
558+
expect(uiItemSortable.index).toEqual(0);
559+
expect(uiItemSortable.source.length).toEqual(1);
560+
expect(uiItemSortable.source[0]).toBe(host.children()[0]);
561+
expect(uiItemSortable.sourceModel).toBe($rootScope.itemsTop);
562+
expect(uiItemSortable.isCanceled()).toBe(false);
563+
expect(uiItemSortable.isCustomHelperUsed()).toBe(false);
564+
565+
expect(uiItemSortable.dropindex).toEqual(1);
566+
expect(uiItemSortable.droptarget.length).toBe(1);
567+
expect(uiItemSortable.droptarget[0]).toBe(host.children()[1]);
568+
expect(uiItemSortable.droptargetModel).toBe($rootScope.itemsBottom);
569+
};
570+
stopCallbackExpectations = function(uiItemSortable) {
571+
expect(uiItemSortable.received).toBe(true);
572+
expect(uiItemSortable.moved).toBe('Top One');
573+
};
574+
simulateElementDrag(li1, li2, 'below');
575+
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top Three']);
576+
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Top One', 'Bottom Two', 'Bottom Three']);
577+
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
578+
expect($rootScope.itemsBottom).toEqual(listContent(elementBottom));
579+
updateCallbackExpectations = stopCallbackExpectations = undefined;
580+
581+
li1 = elementBottom.find(':eq(1)');
582+
li2 = elementTop.find(':eq(1)');
583+
updateCallbackExpectations = function(uiItemSortable) {
584+
expect(uiItemSortable.model).toEqual('Top One');
585+
expect(uiItemSortable.index).toEqual(1);
586+
expect(uiItemSortable.source.length).toEqual(1);
587+
expect(uiItemSortable.source[0]).toBe(host.children()[1]);
588+
expect(uiItemSortable.sourceModel).toBe($rootScope.itemsBottom);
589+
expect(uiItemSortable.isCanceled()).toBe(false);
590+
expect(uiItemSortable.isCustomHelperUsed()).toBe(false);
591+
592+
expect(uiItemSortable.dropindex).toEqual(1);
593+
expect(uiItemSortable.droptarget.length).toBe(1);
594+
expect(uiItemSortable.droptarget[0]).toBe(host.children()[0]);
595+
expect(uiItemSortable.droptargetModel).toBe($rootScope.itemsTop);
596+
};
597+
stopCallbackExpectations = function(uiItemSortable) {
598+
expect(uiItemSortable.received).toBe(true);
599+
expect(uiItemSortable.moved).toBe('Top One');
600+
};
601+
simulateElementDrag(li1, li2, { place: 'above', extradx: -20, extrady: -10 });
602+
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top One', 'Top Three']);
603+
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Bottom Two', 'Bottom Three']);
604+
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
605+
expect($rootScope.itemsBottom).toEqual(listContent(elementBottom));
606+
updateCallbackExpectations = stopCallbackExpectations = undefined;
607+
608+
$(elementTop).remove();
609+
$(elementBottom).remove();
610+
});
611+
});
612+
474613
it('should properly free ui.item.sortable object', function() {
475614
inject(function($compile, $rootScope) {
476615
var elementTop, elementBottom, uiItem, uiItemSortable_Destroy;

0 commit comments

Comments
 (0)