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

100% Statements 28/28
100% Branches 0/0
100% Functions 8/8
100% Lines 28/28
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                                                                                                               
import chai, { expect } from 'chai'
import sinon from 'sinon'
import sinonChai from 'sinon-chai'
chai.use(sinonChai)
import { mount } from '@vue/test-utils'
import Toast from '../../src/toast/toast'
 
describe('Toast', () => {
  it('存在.', () => {
    expect(Toast).to.be.ok
  })
  it('可以设置 autoClose.', done => {
    const callback = sinon.fake()
    mount(Toast, {
      propsData: {
        autoClose: 20
      },
      slots: {
        default: 'ha'
      },
      listeners: {
        close: callback
      }
    })
    setTimeout(() => {
      expect(callback).to.have.been.called
      done()
    }, 22)
  })
  it('设置 callback,自动关闭时调用', done => {
    const callback1 = sinon.fake()
    const callback2 = sinon.fake()
    mount(Toast, {
      propsData: {
        autoClose: 20,
        callback: callback1
      },
      slots: {
        default: 'ha'
      },
      listeners: {
        close: callback2
      }
    })
    setTimeout(() => {
      expect(callback2).to.have.been.called
      expect(callback1).to.have.been.called
      done()
    }, 22)
  })
  it('接受 buttonText 属性, 手动关闭时调用', () => {
    const callback1 = sinon.fake()
    const callback2 = sinon.fake()
    const wrapper = mount(Toast, {
      propsData: {
        autoClose: false,
        callback: callback2,
        buttonText: 'k'
      },
      slots: {
        default: 'ha'
      },
      listeners: {
        close: callback1
      }
    })
    wrapper.find('.am-toast-close').trigger('click')
    expect(callback2).to.have.been.called
    expect(callback1).to.have.been.called
  })
  it('接受 position 属性', () => {
    const wrapper = mount(Toast, {
      propsData: {
        autoClose: 20,
        position: 'middle'
      },
      slots: {
        default: 'ha'
      }
    })
    expect(wrapper.classes('position-middle')).to.eq(true)
  })
})