diff --git a/ilc/client/TransitionManager/ScrollController/ScrollController.js b/ilc/client/TransitionManager/ScrollController/ScrollController.js index 42feb7ad..c686049c 100644 --- a/ilc/client/TransitionManager/ScrollController/ScrollController.js +++ b/ilc/client/TransitionManager/ScrollController/ScrollController.js @@ -1,6 +1,5 @@ export class ScrollController { // @todo write e2e tests for hash position restoring - #hashStoreNode = document.body; #hashStoreAttribute = 'ilcTempStoredHash'; #lastVisitedUrl = this.location.pathname; #shouldScrollToTop = true; @@ -16,19 +15,24 @@ export class ScrollController { store() { if (this.location.hash) { - const node = this.#hashStoreNode; + const node = this.#getHashStoreNode(); const hashValue = this.#getHashValue(); node.setAttribute(this.#hashStoreAttribute, hashValue); } } restore() { - const node = this.#hashStoreNode; + const node = this.#getHashStoreNode(); // @todo: looks like it never used so storing is useless node.removeAttribute(this.#hashStoreAttribute); + this.#restoreScrollOnNavigation(); } + #getHashStoreNode() { + return document.body; + } + #restoreScrollOnNavigation() { let scrollToElement; if (this.location.hash) { diff --git a/ilc/client/TransitionManager/ScrollController/ScrollController.spec.js b/ilc/client/TransitionManager/ScrollController/ScrollController.spec.js index 70956fa1..a074c45a 100644 --- a/ilc/client/TransitionManager/ScrollController/ScrollController.spec.js +++ b/ilc/client/TransitionManager/ScrollController/ScrollController.spec.js @@ -51,6 +51,20 @@ describe('ScrollController', () => { } }); + describe('when the ScrollController is instantiated before the page ready event', () => { + let documentBodyStub; + + beforeEach(() => { + documentBodyStub = sinon.stub(document, 'body').get(() => undefined); + scrollController = new ScrollController(); + documentBodyStub.restore(); + }); + + it('should not throw error', () => { + expect(() => scrollController.restore()).to.not.throw(); + }); + }); + it('should remove the stored hash attribute from the document body', () => { document.body.setAttribute('ilcTempStoredHash', 'testAnchor'); scrollController.restore();