Vuex test helpers
A helper library to construct mock Vuex stores for tests
Installation
$ npm install -D vuex-test-helpers
Dependencies
Vuex test helpers currently requires testdouble.js. Vuex test helpers uses td.function()
to create mock actions and getters. You should install testdouble as a dev dependency in your project as well.
Example usage
Let's say we have a Vue component, MyComponent, that uses state data and an action from a Vuex store:
myComponent.vue
{{ message }} Load awesome data! {{ superAwesomeData }}
MyComponent.spec.js
API
createMockStore() : Returns MockStoreWrapper
Returns a MockStoreWrapper
object which can be used to build a mock store.
const mockStore = store
withState(state: Object) : Returns MockStoreWrapper
Updates the working store with the given state object. If no state is provided, the store will default state to an empty object.
Returns a MockStoreWrapper
so all methods are chainable.
const mockStore = console // { foo: 'bar' }
withGetters(getters: Array) : Returns MockStoreWrapper
Updates the working store with mock getters. If no getters are provided, the store will create a default empty getters object.
Returns a MockStoreWrapper
so all methods are chainable.
const mockStore = // in a spectd // in a view component that has mapped myGetterconsole // my getter value
withActions(actions: Array) : Returns MockStoreWrapper
Updates the working store with mock actions. If no actions are provided, the store will create a default empty actions object.
Returns a MockStoreWrapper
so all methods are chainable.
const mockStore = // in a spectd // in a view component that has mapped myActionthis
withModule(name: String, module: Object) : Returns MockStoreWrapper
Adds a module with the given name to the working store. If no name is provider a default, empty modules object is created. Use this method to add modules created with createMockModule
to your store.
Returns a MockStoreWrapper
so all methods are chainable.
const testModule = const mockStore = console // { test: { state: { foo: 'bar' } } }
createMockModule(name: String) : Returns MockModuleWrapper
Returns a MockModuleWrapper
object which can be used to build a mock module.
const mockModule = console // testconsole // {}
withState(state : Object) : Returns MockModuleWrapper
Updates the modules state with the given object. If no object is provided a default empty object is created.
Returns a MockModuleWrapper
so all methods are chainable.
const testModule = console // { foo: 'bar' }
withGetters(getters : Array) : Returns MockModuleWrapper
Creates mocked getters for the module. If no getters are provided a default empty getters object is created.
Returns a MockModuleWrapper
so all methods are chainable.
const mockModule = // in a spectd // in a view component that has mapped myGetter from the test moduleconsole // my getter value
withActions(actions : Array) : Returns MockModuleWrapper
Creates mocked actions for the module. If no actions are provided a default empty actions object is created.
Returns a MockModuleWrapper
so all methods are chainable.
const mockModule = // in a spectd // in a view component that has mapped myAction from the test modulethis
withModule(name: String, module: Object) : Returns MockModuleWrapper
Adds a module with the given name to the current module. If no name is provider a default, empty modules object is created. You can use this method to create store structures that have nested modules infitely deep.
Returns a MockModuleWrapper
so all methods are chainable.
const testModule = const nestedModule = testModuleconsole// {// state: { foo: 'bar' },// modules: {// nested: {// state: { nested: 'state' }// }// }// }