Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit d93316f

Browse files
committed
test(uiSelectCtrl): test active element is scrolled into view on open
1 parent 6e6748f commit d93316f

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

test/select.spec.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3458,4 +3458,104 @@ describe('ui-select tests', function () {
34583458
expect(el.scope().$select.activeIndex).toBe(2);
34593459
});
34603460
});
3461+
describe('Test scrolling to highlighted item after opening', function () {
3462+
3463+
beforeEach(function () {
3464+
3465+
scope.people = scope.people.concat([
3466+
{ name: 'Elvis', email: '[email protected]', group: 'Foo', age: 23 },
3467+
{ name: 'Ace', email: '[email protected]', group: 'Foo', age: 30 },
3468+
{ name: 'Mitchell', email: '[email protected]', group: 'Foo', age: 41 }
3469+
]);
3470+
});
3471+
3472+
it('Should set ctrl.active index to the selected person index', function () {
3473+
3474+
var lastIndex = scope.people.length - 1;
3475+
scope.selection.selected = scope.people[lastIndex];
3476+
3477+
var el = createUiSelect();
3478+
clickMatch(el);
3479+
3480+
expect(el.scope().$select.activeIndex).toEqual(lastIndex);
3481+
});
3482+
3483+
it('Should set ctrl.active index to the selected with `resetSearchInput = false`', function () {
3484+
3485+
var lastIndex = scope.people.length - 1;
3486+
scope.selection.selected = scope.people[lastIndex];
3487+
3488+
var el = createUiSelect({ resetSearchInput: false});
3489+
clickMatch(el);
3490+
3491+
expect(el.scope().$select.activeIndex).toEqual(lastIndex);
3492+
});
3493+
3494+
it('Should scroll the last item into view with animation enabled', inject(function ($animate) {
3495+
3496+
// This test should be updated with proper animation triggering and testing
3497+
// tried with `ngAnimateMock` but NO animation is triggered on digest or flush
3498+
3499+
scope.selection.selected = scope.people.slice().pop();
3500+
var el = createUiSelect();
3501+
var choicesContentEl = $(el).find('.ui-select-choices-content').get(0);
3502+
3503+
spyOn($animate, 'enabled').and.returnValue(true);
3504+
spyOn($animate, 'on');
3505+
3506+
clickMatch(el);
3507+
3508+
var animationHandler = $animate.on.calls.mostRecent().args[2];
3509+
animationHandler(choicesContentEl, 'close');
3510+
$timeout.flush();
3511+
3512+
var optionEl = $(el).find('.ui-select-choices-row div:contains("Mitchell")').get(0);
3513+
3514+
expect(choicesContentEl.scrollTop).toBeGreaterThan(0);
3515+
expect(isScrolledIntoContainer(choicesContentEl, optionEl)).toEqual(true);
3516+
}));
3517+
3518+
it('Should scroll the last item into view with animation disabled', inject(function ($animate) {
3519+
3520+
spyOn($animate, 'enabled').and.returnValue(false);
3521+
3522+
scope.selection.selected = scope.people.slice().pop();
3523+
3524+
var el = createUiSelect();
3525+
clickMatch(el);
3526+
$timeout.flush();
3527+
3528+
var choicesContentEl = $(el).find('.ui-select-choices-content').get(0);
3529+
var optionEl = $(el).find('.ui-select-choices-row div:contains("Mitchell")').get(0);
3530+
3531+
expect(choicesContentEl.scrollTop).toBeGreaterThan(0);
3532+
expect(isScrolledIntoContainer(choicesContentEl, optionEl)).toEqual(true);
3533+
}));
3534+
3535+
it('Should scroll the last item into view with `resetSearchInput = false`', function () {
3536+
3537+
scope.selection.selected = scope.people.slice().pop();
3538+
3539+
var el = createUiSelect({ resetSearchInput: false});
3540+
clickMatch(el);
3541+
$timeout.flush();
3542+
3543+
var choicesContentEl = $(el).find('.ui-select-choices-content').get(0);
3544+
var optionEl = $(el).find('.ui-select-choices-row div:contains("Mitchell")').get(0);
3545+
3546+
expect(choicesContentEl.scrollTop).toBeGreaterThan(0);
3547+
expect(isScrolledIntoContainer(choicesContentEl, optionEl)).toEqual(true);
3548+
});
3549+
3550+
function isScrolledIntoContainer(container, item)
3551+
{
3552+
var scrollTop = container.scrollTop;
3553+
var scrollBottom = scrollTop + container.clientHeight;
3554+
3555+
var itemTop = item.offsetTop;
3556+
var itemBottom = itemTop + item.clientHeight;
3557+
3558+
return (scrollTop <= itemTop) && (itemBottom <= scrollBottom);
3559+
}
3560+
});
34613561
});

0 commit comments

Comments
 (0)