infiniteloading.spec.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { config, mount } from '@vue/test-utils';
  2. import InfiniteLoading from '../index.vue';
  3. import NutIcon from '../../icon/index.vue';
  4. import { trigger, triggerDrag } from '../../../utils/test/event';
  5. import { nextTick, toRefs, reactive, ref, onMounted } from 'vue';
  6. function sleep(delay = 0): Promise<void> {
  7. return new Promise((resolve) => {
  8. setTimeout(resolve, delay);
  9. });
  10. }
  11. beforeAll(() => {
  12. config.global.components = {
  13. NutIcon
  14. };
  15. });
  16. afterAll(() => {
  17. config.global.components = {};
  18. });
  19. test('pull base', async () => {
  20. const wrapper = mount(InfiniteLoading, {
  21. props: {
  22. isOpenRefresh: true,
  23. loadMoreTxt: '没有更多'
  24. }
  25. });
  26. const track = wrapper.find('.nut-infiniteloading');
  27. // pulling
  28. trigger(track, 'touchstart', 0, 0);
  29. trigger(track, 'touchmove', 0, 20);
  30. expect(wrapper.html()).toMatchSnapshot();
  31. // loading
  32. trigger(track, 'touchend', 0, 100);
  33. expect(wrapper.html()).toMatchSnapshot();
  34. // still loading
  35. triggerDrag(track, 0, 100);
  36. expect(wrapper.emitted('refresh')).toBeTruthy();
  37. });
  38. test('infiniteloading base', async () => {
  39. const wrapper = mount({
  40. components: {
  41. 'nut-infiniteloading': InfiniteLoading,
  42. 'nut-icon': NutIcon
  43. },
  44. template: `
  45. <nut-infiniteloading
  46. :has-more="hasMore"
  47. load-more-txt="没有啦~"
  48. @load-more="loadMore">
  49. <li class="infiniteLi" v-for="(item, index) in defultList" :key="index">{{item}}</li>
  50. </nut-infiniteloading>
  51. `,
  52. setup() {
  53. const hasMore = ref(true);
  54. const data = reactive({
  55. defultList: []
  56. });
  57. const init = () => {
  58. for (let i = 0; i < 10; i++) {
  59. (data.defultList as any).push(i);
  60. }
  61. };
  62. const loadMore = (done: any) => {
  63. setTimeout(() => {
  64. const curLen = data.defultList.length;
  65. for (let i = curLen; i < curLen + 10; i++) {
  66. (data.defultList as any).push(i);
  67. }
  68. if (data.defultList.length > 30) hasMore.value = false;
  69. done();
  70. }, 500);
  71. };
  72. onMounted(() => {
  73. init();
  74. });
  75. return { ...toRefs(data), hasMore, loadMore };
  76. }
  77. });
  78. const track = wrapper.find('.nut-infiniteloading');
  79. await nextTick();
  80. trigger(track, 'touchstart', 0, 0);
  81. trigger(track, 'touchmove', 0, -100);
  82. trigger(track, 'touchend', 0, -800);
  83. triggerDrag(track, 0, -800);
  84. await nextTick();
  85. expect(wrapper.html()).toMatchSnapshot();
  86. });