Skip to content

Commit 56cc316

Browse files
committed
Add simple vuex example
1 parent fa6f841 commit 56cc316

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed
+20-24
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1-
<template>
2-
<div>
3-
<h2>Counter</h2>
4-
<div>
5-
<button data-testid="decrementer" @click="decrement">-</button>
6-
<span data-testid="count-value">{{ count }}</span>
7-
<button data-testid="incrementer" @click="increment">+</button>
8-
</div>
9-
</div>
10-
</template>
11-
12-
<script>
13-
import { mapActions, mapState } from 'vuex'
14-
15-
export default {
16-
computed: {
17-
...mapState(['count'])
18-
},
19-
20-
methods: {
21-
...mapActions(['decrement', 'increment'])
22-
}
23-
}
24-
</script>
1+
<template>
2+
<h2>Counter</h2>
3+
<button data-testid="decrementer" @click="decrement">-</button>
4+
<span data-testid="count-value">{{ count }}</span>
5+
<button data-testid="incrementer" @click="increment">+</button>
6+
</template>
7+
8+
<script>
9+
import {mapActions, mapState} from 'vuex'
10+
11+
export default {
12+
computed: {
13+
...mapState(['count']),
14+
},
15+
16+
methods: {
17+
...mapActions(['decrement', 'increment']),
18+
},
19+
}
20+
</script>

src/__tests__/vuex.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@ import {render, fireEvent} from '..'
44
import VuexTest from './components/Store/VuexTest'
55
import {store} from './components/Store/store'
66

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+
728
// A common testing pattern is to create a custom renderer for a specific test
829
// file. This way, common operations such as registering a Vuex store can be
930
// abstracted out while avoiding sharing mutable state.
@@ -13,11 +34,11 @@ import {store} from './components/Store/store'
1334
function renderVuexTestComponent(customStore) {
1435
// Create a custom store with the original one and the one coming as a
1536
// parameter. This way we can alter some of its values.
16-
const mergedStore = createStore({...store, ...customStore})
37+
const mergedStoreInstance = createStore({...store, ...customStore})
1738

1839
return render(VuexTest, {
1940
global: {
20-
plugins: [mergedStore],
41+
plugins: [mergedStoreInstance],
2142
},
2243
})
2344
}

0 commit comments

Comments
 (0)