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

62.5% Statements 15/24
100% Branches 0/0
37.5% Functions 3/8
62.5% Lines 15/24
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                                                                                           
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 Uploader from '../../src/uploader/uploader'
import http from '../../src/http'
 
describe('Uploader', () => {
  it('存在.', () => {
    expect(Uploader).to.be.ok
  })
  it('可以上传一个.', done => {
    let stub = sinon.stub(http, 'post').callsFake((url, options) => {
      setTimeout(function() {
        options.success(JSON.stringify({ id: '1212' }))
      }, 100)
    })
    const wrapper = mount(Uploader, {
      propsData: {
        name: 'files',
        action: '/upload',
        method: 'post',
        parseReponse: data => {
          let { id } = JSON.parse(data)
          return `/preview/${id}`
        },
        fileList: []
      },
      slots: {
        default: `<button id="x">click me</button>`
      },
      listeners: {
        'update:fileList': fileList => {
          wrapper.setProps({ fileList })
        },
        uploaded: () => {
          expect(wrapper.find('use').exists()).to.eq(false)
          expect(wrapper.props().fileList[0].url).to.eq('/preview/1212')
          stub.restore()
          done()
        }
      }
    })
    wrapper.find('#x').trigger('click')
    let inputWrapper = wrapper.find('input[type="file"]')
    let input = inputWrapper.element
    const file1 = new File(['hahaha'], 'a.png')
 
    const data = new DataTransfer()
    data.items.add(file1)
    input.files = data.files
 
    // let use = wrapper.find('use').element
    // expect(use.getAttribute('xlink:href')).to.eq('#i-loading')
    done()
  })
})