vuex-mock-store
Advanced tools
Comparing version 0.0.0 to 0.0.1
{ | ||
"name": "vuex-mock-store", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "Simple and straightforward mock for Vuex v3.x Store", | ||
@@ -36,8 +36,10 @@ "main": "src/index.js", | ||
"devDependencies": { | ||
"babel-jest": "^23.0.1", | ||
"babel-preset-env": "^1.7.0", | ||
"codecov": "^3.0.2", | ||
"eslint": "^5.0.0", | ||
"eslint-config-posva": "^2.0.0", | ||
"jest": "^23.1.0" | ||
"@vue/test-utils": "^1.0.0-beta.25", | ||
"codecov": "^3.1.0", | ||
"eslint": "^5.6.0", | ||
"eslint-config-posva": "^2.0.1", | ||
"jest": "^23.6.0", | ||
"vue": "^2.5.17", | ||
"vue-template-compiler": "^2.5.17", | ||
"vuex": "^3.0.1" | ||
}, | ||
@@ -67,3 +69,6 @@ "repository": { | ||
}, | ||
"homepage": "https://github.com/posva/vuex-mock-store#readme" | ||
"homepage": "https://github.com/posva/vuex-mock-store#readme", | ||
"dependencies": { | ||
"lodash.clonedeep": "^4.5.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# vuex-mock-store [![Build Status](https://img.shields.io/circleci/project/posva/vuex-mock-store/master.svg)](https://circleci.com/gh/posva/vuex-mock-store) [![npm package](https://img.shields.io/npm/v/vuex-mock-store.svg)](https://www.npmjs.com/package/vuex-mock-store) [![coverage](https://img.shields.io/codecov/c/github/posva/vuex-mock-store.svg)](https://codecov.io/github/posva/vuex-mock-store) [![thanks](https://img.shields.io/badge/thanks-%E2%99%A5-ff69b4.svg)](https://github.com/posva/thanks) | ||
# vuex-mock-store [![Build Status](https://badgen.net/circleci/github/posva/vuex-mock-store)](https://circleci.com/gh/posva/vuex-mock-store) [![npm package](https://badgen.net/npm/v/vuex-mock-store)](https://www.npmjs.com/package/vuex-mock-store) [![coverage](https://badgen.net/codecov/c/github/posva/vuex-mock-store)](https://codecov.io/github/posva/vuex-mock-store) [![thanks](https://img.shields.io/badge/thanks-%E2%99%A5-ff69b4.svg)](https://github.com/posva/thanks) | ||
@@ -62,3 +62,3 @@ > Simple and straightforward mock for Vuex v3.x Store | ||
getters: { | ||
name: () => 'Eduardo', | ||
name: 'Eduardo', | ||
}, | ||
@@ -93,4 +93,8 @@ state: { | ||
```js | ||
// namespaced module | ||
expect(store.commit).toHaveBeenCalledWith('moduleA/setValue') | ||
expect(store.dispatch).toHaveBeenCalledWith('moduleA/postValue') | ||
// non-namespaced | ||
expect(store.commit).toHaveBeenCalledWith('setValue') | ||
expect(store.dispatch).toHaveBeenCalledWith('postValue') | ||
``` | ||
@@ -122,6 +126,6 @@ | ||
Store getters. You can directly modify it to change the returned value by a getter: | ||
Store getters. You can directly modify it to change a value: | ||
```js | ||
store.getters.upperCaseName = state => 'JEFF' | ||
store.getters.upperCaseName = 'JEFF' | ||
``` | ||
@@ -137,4 +141,4 @@ | ||
- [jest.fn]() | ||
- [sinon.spy]() | ||
- [jest.fn](#TODO) | ||
- [sinon.spy](#TODO) | ||
@@ -141,0 +145,0 @@ ## Related |
@@ -1,9 +0,12 @@ | ||
import clone from 'lodash.clone' | ||
const clone = require('lodash.clonedeep') | ||
export default class Store { | ||
constructor({ getters = {}, state = {} } = {}) { | ||
// TODO: support functions in getters by using a Proxy | ||
exports.Store = class Store { | ||
constructor ({ getters = {}, state = {} } = {}) { | ||
this.dispatch = jest.fn() | ||
this.commit = jest.fn() | ||
this.getters = { ...(this.__initialGetters = getters) } | ||
this.state = clone((this.__initialState = state)) | ||
this.__initialGetters = getters | ||
this.__initialState = state | ||
this._initialize() | ||
// this is necessary for map* helpers | ||
@@ -14,3 +17,33 @@ this._modulesNamespaceMap = new Proxy( | ||
// always return the root store as the context module | ||
get: () => ({ context: this }), | ||
get: (target, key) => { | ||
if (typeof key !== 'string') return Reflect.get(this, key) | ||
const modules = key.split('/') | ||
// modules always have a trailing / | ||
if (modules.length > 1) { | ||
// remove trailing empty module | ||
modules.pop() | ||
// get the nested state corresponding to the module | ||
let state = this.state | ||
for (let module of modules) { | ||
if (state[module]) state = state[module] | ||
else { | ||
throw new Error( | ||
`[vuex-mock-store] module "${key.slice(0, -1)}" not defined in state:`, | ||
this.state | ||
) | ||
} | ||
} | ||
return { | ||
context: { | ||
_modulesNamespaceMap: this._modulesNamespaceMap, | ||
state, | ||
getters: this.getters, | ||
}, | ||
} | ||
} else { | ||
return Reflect.get(this, key) | ||
} | ||
}, | ||
} | ||
@@ -20,8 +53,12 @@ ) | ||
reset() { | ||
_initialize () { | ||
this.getters = { ...this.__initialGetters } | ||
this.state = clone(this.__initialState) | ||
} | ||
reset () { | ||
this.dispatch.mockReset() | ||
this.commit.mockReset() | ||
this.getters = { ...(this.__initialGetters = getters) } | ||
this.state = clone(this.__initialState) | ||
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
8052
56
149
1
8
+ Addedlodash.clonedeep@^4.5.0
+ Addedlodash.clonedeep@4.5.0(transitive)