Skip to content

Instantly share code, notes, and snippets.

@lmiller1990
Created January 29, 2018 13:14
Show Gist options
  • Save lmiller1990/2c264448856853d139c10ca3c029df23 to your computer and use it in GitHub Desktop.
Save lmiller1990/2c264448856853d139c10ca3c029df23 to your computer and use it in GitHub Desktop.
import Vue from 'vue'
const EvtBus = new Vue()
EvtBus.addEvent = (evt, fn) => {
EvtBus.$on(evt, () => {
fn()
})
}
export { EvtBus }
import { EvtBus } from './EventBus'
describe('EventBus', () => {
it('adds a new event using addEvent', () => {
const evtToAdd = () => {}
EvtBus.addEvent('test', evtToAdd)
expect(EvtBus._events['test']).toBeTruthy()
})
})
import { shallow } from 'vue-test-utils'
import Test from './Test'
describe('Test', () => {
it('renders', () => {
const wrapper = shallow(Test)
wrapper.find('button').trigger('click')
expect(wrapper.emitted().test.length).toBe(1)
})
})
<template>
<div>
<button @click="go">Emit event</button>
</div>
</template>
<script>
import { EvtBus } from './EventBus'
export default {
name: 'Test',
created () {
EvtBus.addEvent('test', () => {
console.log('Add this function to event bus')
})
},
methods: {
go () {
this.$emit('test')
EvtBus.$emit('test')
}
}
}
</script>
<style scoped>
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment