注意 done setTimeout timeout(10000) 的使用
vue : 2.5.2
webpack : 3.6.0
karma : 1.4.1
autobahn : 17.5.2
// hello.vue
<script>
import autobahn from 'autobahn'
export default {
name: 'Hello',
data () {
return {
msg: 'Welcome to Your Vue.js App',
connction: null,
autobahnSession: null
}
},
mounted () {
this.connection = new autobahn.Connection({
url: 'ws://crossbar.ss-gg.com:28080/ws',
realm: 'realm1'
})
this.connection.onopen = (session, details) => {
this.autobahnSession = session
console.log('connection success')
function getData () {
return '123'
}
this.autobahnSession.register('com.ss-gg.test.getData', getData, {
invoke: 'roundrobin'
}).then(
function (req) {
console.log('updateData register')
},
function (err) {
console.log('updateData error')
console.log(err)
}
)
//
}
this.connection.onclose = (reason, details) => {
console.log('connection close ')
}
this.connection.open()
this.autobahnSession = this.connection.session
},
methods: {
click () {
this.autobahnSession.call('com.ss-gg.fileserver.get_upload_token', [])
.then(
function (res) {
console.log('res')
console.log(res)
},
function (err) {
console.log('err')
console.log(err)
}
)
},
closeConn () {
this.connection.close()
console.log('close')
}
}
}
</script>
// Hello.spect.js
import Vue from 'vue'
import Hello from '@/components/Hello'
describe('Hello.vue', () => {
it('should render correct contents', () => {
const Constructor = Vue.extend(Hello)
const vm = new Constructor().$mount()
expect(vm.$el.querySelector('.hello h1').textContent)
.to.equal('Welcome to Your Vue.js App')
})
it('connection', (done) => {
const Constructor = Vue.extend(Hello)
const vm = new Constructor().$mount()
vm.connection.close()
vm.connection.open()
setTimeout(() => {
vm.autobahnSession.call('com.ss-gg.test.getData', [])
.then(
function (res) {
console.log('res')
console.log(res)
},
function (err) {
console.log('err')
console.log(err)
}
)
done()
}, 1000)
})
it('test call', (done) => {
const Constructor = Vue.extend(Hello)
const vm = new Constructor().$mount()
vm.connection.close()
vm.connection.open()
setTimeout(() => {
vm.click()
}, 1000)
setTimeout(() => {
done()
}, 2000)
}).timeout(10000)
//
it('autobahn connection close', () => {
const Constructor = Vue.extend(Hello)
const vm = new Constructor().$mount()
// vm.connection.open()
vm.connection.close()
})
})