Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
vuex-mock-store
Advanced tools
Simple and straightforward mock for Vuex v3.x Store
Supports using mapMutations
and mapActions
as well as directly doing this.$store.commit()
and this.$store.dispatch()
npm install -D vuex-mock-store
# with yarn
yarn add -D vuex-mock-store
ℹ️: All examples use Jest API
Usage with vue-test-utils:
import { Store } from 'vuex-mock-store'
import { mount } from '@vue/test-utils'
import MyComponent from '@/components/MyComponent.vue'
// create the Store mock
const store = new Store()
// add other mocks here so they are accessible in every component
const mocks = {
$store: store,
}
// reset spies, initial state and getters
afterEach(() => store.reset())
describe('MyComponent.vue', () => {
let wrapper
beforeEach(() => {
wrapper = mount(MyComponent, { mocks })
})
it('commits init on creation', () => {
expect(store.commit).toHaveBeenCalledOnce()
expect(store.commit).toHaveBeenCalledWith('init')
})
it('dispatch save when clicking button', () => {
wrapper.find('button.save').trigger('click')
expect(store.dispatch).toHaveBeenCalledOnce()
expect(store.dispatch).toHaveBeenCalledWith('save', { name: 'Eduardo' })
})
})
You can provide a getters
, and state
object to mock them:
const store = new Store({
getters: {
name: () => 'Eduardo',
},
state: {
counter: 0,
},
})
To mock modules state
, simply provide a nested object for state
:
new Store({
state: {
moduleA: {
moduleC: {
value: 'foo',
},
},
moduleB: {
value: 'bar',
},
},
})
Testing actions and mutations depend whether your modules are namespaced or not. If they are namespaced, make sure to provide the full action/mutation name:
expect(store.commit).toHaveBeenCalledWith('moduleA/setValue')
expect(store.dispatch).toHaveBeenCalledWith('moduleA/postValue')
state
, providing custom getters
You can modify the state
and getters
directly for any test. Calling store.reset()
will reset them to the initial values provided.
Store
classconstructor(options)
options
state
: initial state object, default: {}
getters
: getters object, default: {}
state
Store state. You can directly modify it to change state:
store.state.name = 'Jeff'
getters
Store getters. You can directly modify it to change the returned value by a getter:
store.getters.upperCaseName = state => 'JEFF'
reset
Reset commit
and dispatch
spies and restore getters
and state
to their initial values
commit
& dispatch
Spies. Dependent on the testing framework
FAQs
Simple and straightforward mock for Vuex v3.x Store
The npm package vuex-mock-store receives a total of 112,560 weekly downloads. As such, vuex-mock-store popularity was classified as popular.
We found that vuex-mock-store demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.