Skip to content

Commit

Permalink
Merge pull request #600 from namecheap/fix/scroll-controller-hash-sto…
Browse files Browse the repository at this point in the history
…re-node

fix(ScrollController): Replace the hashStore node variable with getter
  • Loading branch information
yehor-manzhula authored Sep 4, 2024
2 parents cb5ed7b + 4d25a98 commit bf8c8ab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit bf8c8ab

Please sign in to comment.