Skip to content

Commit f34f5c6

Browse files
committed
Add unit test for the debounce logics
1 parent 8dc320e commit f34f5c6

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ delete webpackConfig.entry;
66
// Karma configuration
77
module.exports = function(config) {
88
config.set({
9-
frameworks: ['mocha', 'chai'],
9+
frameworks: ['mocha', 'sinon-chai'],
1010
files: [
1111
'./test/unit/index.js'
1212
],

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"babel-plugin-transform-runtime": "^6.23.0",
4343
"babel-preset-env": "^1.6.0",
4444
"babel-preset-stage-2": "^6.24.1",
45-
"chai": "^4.1.1",
45+
"chai": "^3.5.0",
4646
"css-loader": "^0.28.4",
4747
"eslint": "^4.4.1",
4848
"eslint-config-airbnb-base": "^11.3.1",
@@ -57,13 +57,16 @@
5757
"karma-coverage": "^1.1.1",
5858
"karma-mocha": "^1.3.0",
5959
"karma-phantomjs-launcher": "^1.0.4",
60+
"karma-sinon-chai": "^1.3.1",
6061
"karma-spec-reporter": "0.0.31",
6162
"karma-webpack": "^2.0.4",
6263
"less": "^2.7.2",
6364
"less-loader": "^4.0.5",
6465
"mocha": "^3.5.0",
6566
"phantomjs-prebuilt": "^2.1.15",
6667
"pre-commit": "^1.2.2",
68+
"sinon": "^2.4.1",
69+
"sinon-chai": "^2.13.0",
6770
"style-loader": "^0.18.2",
6871
"uglifyjs-webpack-plugin": "^0.4.6",
6972
"vue": "^2.2.0",

test/unit/.eslintrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
mocha: true
44
globals:
55
expect: true
6+
sinon: true
67
rules:
78
no-unused-expressions: 0

test/unit/specs/InfiniteLoading.spec.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ function isShow(elm) {
1414
return getComputedStyle(elm).display !== 'none';
1515
}
1616

17+
/**
18+
* continues call the specified number of times for a function
19+
* @param {Function} fn target function
20+
* @param {Number} times calls
21+
* @param {Function} cb [description]
22+
*/
23+
function continuesCall(fn, times, cb) {
24+
if (times) {
25+
fn();
26+
setTimeout(() => {
27+
continuesCall(fn, times - 1, cb);
28+
}, 1);
29+
} else {
30+
cb();
31+
}
32+
}
33+
1734
describe('vue-infinite-loading', () => {
1835
let vm; // save Vue model
1936
const basicConfig = {
@@ -193,7 +210,7 @@ describe('vue-infinite-loading', () => {
193210
vm.$mount('#app');
194211
});
195212

196-
it('should always load data until fill up the contianer\n (use div as the container)', (done) => {
213+
it('should always load data until fill up the container\n (use div as the container)', (done) => {
197214
let timer;
198215

199216
vm = new Vue(Object.assign({}, basicConfig, {
@@ -244,7 +261,7 @@ describe('vue-infinite-loading', () => {
244261
vm.$mount(wrapper);
245262
});
246263

247-
it('should not works when deactivated by the `keep-alive` feature\n (use top direction)', (done) => {
264+
it('should not works when deactivated by the `keep-alive` feature\n (use top direction and use div as the container)', (done) => {
248265
let calledTimes = 0;
249266
const InfiniteView = Object.assign({}, basicConfig, {
250267
data() {
@@ -361,4 +378,31 @@ describe('vue-infinite-loading', () => {
361378

362379
vm.$mount('#app');
363380
});
381+
382+
it('should debounce properly for the scroll event handler\n (use div as the container)', (done) => {
383+
vm = new Vue(Object.assign({}, basicConfig, {
384+
data: {
385+
list: [...new Array(20).join('1').split('')],
386+
isDivScroll: true,
387+
direction: 'bottom',
388+
},
389+
mounted: function mounted() {
390+
const scrollParent = this.$refs.infiniteLoading.scrollParent;
391+
const spyFn = sinon.spy(this.$refs.infiniteLoading, 'attemptLoad');
392+
const alreadyCalledTimes = 1; // it will be called immediately after mount
393+
394+
continuesCall(() => {
395+
scrollParent.scrollTop += 10;
396+
}, 10, () => {
397+
expect(spyFn).to.have.been.callCount(0 + alreadyCalledTimes);
398+
setTimeout(() => {
399+
expect(spyFn).to.have.been.callCount(1 + alreadyCalledTimes);
400+
done();
401+
}, this.$refs.infiniteLoading.debounceDuration + 10);
402+
});
403+
},
404+
}));
405+
406+
vm.$mount('#app');
407+
});
364408
});

0 commit comments

Comments
 (0)