Skip to content

Commit 9d2fcc4

Browse files
committed
Merge branch 'release/1.3.0'
2 parents d6a66f8 + f3e6572 commit 9d2fcc4

File tree

4 files changed

+720
-605
lines changed

4 files changed

+720
-605
lines changed

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vuex-map-fields",
3-
"version": "1.2.3",
3+
"version": "1.3.0",
44
"description": "Enable two-way data binding for form fields saved in a Vuex store",
55
"keywords": [
66
"vue",
@@ -29,18 +29,18 @@
2929
},
3030
"devDependencies": {
3131
"@avalanche/eslint-config": "^2.0.0",
32-
"@babel/core": "^7.0.0-beta.47",
33-
"@babel/preset-env": "^7.0.0-beta.47",
34-
"@vue/test-utils": "^1.0.0-beta.16",
32+
"@babel/core": "^7.0.0-beta.51",
33+
"@babel/preset-env": "^7.0.0-beta.51",
34+
"@vue/test-utils": "^1.0.0-beta.18",
3535
"babel-core": "^7.0.0-bridge.0",
36-
"babel-jest": "^22.4.4",
36+
"babel-jest": "^23.0.1",
3737
"coveralls": "^3.0.1",
3838
"eslint": "^4.19.1",
39-
"eslint-plugin-compat": "^2.2.0",
39+
"eslint-plugin-compat": "^2.4.0",
4040
"eslint-plugin-import": "^2.12.0",
4141
"eslint-plugin-markdown": "^1.0.0-beta.6",
42-
"jest": "^22.4.4",
43-
"rollup": "^0.59.2",
42+
"jest": "^23.1.0",
43+
"rollup": "^0.60.2",
4444
"rollup-plugin-babel": "^4.0.0-beta.0",
4545
"uglify-es": "^3.3.9",
4646
"vue": "^2.5.16",

src/index.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import arrayToObject from './lib/array-to-object';
22

33
function normalizeNamespace(fn) {
4-
return (namespace, map, getterType, mutationType) => {
5-
/* eslint-disable no-param-reassign */
6-
if (typeof namespace !== `string`) {
7-
mutationType = getterType;
8-
getterType = map;
9-
map = namespace;
10-
namespace = ``;
11-
} else if (namespace.charAt(namespace.length - 1) !== `/`) {
4+
return (...params) => {
5+
// eslint-disable-next-line prefer-const
6+
let [namespace, map, getterType, mutationType] =
7+
typeof params[0] === `string` ? [...params] : [``, ...params];
8+
9+
if (namespace.length && namespace.charAt(namespace.length - 1) !== `/`) {
1210
namespace += `/`;
1311
}
1412

1513
getterType = `${namespace}${getterType || `getField`}`;
1614
mutationType = `${namespace}${mutationType || `updateField`}`;
17-
/* eslint-enable */
1815

1916
return fn(namespace, map, getterType, mutationType);
2017
};
@@ -62,7 +59,6 @@ export const mapMultiRowFields = normalizeNamespace((
6259
getterType,
6360
mutationType,
6461
) => {
65-
// export function mapMultiRowFields(paths, getterType = `getField`, mutationType = `updateField`) {
6662
const pathsObject = Array.isArray(paths) ? arrayToObject(paths) : paths;
6763

6864
return Object.keys(pathsObject).reduce((entries, key) => {
@@ -97,6 +93,8 @@ export const mapMultiRowFields = normalizeNamespace((
9793
export const createHelpers = ({ getterType, mutationType }) => ({
9894
[getterType]: getField,
9995
[mutationType]: updateField,
100-
mapFields: fields => mapFields(fields, getterType, mutationType),
101-
mapMultiRowFields: paths => mapMultiRowFields(paths, getterType, mutationType),
96+
mapFields: normalizeNamespace((namespace, fields) =>
97+
mapFields(namespace, fields, getterType, mutationType)),
98+
mapMultiRowFields: normalizeNamespace((namespace, paths) =>
99+
mapMultiRowFields(namespace, paths, getterType, mutationType)),
102100
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import Vuex from 'vuex';
2+
import { createLocalVue, shallowMount } from '@vue/test-utils';
3+
4+
import { createHelpers, getField, updateField } from './package/src';
5+
6+
const localVue = createLocalVue();
7+
8+
localVue.use(Vuex);
9+
10+
const { mapFields } = createHelpers({
11+
getterType: `getField`,
12+
mutationType: `updateField`,
13+
});
14+
15+
describe(`Component initialized with namespaced Vuex module.`, () => {
16+
let Component;
17+
let store;
18+
let wrapper;
19+
20+
beforeEach(() => {
21+
Component = {
22+
template: `<input id="foo" v-model="foo">`,
23+
computed: {
24+
...mapFields(`fooModule`, [
25+
`foo`,
26+
]),
27+
},
28+
};
29+
30+
store = new Vuex.Store({
31+
modules: {
32+
fooModule: {
33+
namespaced: true,
34+
state: {
35+
foo: ``,
36+
},
37+
getters: {
38+
getField,
39+
},
40+
mutations: {
41+
updateField,
42+
},
43+
},
44+
},
45+
});
46+
47+
wrapper = shallowMount(Component, { localVue, store });
48+
});
49+
50+
test(`It should render the component.`, () => {
51+
expect(wrapper.exists()).toBe(true);
52+
});
53+
54+
test(`It should update field values when the store is updated.`, () => {
55+
store.state.fooModule.foo = `foo`;
56+
57+
expect(wrapper.element.value).toBe(`foo`);
58+
});
59+
60+
test(`It should update the store when the field values are updated.`, () => {
61+
wrapper.element.value = `foo`;
62+
wrapper.trigger(`input`);
63+
64+
expect(store.state.fooModule.foo).toBe(`foo`);
65+
});
66+
});

0 commit comments

Comments
 (0)