@eddyerburgh
export const sum = (a, b) => a + b
import sum from './sum'
test('returns sum of input', () => {
})
import sum from './sum'
test('returns sum of input', () => {
expect(sum(1,3)).toBe(4)
})
Components
Actions
State
Actions
Mutations
State
Getters
Actions
Mutations
State
Getters
fetchItems
setItems
items
displayItems
Actions
Mutations
State
Getters
export default {
setItems (state, { items }) {
state.items = items
}
}
export default {
// ..
setItems (state, { items }) {
state.items = items
},
// ..
}
import mutations from './mutations'
test('setItems mutates state.items', () => {
})
import mutations from './mutations'
test('setItems mutates state.items', () => {
const state = {
items: []
}
const items = [{}, {}]
mutations.setItems(state, { items })
expect(state.items).toBe(items)
})
import mutations from './mutations'
test('setItems mutates state.items', () => {
const state = {
items: []
}
})
import mutations from './mutations'
test('setItems mutates state.items', () => {
const state = {
items: []
}
const items = [{}, {}]
})
import mutations from './mutations'
test('setItems mutates state.items', () => {
const state = {
items: []
}
const items = [{}, {}]
mutations.setItems(state, { items })
})
1. Call with state object and payload
2. Assert state value updated
displayItems ({ page, items }) {
const perPage = 20
const index = perPage * page
return items.slice(index - perPage, index)
},
test('returns items 21-40 if state.page value is 2', () => {
})
test('returns items 21-40 if state.page value is 2', () => {
const items = Array(42)
.fill()
.map((v, i) => ({ i }))
const state = {
items,
page: 2
}
})
test('returns items 21-40 if state.page value is 2', () => {
const items = Array(42)
.fill()
.map((v, i) => ({ i }))
const state = {
items,
page: 2
}
const result = getters.displayItems(state)
})
test('returns items 21-40 if state.page value is 2', () => {
const items = Array(42)
.fill()
.map((v, i) => ({ i }))
const state = {
items,
page: 2
}
const result = getters.displayItems(state)
expect(result).toEqual(items.slice(20, 40))
})
1. Call with state object, (resolved getters)
2. Assert return value
import { fetchData } from '../api'
export default {
fetchItems ({ commit }) {
return fetchData()
.then(items => commit('setItems', { items }))
}
}
import actions from './actions'
import { fetchData } from '../api'
jest.mock('../api')
test('commits items returned by api method', async () => {
})
import actions from './actions'
import { fetchData } from '../api'
jest.mock('../api')
test('commits items returned by api method', async () => {
const items = [1, 2, 3]
fetchData.mockResolvedValue(items)
const commit = jest.fn()
await actions.fetchItems({ commit })
expect(commit).toHaveBeenCalledWith(
'setItems', { items }
)
})
import actions from './actions'
import { fetchData } from '../api'
jest.mock('../api')
test('commits items returned by api method', async () => {
const items = [1, 2, 3]
fetchData.mockResolvedValue(items)
})
import actions from './actions'
import { fetchData } from '../api'
jest.mock('../api')
test('commits items returned by api method', async () => {
const items = [1, 2, 3]
fetchData.mockResolvedValue(items)
const commit = jest.fn()
})
import actions from './actions'
import { fetchData } from '../api'
jest.mock('../api')
test('commits items returned by api method', async () => {
const items = [1, 2, 3]
fetchData.mockResolvedValue(items)
const commit = jest.fn()
await actions.fetchItems({ commit })
})
1. Call with mocked store context, (payload)
2. Assert that action commits data
Actions
Mutations
State
Getters
store
fetchItems
getters.displayItems
1. Install Vuex on Vue constructor
2. Create a store instance
3. Test instance
Vue.use(Vuex)
Vue
new Vue({})
new Vue({})
Vue.use
Vue
new Vue({})
new Vue({})
localVue.use
localVue
Step 1
import { createLocalVue } from '@vue/test-utils'
const localVue = createLocalVue()
(on localVue constructor)
import { createLocalVue } from '@vue/test-utils'
const localVue = createLocalVue()
localVue.use(Vuex)
const store = new Vuex.Store({
state: {
items: []
},
actions,
mutations
})
const storeConfig = createStoreConfig()
const store = new Vuex.Store(storeConfig)
Step 2
import mutations from './mutations'
import actions from './actions'
export default function createStoreConfig() {
const state = {
items: [],
}
return {
state,
actions,
mutations
}
}
Step 3
await store.dispatch('fetchItems')
expect(store.state.items).toBe(items)
import createStoreConfig from './create-store-config'
import Vuex from 'vuex'
import { fetchData } from '../api'
import { createLocalVue } from '@vue/test-utils'
const localVue = createLocalVue()
localVue.use(Vuex)
jest.mock('../api')
test('returns first 20 items', async () => {
})
import createStoreConfig from './create-store-config'
import Vuex from 'vuex'
import { fetchData } from '../api'
import { createLocalVue } from '@vue/test-utils'
const localVue = createLocalVue()
localVue.use(Vuex)
jest.mock('../api')
test('returns first 20 items', async () => {
const items = Array(22)
.fill()
.map((v, i) => ({ i }))
items[0] = undefined
fetchData.mockResolvedValue(items)
})
import createStoreConfig from './create-store-config'
import Vuex from 'vuex'
import { fetchData } from '../api'
import { createLocalVue } from '@vue/test-utils'
import { createItems } from './utils'
const localVue = createLocalVue()
localVue.use(Vuex)
jest.mock('../api')
test('returns first 20 items', async () => {
const items = createItems(22)
items[0] = undefined
fetchData.mockResolvedValue(items)
const storeConfig = createStoreConfig()
const store = new Vuex.Store(storeConfig)
})
import createStoreConfig from './create-store-config'
import Vuex from 'vuex'
import { fetchData } from '../api'
import { createLocalVue } from '@vue/test-utils'
import { createItems } from './utils'
const localVue = createLocalVue()
localVue.use(Vuex)
jest.mock('../api')
test('returns first 20 items', async () => {
const items = createItems(22)
items[0] = undefined
fetchData.mockResolvedValue(items)
const storeConfig = createStoreConfig()
const store = new Vuex.Store(storeConfig)
await store.dispatch('fetchItems')
})
import createStoreConfig from './create-store-config'
import Vuex from 'vuex'
import { fetchData } from '../api'
import { createLocalVue } from '@vue/test-utils'
import { createItems } from './utils'
const localVue = createLocalVue()
localVue.use(Vuex)
jest.mock('../api')
test('returns first 20 items', async () => {
const items = createItems(22)
items[0] = undefined
fetchData.mockResolvedValue(items)
const storeConfig = createStoreConfig()
const store = new Vuex.Store(storeConfig)
await store.dispatch('fetchItems')
expect(store.getters.displayItems)
.toEqual(items.slice(1, 21))
})
import createStoreConfig from './create-store-config'
import Vuex from 'vuex'
import { fetchData } from '../api'
import { createLocalVue } from '@vue/test-utils'
import { createItems } from './utils'
const localVue = createLocalVue()
localVue.use(Vuex)
jest.mock('../api')
test('returns first 20 items', async () => {
const items = createItems(22)
items[0] = undefined
fetchData.mockResolvedValue(items)
})
npm run test:unit
npm run test:integration