Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In qunit, how to query the scrollbar height or detect if a scroll bar is present on an element?

I’m writing tests for my Ember components. Once of the more delicate issues is getting a specific element to scroll while not generating scroll bars elsewhere.

I’m trying to write a test that will tell me when code breaks my scrolling setup. I’ve tried a variety of syntaxes and strategies. get doesn’t work as scrollHeight is not a property. The qunit accessors don’t have a get property value. qunit hasProperty lets me detect if a property has a specific value, but not if it’s over or under or any comparison to another property.

const scrollElement = assert.dom('[data-test-user-list-ul-element]');

var hasVerticalScrollbar = scrollElement.get('scrollHeight') > scrollElement.get('clientHeight');

assert.true(hasVerticalScrollbar);

graphic showing desired item to test

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you’re very close!!

assert.dom doesn’t return an element, and elements and don’t have a get property, so you probably want to look at scrollHeight and clientHeight directly:

For example in My ember-primitives project, I calculate the scroll-top position like this:

function getTop(element?: Element | null | undefined) {
  assert('Could not find scroller', element);

  return element.scrollHeight - element.clientHeight;
}

so you could define a hasScrollBar like this:

function hasScrollbar(element?: Element | null | undefined) {
  assert(`Can't check if a scrollbar exists on an element, if the element doesn't exist`, element);

  return element.scrollHeight === element.clientHeight;
}

Here is an example set of tests for working with scroll position.


Using @ember/test-helpers, you can confidently acquire the element via selector (ignoring elements outside of your test container) via

import { find } from '@ember/test-helpers';

// ...

const scrollElement = find('[data-test-user-list-ul-element]');

// ...

assert.true(hasScrollbar(scrollElement), 'element has a scrollbar');

References:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading