From d8a403b3defa619624ab9d01c3347bd65796df37 Mon Sep 17 00:00:00 2001 From: Guillaume Vincent Date: Thu, 2 Feb 2017 11:43:01 +0100 Subject: [PATCH] add DELETE_PASSWORD mutation --- src/store/mutation-types.js | 3 ++- src/store/mutations.js | 8 ++++++++ test/store.js | 21 +++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/store/mutation-types.js b/src/store/mutation-types.js index b2f6ae3..9f449a2 100644 --- a/src/store/mutation-types.js +++ b/src/store/mutation-types.js @@ -2,4 +2,5 @@ export const LOGOUT = 'LOGOUT'; export const LOGIN = 'LOGIN'; export const SET_CURRENT_PASSWORD = 'SET_CURRENT_PASSWORD'; export const SET_DEFAULT_PASSWORD = 'SET_DEFAULT_PASSWORD'; -export const SET_PASSWORDS = 'SET_PASSWORDS'; \ No newline at end of file +export const SET_PASSWORDS = 'SET_PASSWORDS'; +export const DELETE_PASSWORD = 'DELETE_PASSWORD'; \ No newline at end of file diff --git a/src/store/mutations.js b/src/store/mutations.js index 05159cc..7cc913b 100644 --- a/src/store/mutations.js +++ b/src/store/mutations.js @@ -21,5 +21,13 @@ export const mutations = { }, [types.SET_PASSWORDS](state, {passwords}){ state.passwords = passwords; + }, + [types.DELETE_PASSWORD](state, {id}){ + state.passwords = state.passwords.filter(password => { + return password.id !== id; + }); + if (state.currentPassword && state.currentPassword.id === id) { + state.currentPassword = Object.assign({}, state.defaultPassword); + } } }; diff --git a/test/store.js b/test/store.js index 789ae63..c2a76d6 100644 --- a/test/store.js +++ b/test/store.js @@ -135,4 +135,25 @@ test('SET_PASSWORDS', t => { SET_PASSWORDS(state, {passwords: [{site: 'site1'}, {site: 'site2'}]}); t.is(state.passwords[0].site, 'site1'); t.is(state.passwords[1].site, 'site2'); +}); + +test('DELETE_PASSWORD', t => { + const DELETE_PASSWORD = mutations[types.DELETE_PASSWORD]; + const state = { + passwords: [{id: '1', site: 'site1'}, {id: '2', site: 'site2'}] + }; + t.is(state.passwords.length, 2); + DELETE_PASSWORD(state, {id: '1'}); + t.is(state.passwords.length, 1); +}); + +test('DELETE_PASSWORD clean current password with default password if same id', t => { + const DELETE_PASSWORD = mutations[types.DELETE_PASSWORD]; + const state = { + passwords: [{id: '1', length: 30}, {id: '2', length: 16}], + currentPassword: {id: '1', length: 30}, + defaultPassword: {length: 16} + }; + DELETE_PASSWORD(state, {id: '1'}); + t.is(state.currentPassword.length, 16); }); \ No newline at end of file