Skip to content

Commit a4ae90e

Browse files
committed
Add base test
1 parent fdf7ebe commit a4ae90e

File tree

3 files changed

+89
-12
lines changed

3 files changed

+89
-12
lines changed

test/base.test.js

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,69 @@
11
import { mount } from '@vue/test-utils'
2+
23
import VirtualList from '../src/index'
4+
import { VirtualProps } from '../src/props'
5+
6+
import Item from './item.vue'
7+
import { getDatas } from './util'
38

49
describe('base', () => {
5-
const wrapper = mount({
10+
const Instance = mount({
611
name: 'test',
12+
components: {
13+
'virtual-list': VirtualList
14+
},
715
template: `
816
<div id="app">
9-
<virtual-list class="list" style="height: 300px; overflow-y: auto;"
10-
:size="50"
11-
:keeps="20"
17+
<virtual-list class="my-list" style="height: 300px; overflow-y: auto;"
18+
:data-key="'id'"
19+
:data-sources="items"
20+
:data-component="item"
1221
/>
1322
</div>
1423
`,
15-
components: {
16-
'virtual-list': VirtualList
17-
},
1824
data () {
1925
return {
20-
items: []
26+
items: getDatas(1000),
27+
item: Item
2128
}
2229
}
2330
})
2431

25-
it('check mount success', () => {
26-
expect(wrapper.name()).toBe('test')
27-
expect(wrapper.is('div')).toBe(true)
28-
expect(wrapper.isVueInstance()).toBe(true)
32+
it('check mount', () => {
33+
expect(Instance.name()).toBe('test')
34+
expect(Instance.is('div')).toBe(true)
35+
expect(Instance.isVueInstance()).toBe(true)
36+
expect(Instance.find('.my-list').exists()).toBe(true)
37+
})
38+
39+
it('check list build by default', () => {
40+
const InstanceData = Instance.vm.$data
41+
const vslVm = Instance.find('.my-list').vm
42+
const rootEl = vslVm.$el
43+
expect(rootEl.tagName.toLowerCase()).toBe(VirtualProps.rootTag.default)
44+
45+
const wrapperEl = rootEl.querySelector('div')
46+
47+
// wrapper element and padding style
48+
expect(wrapperEl.getAttribute('role')).toBe('group')
49+
expect(!!rootEl.style.padding).toBe(false)
50+
expect(!!wrapperEl.style.padding).toBe(true)
51+
expect(wrapperEl.tagName.toLowerCase()).toBe(VirtualProps.wrapTag.default)
52+
53+
// render number keeps by default
54+
expect(wrapperEl.childNodes.length).toBe(VirtualProps.keeps.default)
55+
56+
// items render content
57+
for (let i = 0; i < wrapperEl.childNodes.length; i++) {
58+
const itemEl = wrapperEl.childNodes[i]
59+
expect(itemEl.className).toBe('')
60+
expect(itemEl.tagName.toLowerCase()).toBe(VirtualProps.itemTag.default)
61+
62+
// item inner render (see ./item.vue)
63+
const itemInnerEl = itemEl.firstElementChild
64+
expect(itemInnerEl.className).toBe('inner')
65+
expect(itemInnerEl.querySelector('.index').textContent).toBe(`${i}`)
66+
expect(itemInnerEl.querySelector('.source').textContent).toBe(InstanceData.items[i].text)
67+
}
2968
})
3069
})

test/item.vue

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<template>
2+
<div class="inner">
3+
<span class="index">{{ index }}</span>
4+
<span class="source">{{ source.text }}</span>
5+
<span class="other">{{ otherProp }}</span>
6+
</div>
7+
</template>
8+
9+
<script>
10+
export default {
11+
name: 'item',
12+
13+
props: {
14+
index: {
15+
type: Number
16+
},
17+
source: {
18+
type: Object,
19+
default () {
20+
return {}
21+
}
22+
},
23+
otherProp: {
24+
type: String
25+
}
26+
}
27+
}
28+
</script>

test/util.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function getDatas (counts) {
2+
const data = []
3+
for (let index = 0; index < counts; index++) {
4+
data.push({
5+
id: String(index),
6+
text: Math.random().toString(16).substr(8)
7+
})
8+
}
9+
return data
10+
}

0 commit comments

Comments
 (0)