Skip to content

Improve Vuex test #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tests/__tests__/components/Store/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const store = {
state: {
count: 0
},
actions: {
increment: ({ commit, state }) => commit('SET_COUNT', state.count + 1),
decrement: ({ commit, state }) => commit('SET_COUNT', state.count - 1)
},
mutations: {
SET_COUNT: (state, count) => { state.count = count }
}
}
43 changes: 25 additions & 18 deletions tests/__tests__/vuex.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
import 'jest-dom/extend-expect'

import VuexTest from './components/VuexTest'
import { cleanup, render, fireEvent } from '@testing-library/vue'

import VuexTest from './components/Store/VuexTest'
import { store } from './components/Store/store'

afterEach(cleanup)

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

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

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

test('can render with vuex with custom initial state', async () => {
store.state.count = 3
const { getByTestId, getByText } = render(VuexTest, { store })
const { getByTestId, getByText } = renderVuexTestComponent({
state: { count: 3 }
})
await fireEvent.click(getByText('-'))

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

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

// This is a silly store that can never be changed.
const store = { state: { count: 1000 } }

// Notice how here we are not using the helper method, because there's no
// need to do that.
const { getByTestId, getByText } = render(VuexTest, { store })

await fireEvent.click(getByText('+'))
Expand Down