Skip to content

Commit a6aa71d

Browse files
committed
Add unit test for the feature that support transition and stagger
1 parent 8db8fae commit a6aa71d

File tree

1 file changed

+146
-6
lines changed

1 file changed

+146
-6
lines changed

test/unit/specs/InfiniteLoading.spec.js

Lines changed: 146 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ function isShow(elm) {
1010
describe('InfiniteLoading.vue', () => {
1111
let vm;
1212

13+
Vue.transition('bounce', { type: 'transition', afterEnter() {} });
14+
1315
// create new Vue instance for every test case
1416
beforeEach(() => {
1517
vm = new Vue({
@@ -57,11 +59,11 @@ describe('InfiniteLoading.vue', () => {
5759

5860
it('should execute callback and display a spinner immediately after initialize', (done) => {
5961
vm.onInfinite = function test() {
60-
Vue.nextTick(() => {
62+
this.$nextTick(() => {
6163
expect(isShow(vm.$el.querySelector('.loading-default'))).to.be.true;
6264
done();
6365
});
64-
};
66+
}.bind(vm);
6567

6668
vm.$mount().$appendTo('body');
6769
});
@@ -73,7 +75,7 @@ describe('InfiniteLoading.vue', () => {
7375
this.list.push(i);
7476
}
7577

76-
Vue.nextTick(() => {
78+
this.$nextTick(() => {
7779
if (this.list.length === 20) {
7880
vm.$el.addEventListener('scroll', () => {
7981
expect(this.list).to.have.lengthOf(20);
@@ -92,7 +94,7 @@ describe('InfiniteLoading.vue', () => {
9294
it('should be destroyed completely by v-if', (done) => {
9395
vm.onInfinite = function test() {
9496
this.isLoadedAll = true;
95-
Vue.nextTick(() => {
97+
this.$nextTick(() => {
9698
expect(vm.$el.querySelector('.loading-default')).to.not.be.ok;
9799
done();
98100
});
@@ -104,7 +106,7 @@ describe('InfiniteLoading.vue', () => {
104106
it('should display no results prompt', (done) => {
105107
vm.onInfinite = function test() {
106108
this.$broadcast('$InfiniteLoading:complete');
107-
Vue.nextTick(() => {
109+
this.$nextTick(() => {
108110
expect(isShow(vm.$el.querySelectorAll('.infinite-status-prompt')[0])).to.be.true;
109111
done();
110112
});
@@ -117,7 +119,7 @@ describe('InfiniteLoading.vue', () => {
117119
vm.onInfinite = function test() {
118120
this.$broadcast('$InfiniteLoading:loaded');
119121
this.$broadcast('$InfiniteLoading:complete');
120-
Vue.nextTick(() => {
122+
this.$nextTick(() => {
121123
expect(isShow(vm.$el.querySelectorAll('.infinite-status-prompt')[1])).to.be.true;
122124
done();
123125
});
@@ -147,4 +149,142 @@ describe('InfiniteLoading.vue', () => {
147149

148150
expect(vm.$el.querySelector('.custom-spinner')).to.be.ok;
149151
});
152+
153+
it('should working properly with transition and stagger', (done) => {
154+
const transitonVm = new Vue({
155+
data: {
156+
list: [],
157+
distance: 50,
158+
isLoadedAll: false,
159+
isDivScroll: true,
160+
isCustomSpinner: false,
161+
bounce: Vue.transition('bounce'),
162+
stagger: 10,
163+
pageItems: 20,
164+
},
165+
template: `
166+
<div style="height: 100px; overflow: auto;">
167+
<style>
168+
.item.bounce-transition{
169+
transition: all .3s;
170+
opacity: 1;
171+
}
172+
.item.bounce-enter, .item.bounce-leave{
173+
opacity: 0;
174+
}
175+
</style>
176+
<ul>
177+
<li class="item" v-for="item in list" v-text="item" transition="bounce"
178+
:stagger="stagger"></li>
179+
</ul>
180+
<infinite-loading :distance="distance"
181+
:on-infinite="onInfinite"
182+
v-if="!isLoadedAll"
183+
:transition="bounce"
184+
:stagger-count="list.length">
185+
</infinite-loading>
186+
</div>
187+
`,
188+
components: { InfiniteLoading },
189+
methods: {
190+
onInfinite() {
191+
const len = this.list.length + 1;
192+
for (let i = len; i < len + this.pageItems; i++) {
193+
this.list.push(i);
194+
}
195+
196+
this.$nextTick(() => {
197+
this.$broadcast('$InfiniteLoading:loaded');
198+
199+
if (this.list.length <= this.pageItems) {
200+
// wait for transition complete
201+
setTimeout(() => {
202+
this.$el.onscroll = () => {
203+
this.$nextTick(() => {
204+
// load more data when scroll after transition
205+
expect(this.list.length).to.equal(this.pageItems * 2);
206+
done();
207+
});
208+
};
209+
210+
// trigger scroll event manually
211+
this.$el.scrollTop = this.$el.scrollHeight;
212+
}, (this.stagger * this.pageItems) + 310);
213+
}
214+
});
215+
},
216+
},
217+
});
218+
219+
transitonVm.$mount().$appendTo('body');
220+
});
221+
222+
it('should reset properly with transition and stagger', (done) => {
223+
const transitonVm = new Vue({
224+
data: {
225+
list: [],
226+
distance: 50,
227+
isLoadedAll: false,
228+
isDivScroll: true,
229+
isCustomSpinner: false,
230+
bounce: Vue.transition('bounce'),
231+
stagger: 10,
232+
pageItems: 20,
233+
},
234+
template: `
235+
<div style="height: 100px; overflow: auto;">
236+
<style>
237+
.item.bounce-transition{
238+
transition: all .3s;
239+
opacity: 1;
240+
}
241+
.item.bounce-enter, .item.bounce-leave{
242+
opacity: 0;
243+
}
244+
</style>
245+
<ul>
246+
<li class="item" v-for="item in list" v-text="item" transition="bounce"
247+
:stagger="stagger"></li>
248+
</ul>
249+
<infinite-loading :distance="distance"
250+
:on-infinite="onInfinite"
251+
v-if="!isLoadedAll"
252+
:transition="bounce"
253+
:stagger-count="list.length">
254+
</infinite-loading>
255+
</div>
256+
`,
257+
components: { InfiniteLoading },
258+
ready() {
259+
// when the transition doesn't has hook function
260+
delete this.bounce.afterEnter;
261+
},
262+
methods: {
263+
onInfinite() {
264+
const len = this.list.length + 1;
265+
for (let i = len; i < len + this.pageItems; i++) {
266+
this.list.push(i);
267+
}
268+
this.$broadcast('$InfiniteLoading:loaded');
269+
270+
// wait for transition complete
271+
setTimeout(() => {
272+
this.list = [];
273+
274+
// item will be removed one by one, so the count of item not be 0 immediately
275+
expect(this.$el.querySelectorAll('ul li').length !== 0).to.be.true;
276+
277+
// wait for transition complete
278+
setTimeout(() => {
279+
expect(this.$el.querySelectorAll('ul li')).to.have.lengthOf(0);
280+
this.$broadcast('$InfiniteLoading:reset');
281+
done();
282+
}, (this.pageItems * this.stagger) + 300);
283+
}, (this.pageItems * this.stagger) + 300);
284+
},
285+
},
286+
});
287+
288+
transitonVm.$mount().$appendTo('body');
289+
});
150290
});

0 commit comments

Comments
 (0)