Skip to content

Commit

Permalink
✅ Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
distolma committed Mar 12, 2020
1 parent bf23f3b commit 4e3eea8
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
53 changes: 53 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const Vue = require('vue')

const { mountComponent, ComponentWithChild, Child } = require('./utils')

it('should render with initial state', () => {
let { store, wrapper } = mountComponent()
expect(wrapper.vm.$state).toEqual(store.get())
})

it('should provide Storeon', () => {
let { store, wrapper } = mountComponent()
expect(store.on).toHaveBeenCalledTimes(1)
expect(store.get).toHaveBeenCalledTimes(1)
expect(wrapper.vm.$storeon).toBe(store)
})

it('should unsubscribe only on root destroy', () => {
let { wrapper, store } = mountComponent(
{ component: ComponentWithChild }
)
let child = wrapper.find(Child)

store.dispatch('inc')
expect(wrapper.vm.$state.count).toBe(1)
child.destroy()
store.dispatch('inc')
expect(wrapper.vm.$state.count).toBe(2)
wrapper.destroy()
store.dispatch('inc')
expect(wrapper.vm.$state.count).toBe(2)
})

it('should update view on dispatch', async () => {
let { store, updated, wrapper } = mountComponent()
expect(wrapper.text()).toBe('0')
store.dispatch('inc')
await Vue.nextTick()
expect(wrapper.text()).toBe('1')
expect(updated).toHaveBeenCalledTimes(1)
})

it('should rerenders only changed states', async () => {
let { wrapper, store, updated } = mountComponent(
{ component: ComponentWithChild }
)
let child = wrapper.find(Child)

expect(child.text()).toBe('baz')
store.dispatch('foo/set', 'foobaz')
await Vue.nextTick()
expect(updated).not.toHaveBeenCalled()
expect(child.text()).toBe('foobaz')
})
44 changes: 44 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { mount, createLocalVue } = require('@vue/test-utils')
const { createStoreon } = require('storeon')

const { StoreonVue } = require('../')

const Component = {
template: '<div>{{$state.count}}</div>'
}

const Child = {
name: 'Child',
template: '<div>{{$state.foo}}</div>'
}

const ComponentWithChild = {
template: '<div>{{$state.count}}<Child /></div>',
components: { Child }
}

function createStore () {
let counter = storeon => {
storeon.on('@init', () => ({ count: 0, foo: 'baz' }))
storeon.on('inc', ({ count }) => ({ count: count + 1 }))
storeon.on('foo/set', (_, data) => ({ foo: data }))
}
return createStoreon([counter])
}

function mountComponent ({ store, component } = {}) {
store = store || createStore()
component = component || Component

let updated = jest.fn()
jest.spyOn(store, 'get')
jest.spyOn(store, 'on')

let localVue = createLocalVue()
localVue.use(StoreonVue)
let wrapper = mount(component, { localVue, store, updated })

return { wrapper, store, updated }
}

module.exports = { mountComponent, ComponentWithChild, Component, Child }

0 comments on commit 4e3eea8

Please sign in to comment.