Skip to content

Commit

Permalink
explicitly test passive VS polling modes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan Hrabovyi committed Mar 1, 2023
1 parent 6d52717 commit 7067fed
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 17 deletions.
5 changes: 2 additions & 3 deletions addon/-private/data-view/utils/scroll-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ export class ScrollHandler {
left: element.scrollLeft,
handlers
};
// TODO add explicit test
if (SUPPORTS_PASSIVE) {

if (this.isUsingPassive) {
cache.passiveHandler = function() {
ScrollHandler.triggerElementHandlers(element, cache);
};

element.addEventListener('scroll', cache.passiveHandler, { capture: true, passive: true });
// TODO add explicit test
} else {
cache.passiveHandler = UNDEFINED_VALUE;

Expand Down
76 changes: 62 additions & 14 deletions tests/unit/-private/data-view/utils/scroll-handler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ module('Unit | Radar Utils | Scroll Handler');

test('We can add, trigger, and remove a scroll handler', (assert) => {
let scrollHandlers = new ScrollHandler();
scrollHandlers.isUsingPassive = true;

let done = assert.async(2);
let scrollable = createScrollable();
let handler = () => {
Expand All @@ -44,31 +46,81 @@ test('We can add, trigger, and remove a scroll handler', (assert) => {
// test adding a single handler
scrollHandlers.addScrollHandler(scrollable, handler);

assert.equal(scrollHandlers.length, 1, `We have one element to watch.`);
assert.strictEqual(scrollHandlers.length, 1, `We have one element to watch.`);
assert.false(scrollHandlers.isPolling, 'polling is inactive, using a passive handler');

let scrollableIndex = scrollHandlers.elements.indexOf(scrollable);
assert.ok(scrollableIndex !== -1, `The scrollable was added to the watched elements list.`);
assert.true(scrollableIndex !== -1, `The scrollable was added to the watched elements list.`);
let cache = scrollHandlers.handlers[scrollableIndex];
assert.ok(cache.handlers.length === 1);
assert.strictEqual(cache.handlers.length, 1);

// test triggering that handler
assert.equal(scrollable.scrollTop, 0, `The scrollable is initially unscrolled`);
assert.strictEqual(scrollable.scrollTop, 0, `The scrollable is initially unscrolled`);

afterNextScrollUpdate(() => {
scrollable.scrollTop = 10;
assert.equal(scrollable.scrollTop, 10, `We updated the scrollable's scroll position`);
assert.strictEqual(scrollable.scrollTop, 10, `We updated the scrollable's scroll position`);

afterNextScrollUpdate(() => {
// test removing that handler
scrollHandlers.removeScrollHandler(scrollable, handler);
let newScrollableIndex = scrollHandlers.elements.indexOf(scrollable);

assert.ok(cache.handlers.length === 0, `The handler was removed from the listener cache.`);
assert.ok(newScrollableIndex === -1, `Removing the last handler removed the element from the watched elements list.`);
assert.ok(scrollHandlers.handlers.indexOf(cache) === -1, `Removing the last handler removed the cache.`);
assert.strictEqual(cache.handlers.length, 0, `The handler was removed from the listener cache.`);
assert.strictEqual(newScrollableIndex, -1, `Removing the last handler removed the element from the watched elements list.`);
assert.strictEqual(scrollHandlers.handlers.indexOf(cache), -1, `Removing the last handler removed the cache.`);

assert.equal(scrollHandlers.length, 0, `We have no more elements to watch.`);
assert.equal(scrollHandlers.isPolling, false, `We are no longer polling the elements.`);
assert.strictEqual(scrollHandlers.length, 0, `We have no more elements to watch.`);
assert.false(scrollHandlers.isPolling, `polling is still inactive`);

destroyScrollable(scrollable);
done();
});
});
});

test('Polling', (assert) => {
let scrollHandlers = new ScrollHandler();
scrollHandlers.isUsingPassive = false;

let done = assert.async(2);
let scrollable = createScrollable();
let handler = () => {
assert.ok('handler was triggered');
done();
};

assert.strictEqual(scrollHandlers.length, 0, `We initially have no elements to watch.`);

// test adding a single handler
scrollHandlers.addScrollHandler(scrollable, handler);

assert.strictEqual(scrollHandlers.length, 1, `We have one element to watch.`);
assert.true(scrollHandlers.isPolling, 'polling is active');

let scrollableIndex = scrollHandlers.elements.indexOf(scrollable);
assert.true(scrollableIndex !== -1, `The scrollable was added to the watched elements list.`);
let cache = scrollHandlers.handlers[scrollableIndex];
assert.strictEqual(cache.handlers.length, 1);

// test triggering that handler
assert.strictEqual(scrollable.scrollTop, 0, `The scrollable is initially unscrolled`);

afterNextScrollUpdate(() => {
scrollable.scrollTop = 10;
assert.strictEqual(scrollable.scrollTop, 10, `We updated the scrollable's scroll position`);

afterNextScrollUpdate(() => {
// test removing that handler
scrollHandlers.removeScrollHandler(scrollable, handler);
let newScrollableIndex = scrollHandlers.elements.indexOf(scrollable);

assert.strictEqual(cache.handlers.length, 0, `The handler was removed from the listener cache.`);
assert.strictEqual(newScrollableIndex, -1, `Removing the last handler removed the element from the watched elements list.`);
assert.strictEqual(scrollHandlers.handlers.indexOf(cache), -1, `Removing the last handler removed the cache.`);

assert.strictEqual(scrollHandlers.length, 0, `We have no more elements to watch.`);
assert.false(scrollHandlers.isPolling, `We are no longer polling the elements.`);

destroyScrollable(scrollable);
done();
Expand Down Expand Up @@ -125,7 +177,6 @@ test('Adding/removing multiple handlers to an element works as expected', (asser
assert.ok(scrollHandlers.handlers.indexOf(cache) === -1, `Removing the last handler removed the cache.`);

assert.equal(scrollHandlers.length, 0, `We have no more elements to watch.`);
assert.equal(scrollHandlers.isPolling, false, `We are no longer polling the elements.`);

destroyScrollable(scrollable);
done();
Expand Down Expand Up @@ -192,7 +243,6 @@ test('Multiple elements with handlers works as expected', (assert) => {
assert.ok(scrollHandlers.handlers.indexOf(cache2) === -1, `Removing the last handler removed the cache.`);

assert.equal(scrollHandlers.length, 0, `We have no more elements to watch.`);
assert.equal(scrollHandlers.isPolling, false, `We are no longer polling the elements.`);

destroyScrollable(scrollable1);
destroyScrollable(scrollable2);
Expand Down Expand Up @@ -253,8 +303,6 @@ test('multiple handlers with same scrollable', (assert) => {
assert.strictEqual(scrollHandlers.length, 0, `We have no more elements to watch.`);
assert.strictEqual(cache1.handlers.length, 0, `The last handler was removed from the listener cache.`);

assert.false(scrollHandlers.isPolling, `We are no longer polling the elements.`);

destroyScrollable(scrollable);
done();
});
Expand Down

0 comments on commit 7067fed

Please sign in to comment.