5
votes

I need to trigger window scroll event to test infinite scrolling, I've tried to use triggerEvent, but it seems that I missing something and it doesn't work. I'm using Ember 2.0 and list is rendered inside the component if it matters. Test fails on last 2 assertions, scroll position doesn't change after triggering event

test 'loads more items when scrolling', (assert) ->
  visit '/locations/1'   
  andThen ->
    assert.equal(find('.items-list li').length, 30)

    find(window).scrollTop(10000)
    triggerEvent(window, 'scroll')

    andThen ->
      assert.ok(find(window).scrollTop() > 0, 'window should scroll')
      assert.ok(find('.items-list li').length > 30, 'should load more items after reaching threshold')

Has anyone successfully triggered scroll event in their tests?

2
Ember CLI runs the acceptance test app inside a shrunken div. When testing the scroll of this page, you have to be careful whether scrolling the surrounding test runner window or scrolling the app. Any luck if you scroll #ember-testing-container and/or .ember-application instead? - Bluu

2 Answers

1
votes

Finally I could make it work! Used #ember-testing-container instead window.

The code below was what worked for me:

andThen(() => {
  Ember.$('#ember-testing-container').scrollTop(10000);
});

triggerEvent(Ember.$('#ember-testing-container'), 'scroll');

andThen(() => {
  assert.ok(Ember.$('#ember-testing-container').scrollTop() > 0, 'window should scroll')
});

With ember-infinity you also need to scroll down the body before starting the test:

Ember.$('body').scrollTop(2000);
0
votes

I have an possible answer for this.

Try

triggerEvent('.skip-button', 'scroll', [{isInTestEnvironment:true}] ).then...

Say .skip-button is a selector within your ember app, but it could be any other one.

The scroll event is detected by the ember app, as if it actually doesn't scroll anything... this is why I pass a isInTestEnvironment:true param to indicate the ember app I simulated a user scroll.

Not an ideal solution, but far better than no test at all.