11import '@testing-library/jest-dom'
2+ import { createStore } from 'vuex'
23import { render , fireEvent } from '..'
34import VuexTest from './components/Store/VuexTest'
45import { store } from './components/Store/store'
56
7+ test ( 'basic test with Vuex store' , async ( ) => {
8+ const storeInstance = createStore ( store )
9+
10+ const { getByTestId, getByText} = render ( VuexTest , {
11+ global : {
12+ plugins : [ storeInstance ] ,
13+ } ,
14+ } )
15+
16+ expect ( getByTestId ( 'count-value' ) ) . toHaveTextContent ( '0' )
17+
18+ await fireEvent . click ( getByText ( '+' ) )
19+ expect ( getByTestId ( 'count-value' ) ) . toHaveTextContent ( '1' )
20+
21+ await fireEvent . click ( getByText ( '+' ) )
22+ expect ( getByTestId ( 'count-value' ) ) . toHaveTextContent ( '2' )
23+
24+ await fireEvent . click ( getByText ( '-' ) )
25+ expect ( getByTestId ( 'count-value' ) ) . toHaveTextContent ( '1' )
26+ } )
27+
628// A common testing pattern is to create a custom renderer for a specific test
729// file. This way, common operations such as registering a Vuex store can be
830// abstracted out while avoiding sharing mutable state.
931//
1032// Tests should be completely isolated from one another.
1133// Read this for additional context: https://kentcdodds.com/blog/test-isolation-with-react
1234function renderVuexTestComponent ( customStore ) {
13- // Render the component and merge the original store and the custom one
14- // provided as a parameter. This way, we can alter some behaviors of the
15- // initial implementation.
16- return render ( VuexTest , { store : { ...store , ...customStore } } )
35+ // Create a custom store with the original one and the one coming as a
36+ // parameter. This way we can alter some of its values.
37+ const mergedStoreInstance = createStore ( { ...store , ...customStore } )
38+
39+ return render ( VuexTest , {
40+ global : {
41+ plugins : [ mergedStoreInstance ] ,
42+ } ,
43+ } )
1744}
1845
1946test ( 'can render with vuex with defaults' , async ( ) => {
@@ -26,7 +53,7 @@ test('can render with vuex with defaults', async () => {
2653
2754test ( 'can render with vuex with custom initial state' , async ( ) => {
2855 const { getByTestId, getByText} = renderVuexTestComponent ( {
29- state : { count : 3 } ,
56+ state : ( ) => ( { count : 3 } ) ,
3057 } )
3158
3259 await fireEvent . click ( getByText ( '-' ) )
@@ -37,17 +64,21 @@ test('can render with vuex with custom initial state', async () => {
3764test ( 'can render with vuex with custom store' , async ( ) => {
3865 // This is a silly store that can never be changed.
3966 // eslint-disable-next-line no-shadow
40- const store = {
41- state : { count : 1000 } ,
67+ const store = createStore ( {
68+ state : ( ) => ( { count : 1000 } ) ,
4269 actions : {
4370 increment : ( ) => jest . fn ( ) ,
4471 decrement : ( ) => jest . fn ( ) ,
4572 } ,
46- }
73+ } )
4774
4875 // Notice how here we are not using the helper rendering method, because
4976 // there's no need to do that here. We're passing a whole store.
50- const { getByTestId, getByText} = render ( VuexTest , { store} )
77+ const { getByTestId, getByText} = render ( VuexTest , {
78+ global : {
79+ plugins : [ store ] ,
80+ } ,
81+ } )
5182
5283 await fireEvent . click ( getByText ( '+' ) )
5384 expect ( getByTestId ( 'count-value' ) ) . toHaveTextContent ( '1000' )
0 commit comments