From fb52b9b271eef886921f436cc412f79b9d703230 Mon Sep 17 00:00:00 2001 From: Guillaume Vincent Date: Sat, 28 Jan 2017 10:58:01 +0100 Subject: [PATCH] refactor mutations --- src/store/actions.js | 5 +++++ src/store/mutation-types.js | 4 ++++ src/store/mutations.js | 11 ++++++----- test/store.js | 32 +++++++++++++------------------- 4 files changed, 28 insertions(+), 24 deletions(-) create mode 100644 src/store/mutation-types.js diff --git a/src/store/actions.js b/src/store/actions.js index e69de29..9ed3a09 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -0,0 +1,5 @@ +import * as types from './mutation-types' + +export const saveDefaultOptions = ({commit}, payload) => { + commit(types.SET_DEFAULT_OPTIONS, payload); +}; \ No newline at end of file diff --git a/src/store/mutation-types.js b/src/store/mutation-types.js new file mode 100644 index 0000000..cbc3825 --- /dev/null +++ b/src/store/mutation-types.js @@ -0,0 +1,4 @@ +export const LOGOUT = 'LOGOUT'; +export const LOGIN = 'LOGIN'; +export const SET_CURRENT_PASSWORD = 'SET_CURRENT_PASSWORD'; +export const SET_DEFAULT_OPTIONS = 'SET_DEFAULT_OPTIONS'; \ No newline at end of file diff --git a/src/store/mutations.js b/src/store/mutations.js index 638f0f7..13e96d4 100644 --- a/src/store/mutations.js +++ b/src/store/mutations.js @@ -1,20 +1,21 @@ import {set} from 'vue'; +import * as types from './mutation-types'; function setState(state, id, object) { set(state, id, Object.assign({}, object)); } export const mutations = { - LOGIN(state){ + [types.LOGIN](state){ state.authenticated = true; }, - LOGOUT(state){ + [types.LOGOUT](state){ state.authenticated = false; }, - SET_CURRENT_PASSWORD_PROFILE(state, passwordProfile){ - setState(state, 'currentPasswordProfile', passwordProfile); + [types.SET_CURRENT_PASSWORD](state, {password}){ + setState(state, 'currentPassword', password); }, - SET_DEFAULT_OPTIONS(state, options){ + [types.SET_DEFAULT_OPTIONS](state, {options}){ setState(state, 'defaultOptions', options); } }; \ No newline at end of file diff --git a/test/store.js b/test/store.js index 7110813..5355ae4 100644 --- a/test/store.js +++ b/test/store.js @@ -15,24 +15,21 @@ test('LOGIN', t => { t.true(state.authenticated); }); -test('SET_CURRENT_PASSWORD_PROFILE', t => { - const {SET_CURRENT_PASSWORD_PROFILE} = mutations; - const state = {currentPasswordProfile: null}; - SET_CURRENT_PASSWORD_PROFILE(state, { - uppercase: true, - version: 2 - }); - t.is(state.currentPasswordProfile.version, 2); - t.true(state.currentPasswordProfile.uppercase); +test('SET_CURRENT_PASSWORD', t => { + const {SET_CURRENT_PASSWORD} = mutations; + const state = {currentPassword: null}; + SET_CURRENT_PASSWORD(state, {password: {uppercase: true, version: 2}}); + t.is(state.currentPassword.version, 2); + t.true(state.currentPassword.uppercase); }); -test('SET_CURRENT_PASSWORD_PROFILE immutable', t => { - const {SET_CURRENT_PASSWORD_PROFILE} = mutations; +test('SET_CURRENT_PASSWORD immutable', t => { + const {SET_CURRENT_PASSWORD} = mutations; const state = {}; - const profile = {version: 2}; - SET_CURRENT_PASSWORD_PROFILE(state, profile); - profile.version = 1; - t.is(state.currentPasswordProfile.version, 2); + const password = {version: 2}; + SET_CURRENT_PASSWORD(state, {password}); + password.version = 1; + t.is(state.currentPassword.version, 2); }); test('SET_DEFAULT_OPTIONS', t => { @@ -48,10 +45,7 @@ test('SET_DEFAULT_OPTIONS', t => { version: 2 } }; - SET_DEFAULT_OPTIONS(state, { - symbols: false, - length: 30 - }); + SET_DEFAULT_OPTIONS(state, {options: {symbols: false, length: 30}}); t.is(state.defaultOptions.length, 30); t.false(state.defaultOptions.symbols); }); \ No newline at end of file