Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ScrollController): Replace the hashStore node variable with getter #600

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading