Skip to content

Commit 1a2fdc1

Browse files
committed
test: move test utils into a separate file
1 parent 360bba8 commit 1a2fdc1

File tree

2 files changed

+63
-38
lines changed

2 files changed

+63
-38
lines changed

test/unit/specs/InfiniteLoading.spec.js

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,9 @@
11
/* eslint-disable no-console */
22

33
import Vue from 'vue/dist/vue.common';
4+
import { isShow, continuesCall, fakeBox } from '../utils';
45
import InfiniteLoading from '../../../src/components/InfiniteLoading.vue';
56

6-
/**
7-
* check display status for a specific element
8-
* @param {DOM} elm
9-
* @return {Boolean}
10-
*/
11-
function isShow(elm) {
12-
return getComputedStyle(elm).display !== 'none';
13-
}
14-
15-
/**
16-
* continues call the specified number of times for a function
17-
* @param {Function} fn target function
18-
* @param {Number} times calls
19-
* @param {Function} cb [description]
20-
*/
21-
function continuesCall(fn, times, cb) {
22-
if (times) {
23-
fn();
24-
setTimeout(() => {
25-
continuesCall(fn, times - 1, cb);
26-
}, 1);
27-
} else {
28-
cb();
29-
}
30-
}
31-
327
describe('vue-infinite-loading:component', () => {
338
let vm; // save Vue model
349
const basicConfig = {
@@ -315,20 +290,19 @@ describe('vue-infinite-loading:component', () => {
315290
});
316291

317292
it('should still works properly with the deprecated property `:on-infinite` but throw warning', (done) => {
318-
const originalError = console.warn;
319293
let isThrowWarn;
320294

321-
console.warn = (text) => {
295+
console.warn = fakeBox(console.warn, (text) => {
322296
if (text.indexOf('@infinite') > -1) {
323297
isThrowWarn = true;
324298
}
325-
};
299+
});
326300

327301
vm = new Vue(Object.assign({}, basicConfig, {
328302
methods: {
329303
onInfinite: function onInfinite() {
330304
expect(isThrowWarn).to.be.true;
331-
console.warn = originalError;
305+
console.warn = fakeBox();
332306
done();
333307
},
334308
},
@@ -431,14 +405,13 @@ describe('vue-infinite-loading:component', () => {
431405
});
432406

433407
it('should still works properly with the $refs.component.$emit but throw warning', (done) => {
434-
const originalError = console.warn;
435408
let throwWarnTimes = 0;
436409

437-
console.warn = (text) => {
410+
console.warn = fakeBox(console.warn, (text) => {
438411
if (text.indexOf('$state') > -1) {
439412
throwWarnTimes += 1;
440413
}
441-
};
414+
});
442415

443416
vm = new Vue(Object.assign({}, basicConfig, {
444417
methods: {
@@ -448,7 +421,7 @@ describe('vue-infinite-loading:component', () => {
448421
} else {
449422
this.$refs.infiniteLoading.$emit('$InfiniteLoading:complete');
450423
expect(throwWarnTimes).to.equal(2);
451-
console.warn = originalError;
424+
console.warn = fakeBox();
452425
done();
453426
}
454427
},
@@ -490,14 +463,13 @@ describe('vue-infinite-loading:component', () => {
490463
});
491464

492465
it('should throw error when the component be in a infinite loop caused by a wrapper with unfixed height', (done) => {
493-
const originalError = console.error;
494466
let isThrowError;
495467

496-
console.error = (text) => {
468+
console.error = fakeBox(console.error, (text) => {
497469
if (text.indexOf('issues/55') > -1) {
498470
isThrowError = true;
499471
}
500-
};
472+
});
501473

502474
vm = new Vue(Object.assign({}, basicConfig, {
503475
template: `
@@ -521,7 +493,7 @@ describe('vue-infinite-loading:component', () => {
521493
} else {
522494
$state.complete();
523495
expect(isThrowError).to.be.true;
524-
console.error = originalError;
496+
console.error = fakeBox();
525497
// wait for the loop check flag be marked as true
526498
setTimeout(() => {
527499
expect(this.$refs.infiniteLoading.infiniteLoopChecked).to.be.true;

test/unit/utils.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* check display status for a specific element
3+
* @param {DOM} elm
4+
* @return {Boolean}
5+
*/
6+
export function isShow(elm) {
7+
return window.getComputedStyle(elm).display !== 'none';
8+
}
9+
10+
/**
11+
* continues call the specified number of times for a function
12+
* @param {Function} fn target function
13+
* @param {Number} times calls
14+
* @param {Function} cb [description]
15+
*/
16+
export function continuesCall(fn, times, cb) {
17+
if (times) {
18+
fn();
19+
setTimeout(() => {
20+
continuesCall(fn, times - 1, cb);
21+
}, 1);
22+
} else {
23+
cb();
24+
}
25+
}
26+
27+
let fakeCache;
28+
/**
29+
* fake function or restore function
30+
* @param {Function} fn target function
31+
* @param {Function} cb fake function
32+
*/
33+
export function fakeBox(fn, cb) {
34+
let result;
35+
36+
if (fakeCache) {
37+
result = fakeCache;
38+
fakeCache = null;
39+
} else {
40+
fakeCache = fn;
41+
result = (...args) => {
42+
cb(...args);
43+
};
44+
}
45+
46+
return result;
47+
}
48+
49+
export default {
50+
isShow,
51+
continuesCall,
52+
fakeBox,
53+
};

0 commit comments

Comments
 (0)