Skip to content

Commit

Permalink
Merge branch 'release/1.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
maoberlehner committed Jun 13, 2018
2 parents d6a66f8 + f3e6572 commit 9d2fcc4
Show file tree
Hide file tree
Showing 4 changed files with 720 additions and 605 deletions.
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vuex-map-fields",
"version": "1.2.3",
"version": "1.3.0",
"description": "Enable two-way data binding for form fields saved in a Vuex store",
"keywords": [
"vue",
Expand Down Expand Up @@ -29,18 +29,18 @@
},
"devDependencies": {
"@avalanche/eslint-config": "^2.0.0",
"@babel/core": "^7.0.0-beta.47",
"@babel/preset-env": "^7.0.0-beta.47",
"@vue/test-utils": "^1.0.0-beta.16",
"@babel/core": "^7.0.0-beta.51",
"@babel/preset-env": "^7.0.0-beta.51",
"@vue/test-utils": "^1.0.0-beta.18",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^22.4.4",
"babel-jest": "^23.0.1",
"coveralls": "^3.0.1",
"eslint": "^4.19.1",
"eslint-plugin-compat": "^2.2.0",
"eslint-plugin-compat": "^2.4.0",
"eslint-plugin-import": "^2.12.0",
"eslint-plugin-markdown": "^1.0.0-beta.6",
"jest": "^22.4.4",
"rollup": "^0.59.2",
"jest": "^23.1.0",
"rollup": "^0.60.2",
"rollup-plugin-babel": "^4.0.0-beta.0",
"uglify-es": "^3.3.9",
"vue": "^2.5.16",
Expand Down
22 changes: 10 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import arrayToObject from './lib/array-to-object';

function normalizeNamespace(fn) {
return (namespace, map, getterType, mutationType) => {
/* eslint-disable no-param-reassign */
if (typeof namespace !== `string`) {
mutationType = getterType;
getterType = map;
map = namespace;
namespace = ``;
} else if (namespace.charAt(namespace.length - 1) !== `/`) {
return (...params) => {
// eslint-disable-next-line prefer-const
let [namespace, map, getterType, mutationType] =
typeof params[0] === `string` ? [...params] : [``, ...params];

if (namespace.length && namespace.charAt(namespace.length - 1) !== `/`) {
namespace += `/`;
}

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

return fn(namespace, map, getterType, mutationType);
};
Expand Down Expand Up @@ -62,7 +59,6 @@ export const mapMultiRowFields = normalizeNamespace((
getterType,
mutationType,
) => {
// export function mapMultiRowFields(paths, getterType = `getField`, mutationType = `updateField`) {
const pathsObject = Array.isArray(paths) ? arrayToObject(paths) : paths;

return Object.keys(pathsObject).reduce((entries, key) => {
Expand Down Expand Up @@ -97,6 +93,8 @@ export const mapMultiRowFields = normalizeNamespace((
export const createHelpers = ({ getterType, mutationType }) => ({
[getterType]: getField,
[mutationType]: updateField,
mapFields: fields => mapFields(fields, getterType, mutationType),
mapMultiRowFields: paths => mapMultiRowFields(paths, getterType, mutationType),
mapFields: normalizeNamespace((namespace, fields) =>
mapFields(namespace, fields, getterType, mutationType)),
mapMultiRowFields: normalizeNamespace((namespace, paths) =>
mapMultiRowFields(namespace, paths, getterType, mutationType)),
});
66 changes: 66 additions & 0 deletions test/module-namespaced-double.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import Vuex from 'vuex';
import { createLocalVue, shallowMount } from '@vue/test-utils';

import { createHelpers, getField, updateField } from './package/src';

const localVue = createLocalVue();

localVue.use(Vuex);

const { mapFields } = createHelpers({
getterType: `getField`,
mutationType: `updateField`,
});

describe(`Component initialized with namespaced Vuex module.`, () => {
let Component;
let store;
let wrapper;

beforeEach(() => {
Component = {
template: `<input id="foo" v-model="foo">`,
computed: {
...mapFields(`fooModule`, [
`foo`,
]),
},
};

store = new Vuex.Store({
modules: {
fooModule: {
namespaced: true,
state: {
foo: ``,
},
getters: {
getField,
},
mutations: {
updateField,
},
},
},
});

wrapper = shallowMount(Component, { localVue, store });
});

test(`It should render the component.`, () => {
expect(wrapper.exists()).toBe(true);
});

test(`It should update field values when the store is updated.`, () => {
store.state.fooModule.foo = `foo`;

expect(wrapper.element.value).toBe(`foo`);
});

test(`It should update the store when the field values are updated.`, () => {
wrapper.element.value = `foo`;
wrapper.trigger(`input`);

expect(store.state.fooModule.foo).toBe(`foo`);
});
});
Loading

0 comments on commit 9d2fcc4

Please sign in to comment.