PeachScript / vue-infinite-loading

An infinite scroll plugin for Vue.js.
https://peachscript.github.io/vue-infinite-loading/
MIT License
2.67k stars 369 forks source link

How to test infiniteHandler in jest? #232

Closed smoothdvd closed 5 years ago

smoothdvd commented 5 years ago

As title

PeachScript commented 5 years ago

Emm, in this project, I tested it through trigger the scroll event manually, you can check out it in this test case, and do you have any idea about this topic?

smoothdvd commented 5 years ago

@PeachScript I tested following jest test code and passed:

// Mock request load more data(comments)
axios.get.mockResolvedValue({ status: 200, data: mockCommentsMore });
...
wrapper.find(InfiniteLoading).vm.$emit('infinite');
setTimeout(() => {
  expect(wrapper.vm.comments.length).toBe(2);
  expect(wrapper.vm.comments[1].commentID).toBe(2179712);
  done();
}, 10);

Did I do the right thing?

PeachScript commented 5 years ago

@smoothdvd I totally agree with you, because listen scroll event is this plugin's job, so this plugin need to make sure it can convert correctly from scroll event to infinite event, for any project that use this plugin, it just need to care about infinite event rather than scroll event :P