Skip to content

Commit

Permalink
使用 chai.spy 监听回调函数
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankFang committed Jun 24, 2018
1 parent 4b3e3dc commit 0719419
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"devDependencies": {
"@vue/component-compiler-utils": "^2.0.0",
"chai": "^4.1.2",
"chai-spies": "^1.0.0",
"node-sass": "^4.9.0",
"parcel-bundler": "^1.9.2",
"vue-template-compiler": "^2.5.16"
Expand Down
10 changes: 6 additions & 4 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ new Vue({
})

import chai from 'chai'
import spies from 'chai-spies'
chai.use(spies)

const expect = chai.expect
// 单元测试
Expand Down Expand Up @@ -83,18 +85,18 @@ const expect = chai.expect
vm.$destroy()
}
{
// mock
const Constructor = Vue.extend(Button)
const vm = new Constructor({
propsData: {
icon: 'settings',
}
})
vm.$mount()
vm.$on('click', function () {
expect(1).to.eq(1)
})
let spy = chai.spy(function(){})

vm.$on('click', spy)
// 希望这个函数被执行
let button = vm.$el
button.click()
expect(spy).to.have.been.called()
}

0 comments on commit 0719419

Please sign in to comment.