diff --git a/src/__tests__/vuex.js b/src/__tests__/vuex.js index f77206e6..4ff17fab 100644 --- a/src/__tests__/vuex.js +++ b/src/__tests__/vuex.js @@ -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' @@ -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') +}) diff --git a/src/render.js b/src/render.js index 63ba145d..cd8f913f 100644 --- a/src/render.js +++ b/src/render.js @@ -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) { diff --git a/types/test.ts b/types/test.ts index 18092963..7e04f391 100644 --- a/types/test.ts +++ b/types/test.ts @@ -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 @@ -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",