Skip to content

Commit

Permalink
feat: allow passing instantiated Vuex store (#232)
Browse files Browse the repository at this point in the history
* feat: allow passing instantiated Vuex store

* Update types/index.d.ts

Co-authored-by: Adrià Fontcuberta <afontcu@gmail.com>
  • Loading branch information
blimmer and afontcu authored Jun 25, 2021
1 parent 4efca8a commit 9d63d71
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/__tests__/vuex.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import '@testing-library/jest-dom'
import {render, fireEvent} from '@testing-library/vue'
import Vuex from 'vuex'

import VuexTest from './components/Store/VuexTest'
import {store} from './components/Store/store'
Expand Down Expand Up @@ -54,3 +55,33 @@ test('can render with vuex with custom store', async () => {
await fireEvent.click(getByText('-'))
expect(getByTestId('count-value')).toHaveTextContent('1000')
})

test('can render with an instantiated Vuex store', async () => {
const {getByTestId, getByText} = render(VuexTest, {
store: new Vuex.Store({
state: {count: 3},
mutations: {
increment(state) {
state.count++
},
decrement(state) {
state.count--
},
},
actions: {
increment(context) {
context.commit('increment')
},
decrement(context) {
context.commit('decrement')
},
},
}),
})

await fireEvent.click(getByText('+'))
expect(getByTestId('count-value')).toHaveTextContent('4')

await fireEvent.click(getByText('-'))
expect(getByTestId('count-value')).toHaveTextContent('3')
})
2 changes: 1 addition & 1 deletion src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function render(
const Vuex = require('vuex')
localVue.use(Vuex)

vuexStore = new Vuex.Store(store)
vuexStore = store instanceof Vuex.Store ? store : new Vuex.Store(store)
}

if (routes) {
Expand Down
25 changes: 25 additions & 0 deletions types/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Vue from 'vue'
import Vuex from 'vuex'
import {render, fireEvent, screen, waitFor} from '@testing-library/vue'

declare const elem: Element
Expand Down Expand Up @@ -127,6 +128,30 @@ export function testConfigCallback() {
})
}

export function testInstantiatedStore() {
render(SomeComponent, {
store: new Vuex.Store({
state: {count: 3},
mutations: {
increment(state) {
state.count++
},
decrement(state) {
state.count--
},
},
actions: {
increment(context) {
context.commit('increment')
},
decrement(context) {
context.commit('decrement')
},
},
}),
})
}

/*
eslint
testing-library/prefer-explicit-assert: "off",
Expand Down

0 comments on commit 9d63d71

Please sign in to comment.