Skip to content

Commit

Permalink
fix(frontend/scroll): no callback for disconnected elements
Browse files Browse the repository at this point in the history
  • Loading branch information
saschanaz committed Aug 13, 2023
1 parent 2896fc6 commit 0adbedd
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/frontend/src/scripts/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function getScrollPosition(el: HTMLElement | null): number {

export function onScrollTop(el: HTMLElement, cb: () => unknown, tolerance = 1, once = false) {
// とりあえず評価してみる
if (isTopVisible(el)) {
if (el.isConnected && isTopVisible(el)) {
cb();
if (once) return null;
}
Expand All @@ -54,7 +54,7 @@ export function onScrollBottom(el: HTMLElement, cb: () => unknown, tolerance = 1
const container = getScrollContainer(el);

// とりあえず評価してみる
if (isBottomVisible(el, tolerance, container)) {
if (el.isConnected && isBottomVisible(el, tolerance, container)) {
cb();
if (once) return null;
}
Expand Down
64 changes: 64 additions & 0 deletions packages/frontend/test/scroll.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/

import { describe, test, assert, afterEach } from 'vitest';
import { Window } from 'happy-dom';
import { onScrollBottom, onScrollTop } from '@/scripts/scroll';

describe('Scroll', () => {
describe('onScrollTop', () => {
test('Initial onScrollTop callback for connected elements', () => {
const { document } = new Window();
const div = document.createElement('div');
assert.strictEqual(div.scrollTop, 0);

document.body.append(div);

let called = false;
onScrollTop(div as any as HTMLElement, () => called = true);

assert.ok(called);
});

test('No onScrollTop callback for disconnected elements', () => {
const { document } = new Window();
const div = document.createElement('div');
assert.strictEqual(div.scrollTop, 0);

let called = false;
onScrollTop(div as any as HTMLElement, () => called = true);

assert.ok(!called);
});
});

describe('onScrollBottom', () => {
test('Initial onScrollBottom callback for connected elements', () => {
const { document } = new Window();
const div = document.createElement('div');
assert.strictEqual(div.scrollTop, 0);
(div as any).scrollHeight = 100; // happy-dom has no scrollHeight

document.body.append(div);

let called = false;
onScrollBottom(div as any as HTMLElement, () => called = true);

assert.ok(called);
});

test('No onScrollBottom callback for disconnected elements', () => {
const { document } = new Window();
const div = document.createElement('div');
assert.strictEqual(div.scrollTop, 0);
(div as any).scrollHeight = 100; // happy-dom has no scrollHeight

let called = false;
onScrollBottom(div as any as HTMLElement, () => called = true);

assert.ok(!called);
});
});
});

0 comments on commit 0adbedd

Please sign in to comment.