|
| 1 | +import VirtualList from '../src/index' |
| 2 | +import { mount } from '@vue/test-utils' |
| 3 | +import { getVariableList } from './util' |
| 4 | + |
| 5 | +// for testing variable height. |
| 6 | +const theme = 'variable-function-test' |
| 7 | + |
| 8 | +describe(theme, () => { |
| 9 | + const initSize = 40 |
| 10 | + const initRemian = 6 |
| 11 | + const listCount = 1000 |
| 12 | + |
| 13 | + const itemComponent = { |
| 14 | + template: ` |
| 15 | + <div class="for-item" :style="{height: item.height + 'px'}"> |
| 16 | + <span class="for-item-text">{{ item.text }}</span> |
| 17 | + </div> |
| 18 | + `, |
| 19 | + |
| 20 | + name: 'item', |
| 21 | + |
| 22 | + props: { |
| 23 | + item: Object |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + const wrapper = mount({ |
| 28 | + template: ` |
| 29 | + <div id="app" style="width: 300px;"> |
| 30 | + <virtual-list class="list" |
| 31 | + :size="size" |
| 32 | + ref="vlist" |
| 33 | + :item="item" |
| 34 | + :itemcount="items.length" |
| 35 | + :itemprops="getItemProps" |
| 36 | + :remain="remian" |
| 37 | + :start="start" |
| 38 | + /> |
| 39 | + <button v-on:click="addItem">Add Item</button> |
| 40 | + </div> |
| 41 | + `, |
| 42 | + |
| 43 | + name: 'test', |
| 44 | + |
| 45 | + components: { |
| 46 | + 'virtual-list': VirtualList, |
| 47 | + 'item-component': itemComponent |
| 48 | + }, |
| 49 | + |
| 50 | + data () { |
| 51 | + return { |
| 52 | + start: 5, |
| 53 | + size: initSize, |
| 54 | + item: itemComponent, |
| 55 | + remian: initRemian, |
| 56 | + items: getVariableList(listCount) |
| 57 | + } |
| 58 | + }, |
| 59 | + |
| 60 | + methods: { |
| 61 | + getItemProps (index) { |
| 62 | + return { |
| 63 | + key: this.items[index].text, |
| 64 | + props: { |
| 65 | + item: this.items[index] |
| 66 | + } |
| 67 | + } |
| 68 | + }, |
| 69 | + addItem () { |
| 70 | + this.items.push(getVariableList(1)[0]) |
| 71 | + this.$refs.vlist.forceRender() |
| 72 | + } |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | + it(`[${theme}] check build success.`, () => { |
| 77 | + const itemFrags = wrapper.findAll('.for-item') |
| 78 | + |
| 79 | + expect(itemFrags.length).toBe(initRemian + initRemian) |
| 80 | + |
| 81 | + for (let i = 0; i < itemFrags.length; i++) { |
| 82 | + const item = itemFrags.at(i) |
| 83 | + expect(item.text()).toBe('#' + i) |
| 84 | + expect(item.vm.$el.style.height).toBe(wrapper.vm.$data.items[i].height + 'px') |
| 85 | + } |
| 86 | + }) |
| 87 | + |
| 88 | + it(`[${theme}] adds an item.`, () => { |
| 89 | + const button = wrapper.find('button') |
| 90 | + button.trigger('click') |
| 91 | + expect(wrapper.vm.items.length === listCount + 1) |
| 92 | + }) |
| 93 | +}) |
0 commit comments