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

Commit ed85847

Browse files
committed
test(e2e.callbacks): access sortable object inside helper
1 parent f5b9ce5 commit ed85847

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

test/sortable.e2e.callbacks.spec.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,64 @@ describe('uiSortable', function() {
457457
});
458458
});
459459

460+
it('should provide the item.sortable properties on helper callback', function() {
461+
inject(function($compile, $rootScope) {
462+
var element, helperItem, itemSortable_Restore, sortableAfterRestore, helperCallbackExpectations;
463+
element = $compile(''.concat(
464+
'<ul ui-sortable="opts" ng-model="items">',
465+
beforeLiElement,
466+
'<li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li>',
467+
afterLiElement +
468+
'</ul>'))($rootScope);
469+
$rootScope.$apply(function() {
470+
$rootScope.opts = {
471+
helper: function(e, item) {
472+
helperItem = item;
473+
474+
var oldRestore = item.sortable._restore;
475+
item.sortable._restore = function () {
476+
oldRestore.apply(this, arguments);
477+
// hold the value of the sortable object
478+
// right after the _restore method completes
479+
sortableAfterRestore = item.sortable;
480+
};
481+
482+
spyOn(item.sortable, '_restore').and.callThrough();
483+
itemSortable_Restore = item.sortable._restore;
484+
helperCallbackExpectations(item.sortable);
485+
return item.clone();
486+
}
487+
};
488+
$rootScope.items = ['One', 'Two', 'Three'];
489+
});
490+
491+
host.append(element);
492+
493+
$rootScope.$apply(function() {
494+
});
495+
496+
var li = element.find('[ng-repeat]:eq(0)');
497+
var dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
498+
helperCallbackExpectations = function(helperItemSortable) {
499+
expect(helperItemSortable.model).toEqual('One');
500+
expect(helperItemSortable.index).toEqual(0);
501+
expect(helperItemSortable.source.length).toEqual(1);
502+
expect(helperItemSortable.source[0]).toBe(host.children()[0]);
503+
expect(helperItemSortable.sourceModel).toBe($rootScope.items);
504+
};
505+
li.simulate('drag', { dy: dy });
506+
expect($rootScope.items).toEqual(['Two', 'Three', 'One']);
507+
expect($rootScope.items).toEqual(listContent(element));
508+
expect(itemSortable_Restore).toHaveBeenCalled();
509+
expect(hasUndefinedProperties(helperItem.sortable)).toBe(true);
510+
// this happens after the update callback, so everything is udnefined
511+
expect(typeof sortableAfterRestore).toBe('function');
512+
helperItem = itemSortable_Restore = sortableAfterRestore = helperCallbackExpectations = undefined;
513+
514+
$(element).remove();
515+
});
516+
});
517+
460518
it('should properly reset a deleted callback option', function() {
461519
inject(function($compile, $rootScope) {
462520
var element, logsElement;

0 commit comments

Comments
 (0)