JS - Testing code that uses an IntersectionObserver : <p>I have a (rather poorly written) javascript component in my application that handles infinite scroll pagination, and i'm trying to rewrite it to use the <code>IntersectionObserver</code>, as described <a href="https://developers.google.com/web/updates/2016/04/intersectionobserver" rel="noreferrer">here</a>, however i'm having issues in testing it.</p>

<p>Is there a way to drive the behavior of the observer in a QUnit test, i.e. to trigger the observer callback with some entries described in my tests?</p>

<p>A possible solution I have come up with is to expose the callback function in the component's prototype and to invoke it directly in my test, something like this:</p>

<pre><code>InfiniteScroll.prototype.observerCallback = function(entries) {
    //handle the infinite scroll
}

InfiniteScroll.prototype.initObserver = function() {
    var io = new IntersectionObserver(this.observerCallback);
    io.observe(someElements);
}

//In my test
var component = new InfiniteScroll();
component.observerCallback(someEntries);
//Do some assertions about the state after the callback has been executed
</code></pre>

<p>I don't really like this approach since it's exposing the fact that the component uses an <code>IntersectionObserver</code> internally, which is an implementation detail that in my opinion should not be visible to client code, so is there any better way to test this?</p>

<p>Bonus love for solutions not using jQuery :)</p>
