all files / tests/unit/ select.spec.js

100% Statements 43/43
100% Branches 0/0
100% Functions 8/8
100% Lines 43/43
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137                                                                                                                                                                                           
import chai, {expect} from 'chai'
import sinon from 'sinon'
import sinonChai from 'sinon-chai'
 
chai.use(sinonChai)
 
import {mount} from '@vue/test-utils'
 
import Vue from 'vue'
import AmSelect from '../../src/select/index'
import AmOption from '../../src/select/option'
 
Vue.component('am-select', AmSelect)
Vue.component('am-option', AmOption)
 
describe('AmSelect', () => {
  it('存在.', () => {
    expect(AmSelect).to.be.ok
  })
  it('设置 selected', () => {
    const wrapper = mount(AmSelect, {
      propsData: {
        selected: ''
      },
      slots: {
        default: `
          <am-option value="A">Apple</am-option>
          <am-option value="B">Banana</am-option>
          <am-option value="C">Cherry</am-option>
        `
      }
    })
    wrapper.setProps({'selected': 'B'})
    expect(wrapper.props().selected).to.eq('B')
  })
  it('设置 disabled', () => {
    const wrapper = mount(AmSelect, {
      propsData: {
        selected: '',
        disabled: true
      },
      slots: {
        default: `
          <am-option value="A">Apple</am-option>
          <am-option value="B">Banana</am-option>
          <am-option value="C">Cherry</am-option>
        `
      }
    })
    expect(wrapper.props().disabled).to.eq(true)
    expect(wrapper.classes('am-select-disabled')).to.eq(true)
    wrapper.find('.am-select').trigger('click')
    expect(wrapper.vm.$data.visible).to.eq(false)
  })
  it('设置 placeholder', () => {
    const wrapper = mount(AmSelect, {
      propsData: {
        selected: '',
        placeholder: '哈哈'
      },
    })
    expect(wrapper.props().placeholder).to.eq('哈哈')
  })
  it('设置 clearable', () => {
    const wrapper = mount(AmSelect, {
      propsData: {
        selected: 'B',
        clearable: true
      },
      slots: {
        default: `
          <am-option value="A">Apple</am-option>
          <am-option value="B">Banana</am-option>
          <am-option value="C">Cherry</am-option>
        `
      }
    })
    expect(wrapper.props().clearable).to.eq(true)
    wrapper.find('.am-select').trigger('mouseenter')
    expect(wrapper.find('.am-select-clear').exists()).to.eq(true)
  })
 
  it('设置 update:selected 事件', () => {
    const callback = sinon.fake()
    const wrapper = mount(AmSelect, {
      attachToDocument: true,
      propsData: {
        selected: ''
      },
      slots: {
        default: `
          <am-option value="A">Apple</am-option>
          <am-option value="B">Banana</am-option>
          <am-option value="C">Cherry</am-option>
        `
      },
      listeners: {
        'update:selected': callback
      }
    })
    wrapper.find('.am-select').trigger('click')
    expect(wrapper.find('.am-select-options').isVisible()).to.eq(true)
    wrapper.find('[data-name="B"]').trigger('click')
    expect(callback).to.have.been.calledWith('B')
    expect(wrapper.find('.am-select-options').isVisible()).to.eq(false)
    wrapper.destroy()
  })
 
  it('设置 clear 事件', () => {
    const callback = sinon.fake()
    const wrapper = mount(AmSelect, {
      propsData: {
        selected: 'A',
        clearable: true,
        disabled: false,
      },
      slots: {
        default: `
          <am-option value="A">Apple</am-option>
          <am-option value="B">Banana</am-option>
          <am-option value="C">Cherry</am-option>
        `
      },
      listeners: {
        'update:selected': callback
      }
    })
    wrapper.find('.am-select').trigger('mouseenter')
    const el = wrapper.find('.am-select-clear')
    expect(el.exists()).to.eq(true)
    el.trigger('click')
    expect(callback).to.have.been.calledWith("")
    wrapper.find('.am-select').trigger('mouseleave')
    expect(wrapper.find('.am-select-clear').exists()).to.eq(false)
  })
})