Skip to content

Commit

Permalink
Correct preserveScrollPosition calculation in nested controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaurav0 committed Mar 6, 2023
1 parent 2f17728 commit c47a107
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
15 changes: 10 additions & 5 deletions addon/services/router-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,18 @@ class RouterScroll extends Service {
this.unsetFirstLoad();
}

let scrollPosition = this.position;
let scrollPosition = this.position || { x: 0, y: 0 };

// If `preserveScrollPosition` was not set on the controller, attempt fallback to `preserveScrollPosition` which was set on the router service.
let preserveScrollPosition =
(transition.router.currentRouteInfos || []).some(
(routeInfo) => routeInfo.route.controller.preserveScrollPosition
) || this.preserveScrollPosition;
let preserveScrollPosition = false;
(transition.router.currentRouteInfos || []).forEach((routeInfo) => {
let thisPreserveScrollPosition =
routeInfo.route.controller.preserveScrollPosition;
preserveScrollPosition =
typeof thisPreserveScrollPosition === 'boolean' &&
thisPreserveScrollPosition;
});
preserveScrollPosition ||= this.preserveScrollPosition;

if (!preserveScrollPosition) {
const { scrollElement, targetElement, currentURL } = this;
Expand Down
37 changes: 34 additions & 3 deletions tests/unit/router-scroll-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ module('router-scroll', function (hooks) {
window.scrollTo = scrollTo;
});

function getTransitionsMock(URL, isPreserveScroll) {
function getTransitionsMock(URL, isPreserveScroll = false, nested = false) {
Object.defineProperty(subject, 'currentURL', {
value: URL || 'Hello/World',
});

const transition = {
handler: {
controller: {
preserveScrollPosition: isPreserveScroll || false,
preserveScrollPosition: nested || isPreserveScroll,
},
},
router: {
currentRouteInfos: [
{
route: {
controller: {
preserveScrollPosition: isPreserveScroll || false,
preserveScrollPosition: !nested && isPreserveScroll,
},
},
},
Expand Down Expand Up @@ -177,6 +177,37 @@ module('router-scroll', function (hooks) {
await settled();
});

test('Update Scroll Position: Can override preserveScrollPosition', async function (assert) {
assert.expect(1);
const done = assert.async();

window.scrollTo = () => {
assert.ok(true, 'Scroll To should be called');
done();
};

this.owner.register(
'service:fastboot',
class extends EmberObject {
isFastBoot = false;
}
);
const routerScrollService = this.owner.lookup('service:router-scroll');
Object.defineProperty(routerScrollService, 'position', {
get position() {
return { x: 0, y: 0 };
},
});
routerScrollService.scrollElement = 'window';

subject = this.owner.lookup('service:router');
subject.trigger(
'routeDidChange',
getTransitionsMock('Hello/World', false, true)
);
await settled();
});

test('Update Scroll Position: Can preserve position using routerService', async function (assert) {
assert.expect(0);

Expand Down

0 comments on commit c47a107

Please sign in to comment.