Skip to content

Commit ec92b78

Browse files
committed
Modify unit test for current source code
1 parent ce1ff3a commit ec92b78

File tree

1 file changed

+80
-45
lines changed

1 file changed

+80
-45
lines changed

test/unit/specs/InfiniteLoading.spec.js

Lines changed: 80 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,84 @@ function isShow(elm) {
99

1010
describe('InfiniteLoading.vue', () => {
1111
let vm;
12-
13-
// create new Vue instance for every test case
14-
beforeEach(() => {
15-
vm = new Vue({
16-
data: {
12+
const initConf = {
13+
data() {
14+
return {
1715
list: [],
1816
distance: 50,
1917
isLoadedAll: false,
2018
isDivScroll: true,
2119
isCustomSpinner: false,
22-
},
23-
template: `
24-
<div style="height: 100px;"
25-
:style="{
26-
overflow: isDivScroll ? 'auto' : 'visible'
27-
}">
28-
<ul>
29-
<li v-for="item in list" v-text="item"></li>
30-
</ul>
31-
<infinite-loading :distance="distance"
32-
:on-infinite="onInfinite"
33-
v-if="!isLoadedAll">
34-
<span slot="spinner" v-if="isCustomSpinner"><i class="custom-spinner"></i></span>
35-
</infinite-loading>
36-
</div>
37-
`,
38-
components: { InfiniteLoading },
39-
methods: {},
40-
});
20+
};
21+
},
22+
render(createElement) {
23+
return createElement(
24+
'div',
25+
{
26+
style: {
27+
height: '100px',
28+
overflow: this.isDivScroll ? 'auto' : 'visible',
29+
},
30+
},
31+
[
32+
createElement('ul', this.list.map((item) => createElement('li', item))),
33+
this.isLoadedAll ? undefined : createElement(InfiniteLoading,
34+
{
35+
props: {
36+
distance: this.distance,
37+
onInfinite: this.onInfinite,
38+
},
39+
ref: 'infiniteLoading',
40+
},
41+
[
42+
this.isCustomSpinner ? createElement('span',
43+
{
44+
slot: 'spinner',
45+
},
46+
[
47+
createElement('i', {
48+
attrs: {
49+
class: 'custom-spinner',
50+
},
51+
}),
52+
]
53+
) : undefined,
54+
]
55+
),
56+
]
57+
);
58+
},
59+
};
60+
61+
// create new Vue instance for every test case
62+
beforeEach(() => {
63+
const container = document.createElement('div');
64+
container.setAttribute('id', 'app');
65+
document.body.appendChild(container);
66+
67+
vm = new Vue(initConf);
4168
});
4269

4370
afterEach(() => {
44-
vm.$destroy();
71+
/**
72+
* because of call the $destroy method of parent cannot trigger
73+
* destroy event for child component in Vue.js 2.0.0-rc6
74+
*/
75+
vm.$refs.infiniteLoading && vm.$refs.infiniteLoading.$destroy();
76+
vm.$destroy(true);
77+
/**
78+
* because of pass true as the argument for destroy method cannot
79+
* remove element from DOM in Vue.js 2.0.0-rc6
80+
*/
81+
vm.$el && vm.$el.remove();
4582
});
4683

47-
it('should render a basic template', () => {
48-
vm.isDivScroll = false;
49-
vm.distance = undefined;
50-
51-
vm.$mount().$appendTo('body');
52-
53-
expect(vm.$el.querySelector('.loading-default')).to.be.ok;
84+
it('should render a basic template', (done) => {
85+
setTimeout(() => {
86+
vm.$mount('#app');
87+
expect(vm.$el.querySelector('.loading-default')).to.be.ok;
88+
done();
89+
}, 1);
5490
});
5591

5692
it('should execute callback and display a spinner immediately after initialize', (done) => {
@@ -61,7 +97,7 @@ describe('InfiniteLoading.vue', () => {
6197
});
6298
};
6399

64-
vm.$mount().$appendTo('body');
100+
vm.$mount('#app');
65101
});
66102

67103
it('should not to execute callback if the previous loading has not be completed', (done) => {
@@ -84,7 +120,7 @@ describe('InfiniteLoading.vue', () => {
84120
});
85121
}.bind(vm);
86122

87-
vm.$mount().$appendTo('body');
123+
vm.$mount('#app');
88124
});
89125

90126
it('should be destroyed completely by v-if', (done) => {
@@ -96,53 +132,52 @@ describe('InfiniteLoading.vue', () => {
96132
});
97133
}.bind(vm);
98134

99-
vm.$mount().$appendTo('body');
135+
vm.$mount('#app');
100136
});
101137

102138
it('should display no results prompt', (done) => {
103139
vm.onInfinite = function test() {
104-
this.$broadcast('$InfiniteLoading:complete');
140+
this.$refs.infiniteLoading.$emit('$InfiniteLoading:complete');
105141
Vue.nextTick(() => {
106142
expect(isShow(vm.$el.querySelectorAll('.infinite-status-prompt')[0])).to.be.true;
107143
done();
108144
});
109145
}.bind(vm);
110146

111-
vm.$mount().$appendTo('body');
147+
vm.$mount('#app');
112148
});
113149

114150
it('should display no more data prompt', (done) => {
115151
vm.onInfinite = function test() {
116-
this.$broadcast('$InfiniteLoading:loaded');
117-
this.$broadcast('$InfiniteLoading:complete');
152+
this.$refs.infiniteLoading.$emit('$InfiniteLoading:loaded');
153+
this.$refs.infiniteLoading.$emit('$InfiniteLoading:complete');
118154
Vue.nextTick(() => {
119155
expect(isShow(vm.$el.querySelectorAll('.infinite-status-prompt')[1])).to.be.true;
120156
done();
121157
});
122158
}.bind(vm);
123159

124-
vm.$mount().$appendTo('body');
160+
vm.$mount('#app');
125161
});
126162

127163
it('should reset component and call onInfinite again', (done) => {
128164
let callCount = 0;
129165
vm.onInfinite = function test() {
130166
if (!callCount++) {
131-
this.$broadcast('$InfiniteLoading:reset');
167+
this.$refs.infiniteLoading.$emit('$InfiniteLoading:reset');
132168
} else {
133169
done();
134170
}
135171
}.bind(vm);
136172

137-
vm.$mount().$appendTo('body');
173+
vm.$mount('#app');
138174
});
139175

140176
it('should display the custom spinner if customize it with slot', () => {
141177
vm.isCustomSpinner = true;
142178
vm.isDivScroll = false;
143-
vm.distance = undefined;
144-
145-
vm.$mount().$appendTo('body');
179+
delete vm.distance;
180+
vm.$mount('#app');
146181

147182
expect(vm.$el.querySelector('.custom-spinner')).to.be.ok;
148183
});

0 commit comments

Comments
 (0)