Skip to content

Commit 2e70fa3

Browse files
committed
Add test
1 parent a4ae90e commit 2e70fa3

File tree

4 files changed

+151
-4
lines changed

4 files changed

+151
-4
lines changed

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ module.exports = {
2020
'.*\\.(vue)$': 'vue-jest',
2121
'^.+\\.js$': '<rootDir>/node_modules/babel-jest'
2222
},
23-
testRegex: 'base.test.js?$'
23+
// testRegex: 'slot.test.js?$'
2424
}

test/base.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { mount } from '@vue/test-utils'
2-
32
import VirtualList from '../src/index'
43
import { VirtualProps } from '../src/props'
5-
64
import Item from './item.vue'
75
import { getDatas } from './util'
86

@@ -42,7 +40,7 @@ describe('base', () => {
4240
const rootEl = vslVm.$el
4341
expect(rootEl.tagName.toLowerCase()).toBe(VirtualProps.rootTag.default)
4442

45-
const wrapperEl = rootEl.querySelector('div')
43+
const wrapperEl = rootEl.firstElementChild
4644

4745
// wrapper element and padding style
4846
expect(wrapperEl.getAttribute('role')).toBe('group')

test/element.test.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { mount } from '@vue/test-utils'
2+
import VirtualList from '../src/index'
3+
import { VirtualProps } from '../src/props'
4+
import Item from './item.vue'
5+
import { getDatas } from './util'
6+
7+
describe('element', () => {
8+
const Instance = mount({
9+
name: 'test',
10+
components: {
11+
'virtual-list': VirtualList
12+
},
13+
template: `
14+
<div id="app">
15+
<virtual-list class="my-list" style="height: 300px; overflow-y: auto;"
16+
:data-key="'id'"
17+
:data-sources="items"
18+
:data-component="item"
19+
:root-tag="'article'"
20+
:wrap-tag="'section'"
21+
:wrap-class="'wrap-class-aaa'"
22+
:item-tag="'p'"
23+
:item-class="'item-class-bbb'"
24+
:item-class-add="addItemClass"
25+
/>
26+
</div>
27+
`,
28+
data () {
29+
return {
30+
items: getDatas(1000),
31+
item: Item
32+
}
33+
},
34+
methods: {
35+
addItemClass (index) {
36+
return 'extra-item-' + index
37+
}
38+
}
39+
})
40+
41+
it('check mount', () => {
42+
expect(Instance.name()).toBe('test')
43+
expect(Instance.is('div')).toBe(true)
44+
expect(Instance.isVueInstance()).toBe(true)
45+
expect(Instance.find('.my-list').exists()).toBe(true)
46+
})
47+
48+
it('check element tag and class', () => {
49+
const InstanceData = Instance.vm.$data
50+
const vslVm = Instance.find('.my-list').vm
51+
const rootEl = vslVm.$el
52+
expect(rootEl.tagName.toLowerCase()).toBe('article')
53+
54+
const wrapperEl = rootEl.firstElementChild
55+
56+
// wrapper element and padding style
57+
expect(wrapperEl.getAttribute('role')).toBe('group')
58+
expect(!!rootEl.style.padding).toBe(false)
59+
expect(!!wrapperEl.style.padding).toBe(true)
60+
expect(wrapperEl.className).toBe('wrap-class-aaa')
61+
expect(wrapperEl.tagName.toLowerCase()).toBe('section')
62+
63+
// render number keeps by default
64+
expect(wrapperEl.childNodes.length).toBe(VirtualProps.keeps.default)
65+
66+
// items render content
67+
for (let i = 0; i < wrapperEl.childNodes.length; i++) {
68+
const itemEl = wrapperEl.childNodes[i]
69+
expect(itemEl.className).toBe(`item-class-bbb extra-item-${i}`)
70+
expect(itemEl.tagName.toLowerCase()).toBe('p')
71+
72+
// item inner render (see ./item.vue)
73+
const itemInnerEl = itemEl.firstElementChild
74+
expect(itemInnerEl.className).toBe('inner')
75+
expect(itemInnerEl.querySelector('.index').textContent).toBe(`${i}`)
76+
expect(itemInnerEl.querySelector('.source').textContent).toBe(InstanceData.items[i].text)
77+
}
78+
})
79+
})

test/slot.test.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { mount } from '@vue/test-utils'
2+
import VirtualList from '../src/index'
3+
import Item from './item.vue'
4+
import { getDatas } from './util'
5+
6+
describe('slot', () => {
7+
const Instance = mount({
8+
name: 'test',
9+
components: {
10+
'virtual-list': VirtualList
11+
},
12+
template: `
13+
<div id="app">
14+
<virtual-list class="my-list" style="height: 300px; overflow-y: auto;"
15+
:data-key="'id'"
16+
:data-sources="items"
17+
:data-component="item"
18+
:header-tag="'section'"
19+
:header-class="'head1'"
20+
:footer-tag="'article'"
21+
:footer-class="'foot1'"
22+
>
23+
<div slot="header">Header</div>
24+
<div slot="footer">Footer</div>
25+
</virtual-list>
26+
</div>
27+
`,
28+
data () {
29+
return {
30+
items: getDatas(1000),
31+
item: Item
32+
}
33+
},
34+
methods: {
35+
addItemClass (index) {
36+
return 'extra-item-' + index
37+
}
38+
}
39+
})
40+
41+
it('check mount', () => {
42+
expect(Instance.name()).toBe('test')
43+
expect(Instance.is('div')).toBe(true)
44+
expect(Instance.isVueInstance()).toBe(true)
45+
expect(Instance.find('.my-list').exists()).toBe(true)
46+
})
47+
48+
it('check slot build', () => {
49+
const vslVm = Instance.find('.my-list').vm
50+
const rootEl = vslVm.$el
51+
const wrapperEl = rootEl.querySelector('[role="group"]')
52+
const headerEl = rootEl.querySelector('[role="header"]')
53+
const footerEl = rootEl.querySelector('[role="footer"]')
54+
55+
// wrapper shoud be in middle between header and footer
56+
expect(wrapperEl.previousElementSibling).toBe(headerEl)
57+
expect(wrapperEl.nextElementSibling).toBe(footerEl)
58+
59+
expect(!!headerEl).toBe(true)
60+
expect(!!footerEl).toBe(true)
61+
62+
expect(headerEl.className).toBe('head1')
63+
expect(headerEl.tagName.toLowerCase()).toBe('section')
64+
expect(headerEl.firstElementChild.textContent).toBe('Header')
65+
66+
expect(footerEl.className).toBe('foot1')
67+
expect(footerEl.tagName.toLowerCase()).toBe('article')
68+
expect(footerEl.firstElementChild.textContent).toBe('Footer')
69+
})
70+
})

0 commit comments

Comments
 (0)