vuex-mock-store
Advanced tools
Comparing version 0.0.1 to 0.0.2
{ | ||
"name": "vuex-mock-store", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Simple and straightforward mock for Vuex v3.x Store", | ||
"main": "src/index.js", | ||
"types": "src/index.d.ts", | ||
"author": { | ||
@@ -36,2 +37,3 @@ "name": "Eduardo San Martin Morote", | ||
"devDependencies": { | ||
"@types/jest": "^23.3.2", | ||
"@vue/test-utils": "^1.0.0-beta.25", | ||
@@ -38,0 +40,0 @@ "codecov": "^3.1.0", |
@@ -0,19 +1,30 @@ | ||
// @ts-check | ||
const clone = require('lodash.clonedeep') | ||
// TODO: support functions in getters by using a Proxy | ||
const defaultSpy = { | ||
create: () => jest.fn(), | ||
/** | ||
* @param {jest.Mock} spy | ||
*/ | ||
reset: spy => spy.mockReset(), | ||
} | ||
exports.spy = defaultSpy | ||
exports.Store = class Store { | ||
constructor ({ getters = {}, state = {} } = {}) { | ||
this.dispatch = jest.fn() | ||
this.commit = jest.fn() | ||
this.__initialGetters = getters | ||
this.__initialState = state | ||
constructor ({ getters = {}, state = {}, spy = defaultSpy } = {}) { | ||
this._spy = spy | ||
this.commit = this._spy.create() | ||
this.dispatch = this._spy.create() | ||
this._initialGetters = getters | ||
this._initialState = state | ||
this._initialize() | ||
// this is necessary for map* helpers | ||
/** @type {any} */ | ||
this._modulesNamespaceMap = new Proxy( | ||
{}, | ||
{ | ||
// always return the root store as the context module | ||
get: (target, key) => { | ||
if (typeof key !== 'string') return Reflect.get(this, key) | ||
// for Symbols | ||
if (typeof key !== 'string') return Reflect.get(target, key) | ||
@@ -31,4 +42,6 @@ const modules = key.split('/') | ||
throw new Error( | ||
`[vuex-mock-store] module "${key.slice(0, -1)}" not defined in state:`, | ||
this.state | ||
`[vuex-mock-store] module "${key.slice( | ||
0, | ||
-1 | ||
)}" not defined in state:\n${JSON.stringify(this.state, null, 2)}` | ||
) | ||
@@ -40,4 +53,7 @@ } | ||
context: { | ||
// make sure we reuse this proxy | ||
_modulesNamespaceMap: this._modulesNamespaceMap, | ||
// pass the right state | ||
state, | ||
// getters are not nested | ||
getters: this.getters, | ||
@@ -47,3 +63,3 @@ }, | ||
} else { | ||
return Reflect.get(this, key) | ||
return Reflect.get(target, key) | ||
} | ||
@@ -56,11 +72,12 @@ }, | ||
_initialize () { | ||
this.getters = { ...this.__initialGetters } | ||
this.state = clone(this.__initialState) | ||
// getters is a plain object | ||
this.getters = { ...this._initialGetters } | ||
this.state = clone(this._initialState) | ||
} | ||
reset () { | ||
this.dispatch.mockReset() | ||
this.commit.mockReset() | ||
this._spy.reset(this.dispatch) | ||
this._spy.reset(this.commit) | ||
this._initialize() | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9119
5
96
9