Skip to content

Commit 4f874bc

Browse files
authored
Merge branch 'master' into signup
2 parents a7f256e + e97ac1d commit 4f874bc

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export const store = {
2+
state: {
3+
count: 0
4+
},
5+
actions: {
6+
increment: ({ commit, state }) => commit('SET_COUNT', state.count + 1),
7+
decrement: ({ commit, state }) => commit('SET_COUNT', state.count - 1)
8+
},
9+
mutations: {
10+
SET_COUNT: (state, count) => { state.count = count }
11+
}
12+
}

tests/__tests__/vuex.js

+25-18
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,50 @@
11
import 'jest-dom/extend-expect'
2-
3-
import VuexTest from './components/VuexTest'
42
import { cleanup, render, fireEvent } from '@testing-library/vue'
53

4+
import VuexTest from './components/Store/VuexTest'
5+
import { store } from './components/Store/store'
6+
67
afterEach(cleanup)
78

8-
const store = {
9-
state: {
10-
count: 0
11-
},
12-
actions: {
13-
increment: ({ commit, state }) => commit('SET_COUNT', state.count + 1),
14-
decrement: ({ commit, state }) => commit('SET_COUNT', state.count - 1)
15-
},
16-
mutations: {
17-
SET_COUNT: (state, count) => { state.count = count }
18-
}
9+
// A common testing pattern is to create a custom renderer for a specific test
10+
// file. This way, common operations such as registering a Vuex store can be
11+
// abstracted out while avoiding sharing mutable state.
12+
//
13+
// Tests should be completely isolated from one another.
14+
// Read this for additional context: https://kentcdodds.com/blog/test-isolation-with-react
15+
function renderVuexTestComponent (customStore) {
16+
// Render the component and merge the original store and the custom one
17+
// provided as a parameter. This way, we can alter some behaviors of the
18+
// initial implementation.
19+
return render(VuexTest, {
20+
store: { ...store, ...customStore }
21+
})
1922
}
2023

2124
test('can render with vuex with defaults', async () => {
22-
const { getByTestId, getByText } = render(VuexTest, { store })
25+
const { getByTestId, getByText } = renderVuexTestComponent()
2326
await fireEvent.click(getByText('+'))
2427

2528
expect(getByTestId('count-value')).toHaveTextContent('1')
2629
})
2730

2831
test('can render with vuex with custom initial state', async () => {
29-
store.state.count = 3
30-
const { getByTestId, getByText } = render(VuexTest, { store })
32+
const { getByTestId, getByText } = renderVuexTestComponent({
33+
state: { count: 3 }
34+
})
3135
await fireEvent.click(getByText('-'))
3236

3337
expect(getByTestId('count-value')).toHaveTextContent('2')
3438
})
3539

3640
test('can render with vuex with custom store', async () => {
37-
// this is a silly store that can never be changed
38-
jest.spyOn(console, 'error').mockImplementation(() => {})
41+
jest.spyOn(console, 'error').mockImplementation(() => { })
3942

43+
// This is a silly store that can never be changed.
4044
const store = { state: { count: 1000 } }
45+
46+
// Notice how here we are not using the helper method, because there's no
47+
// need to do that.
4148
const { getByTestId, getByText } = render(VuexTest, { store })
4249

4350
await fireEvent.click(getByText('+'))

0 commit comments

Comments
 (0)