|
| 1 | +import { mount } from '@vue/test-utils' |
| 2 | +import ItemListSelector from '@/index.vue' |
| 3 | +import getTestData from './getTestData.js' |
| 4 | + |
| 5 | +describe('Events', () => { |
| 6 | + const wrapper = mount(ItemListSelector, { |
| 7 | + propsData: { |
| 8 | + data: getTestData(), |
| 9 | + selection: [] |
| 10 | + } |
| 11 | + }) |
| 12 | + const $input = wrapper.find('.item-selector__searchbar input') |
| 13 | + |
| 14 | + it('鼠标点击选项', () => { |
| 15 | + wrapper |
| 16 | + .findAll('.item-selector__option') |
| 17 | + .at(0) |
| 18 | + .element.click() |
| 19 | + wrapper.setProps({ selection: wrapper.emitted()['selection-change'][0][0] }) |
| 20 | + wrapper |
| 21 | + .findAll('.item-selector__option') |
| 22 | + .at(1) |
| 23 | + .element.click() |
| 24 | + wrapper.setProps({ selection: wrapper.emitted()['selection-change'][1][0] }) |
| 25 | + wrapper |
| 26 | + .findAll('.item-selector__option') |
| 27 | + .at(1) |
| 28 | + .element.click() |
| 29 | + wrapper.setProps({ selection: wrapper.emitted()['selection-change'][2][0] }) |
| 30 | + expect(wrapper.vm.selection.length).toEqual(1) |
| 31 | + expect(wrapper.vm.selection[0]).toEqual(wrapper.vm.data[0]) |
| 32 | + expect(wrapper.emittedByOrder().map(r => r.name)).toEqual([ |
| 33 | + 'selection-change', |
| 34 | + 'selection-change', |
| 35 | + 'selection-change' |
| 36 | + ]) |
| 37 | + }) |
| 38 | + |
| 39 | + it('下箭头移动高亮选项', () => { |
| 40 | + wrapper.setData({ |
| 41 | + optionActiveIndex: 2 |
| 42 | + }) |
| 43 | + expect(wrapper.findAll('.item-selector__option--active').length).toEqual(1) |
| 44 | + $input.trigger('keyup.down') |
| 45 | + expect(wrapper.vm.optionActiveIndex).toEqual(3) |
| 46 | + $input.trigger('keyup.down') |
| 47 | + expect(wrapper.vm.optionActiveIndex).toEqual(4) |
| 48 | + }) |
| 49 | + |
| 50 | + it('在底部选项边缘,下箭头移动高亮选项到第一项', () => { |
| 51 | + wrapper.setData({ |
| 52 | + optionActiveIndex: wrapper.vm.filtedData.length - 1 |
| 53 | + }) |
| 54 | + $input.trigger('keyup.down') |
| 55 | + expect(wrapper.vm.optionActiveIndex).toEqual(0) |
| 56 | + }) |
| 57 | + |
| 58 | + it('上箭头移动高亮选项', () => { |
| 59 | + wrapper.setData({ |
| 60 | + optionActiveIndex: 2 |
| 61 | + }) |
| 62 | + $input.trigger('keyup.up') |
| 63 | + expect(wrapper.vm.optionActiveIndex).toEqual(1) |
| 64 | + $input.trigger('keyup.up') |
| 65 | + expect(wrapper.vm.optionActiveIndex).toEqual(0) |
| 66 | + }) |
| 67 | + |
| 68 | + it('在底部选项边缘,上箭头移动高亮选项到第一项', () => { |
| 69 | + wrapper.setData({ |
| 70 | + optionActiveIndex: 0 |
| 71 | + }) |
| 72 | + $input.trigger('keyup.up') |
| 73 | + expect(wrapper.vm.optionActiveIndex).toEqual( |
| 74 | + wrapper.vm.filtedData.length - 1 |
| 75 | + ) |
| 76 | + }) |
| 77 | +}) |
0 commit comments