From 9d63d71acf1a82565af9feacd7610501b3b6d050 Mon Sep 17 00:00:00 2001 From: Ben Limmer Date: Fri, 25 Jun 2021 02:47:02 -0600 Subject: [PATCH] feat: allow passing instantiated Vuex store (#232) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: allow passing instantiated Vuex store * Update types/index.d.ts Co-authored-by: AdriĆ  Fontcuberta --- src/__tests__/vuex.js | 31 +++++++++++++++++++++++++++++++ src/render.js | 2 +- types/test.ts | 25 +++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) 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",