Skip to content

Instantly share code, notes, and snippets.

@lmiller1990
Created February 3, 2020 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmiller1990/25e9670be532abd000d465f2d31a2b25 to your computer and use it in GitHub Desktop.
Save lmiller1990/25e9670be532abd000d465f2d31a2b25 to your computer and use it in GitHub Desktop.
const { createApp, h, nextTick, ref } = require('vue')
const Increment = {
setup(props, context) {
return () => h('button', { onClick: () => context.emit('increment') }, 'Button')
}
}
const App = {
components: {
Increment
},
setup() {
return {
count: ref(0)
}
},
render() {
return h(
'div',
{},
[
h('div', { id: 'root' }, `Count is ${this.count}`),
h(Increment, { onIncrement: () => this.count++ })
]
)
}
}
class DOMWrapper {
constructor(el) {
this.el = el
}
trigger(evtString) {
const evt = document.createEvent('Event')
evt.initEvent(evtString)
this.el.dispatchEvent(evt)
}
}
class VueWrapper {
constructor(vm) {
this.vm = vm
}
find(selector) {
return new DOMWrapper(this.vm.$el.querySelector(selector))
}
}
function mount(Component) {
const el = document.createElement('div')
el.id = 'app'
document.body.appendChild(el)
return new VueWrapper(createApp(Component).mount('#app'))
}
test('Renders', async () => {
const wrapper = mount(App)
expect(wrapper.find('#root').el.textContent).toBe('Count is 0')
wrapper.find('button').trigger('click')
await nextTick()
expect(wrapper.find('#root').el.textContent).toBe('Count is 1')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment