Skip to content

Commit 1159003

Browse files
committed
feat: allow passing instantiated Vuex store
1 parent 4efca8a commit 1159003

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/__tests__/vuex.js

+31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import '@testing-library/jest-dom'
22
import {render, fireEvent} from '@testing-library/vue'
3+
import Vuex from 'vuex'
34

45
import VuexTest from './components/Store/VuexTest'
56
import {store} from './components/Store/store'
@@ -54,3 +55,33 @@ test('can render with vuex with custom store', async () => {
5455
await fireEvent.click(getByText('-'))
5556
expect(getByTestId('count-value')).toHaveTextContent('1000')
5657
})
58+
59+
test('can render with an instantiated Vuex store', async () => {
60+
const {getByTestId, getByText} = render(VuexTest, {
61+
store: new Vuex.Store({
62+
state: {count: 3},
63+
mutations: {
64+
increment(state) {
65+
state.count++
66+
},
67+
decrement(state) {
68+
state.count--
69+
},
70+
},
71+
actions: {
72+
increment(context) {
73+
context.commit('increment')
74+
},
75+
decrement(context) {
76+
context.commit('decrement')
77+
},
78+
},
79+
}),
80+
})
81+
82+
await fireEvent.click(getByText('+'))
83+
expect(getByTestId('count-value')).toHaveTextContent('4')
84+
85+
await fireEvent.click(getByText('-'))
86+
expect(getByTestId('count-value')).toHaveTextContent('3')
87+
})

src/render.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function render(
3131
const Vuex = require('vuex')
3232
localVue.use(Vuex)
3333

34-
vuexStore = new Vuex.Store(store)
34+
vuexStore = store instanceof Vuex.Store ? store : new Vuex.Store(store)
3535
}
3636

3737
if (routes) {

types/index.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export interface RenderOptions<V extends Vue, S = {}>
4040
// The props and store options special-cased by Vue Testing Library and NOT passed to mount().
4141
extends Omit<ThisTypedMountOptions<V>, 'store' | 'props'> {
4242
props?: object
43-
store?: StoreOptions<S>
43+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
44+
store?: StoreOptions<S> | Store<any>
4445
routes?: RouteConfig[]
4546
container?: Element
4647
baseElement?: Element

0 commit comments

Comments
 (0)