From 87f9f9eb01773d431931b07b607ae88156ba7e0d Mon Sep 17 00:00:00 2001 From: lana-k Date: Sun, 5 Jan 2025 22:30:12 +0100 Subject: [PATCH] use actions, add store tests --- src/store/actions.js | 35 ++++++++++++++++++++ src/store/mutations.js | 35 -------------------- src/views/Main/Inquiries/index.vue | 8 ++--- tests/store/actions.spec.js | 53 +++++++++++++++++++++++++++++- tests/store/mutations.spec.js | 12 ++++++- 5 files changed, 102 insertions(+), 41 deletions(-) diff --git a/src/store/actions.js b/src/store/actions.js index 58796e9..7c65899 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -42,5 +42,40 @@ export default { } return value + }, + addInquiry ({ state }, newInquiry) { + state.inquiries.push(newInquiry) + }, + deleteInquiries ({ state, commit }, inquiryIdSet) { + state.inquiries = state.inquiries.filter( + inquiry => !inquiryIdSet.has(inquiry.id) + ) + + // Close deleted inquiries if it was opened + const tabs = state.tabs + let i = tabs.length - 1 + while (i > -1) { + if (inquiryIdSet.has(tabs[i].id)) { + commit('deleteTab', tabs[i]) + } + i-- + } + }, + renameInquiry ({ state, commit }, {inquiryId, newName}) { + const renamingInquiry = state.inquiries + .find(inquiry => inquiry.id === inquiryId) + + renamingInquiry.name = newName + + // update tab, if renamed inquiry is opened + const tab = state.tabs.find(tab => tab.id === renamingInquiry.id) + if (tab) { + commit('updateTab', { + tab, + newValues: { + name: newName + } + }) + } } } diff --git a/src/store/mutations.js b/src/store/mutations.js index b90fbf5..16c9c9a 100644 --- a/src/store/mutations.js +++ b/src/store/mutations.js @@ -63,40 +63,5 @@ export default { }, setInquiries (state, value) { state.inquiries = value - }, - addInquiry (state, newInquiry) { - state.inquiries.push(newInquiry) - }, - deleteInquiries (state, inquiryIdSet) { - state.inquiries = state.inquiries.filter( - inquiry => !inquiryIdSet.has(inquiry.id) - ) - - // Close deleted inquiries if it was opened - const tabs = state.tabs - let i = tabs.length - 1 - while (i > -1) { - if (inquiryIdSet.has(tabs[i].id)) { - this.commit('deleteTab', tabs[i]) - } - i-- - } - }, - renameInquiry (state, {inquiryId, newName}) { - const renamingInquiry = state.inquiries - .find(inquiry => inquiry.id === inquiryId) - - renamingInquiry.name = newName - - // update tab, if renamed inquiry is opened - const tab = state.tabs.find(tab => tab.id === renamingInquiry.id) - if (tab) { - this.commit('updateTab', { - tab, - newValues: { - name: newName - } - }) - } } } diff --git a/src/views/Main/Inquiries/index.vue b/src/views/Main/Inquiries/index.vue index 5a46d3d..0b174ed 100644 --- a/src/views/Main/Inquiries/index.vue +++ b/src/views/Main/Inquiries/index.vue @@ -331,7 +331,7 @@ export default { this.errorMsg = "Inquiry name can't be empty" return } - this.$store.commit('renameInquiry', { + this.$store.dispatch('renameInquiry', { inquiryId: this.processedInquiryId, newName: this.newName }) @@ -341,7 +341,7 @@ export default { }, duplicateInquiry (index) { const newInquiry = storedInquiries.duplicateInquiry(this.showedInquiries[index]) - this.$store.commit('addInquiry', newInquiry) + this.$store.dispatch('addInquiry', newInquiry) }, showDeleteDialog (idsSet) { this.deleteGroup = idsSet.size > 1 @@ -353,14 +353,14 @@ export default { deleteInquiry () { this.$modal.hide('delete') if (!this.deleteGroup) { - this.$store.commit('deleteInquiries', new Set().add(this.processedInquiryId)) + this.$store.dispatch('deleteInquiries', new Set().add(this.processedInquiryId)) // Clear checkbox if (this.selectedInquiriesIds.has(this.processedInquiryId)) { this.selectedInquiriesIds.delete(this.processedInquiryId) } } else { - this.$store.commit('deleteInquiries', this.selectedInquiriesIds) + this.$store.dispatch('deleteInquiries', this.selectedInquiriesIds) // Clear checkboxes this.selectedInquiriesIds.clear() diff --git a/tests/store/actions.spec.js b/tests/store/actions.spec.js index 45ea799..e18ade5 100644 --- a/tests/store/actions.spec.js +++ b/tests/store/actions.spec.js @@ -1,7 +1,12 @@ import { expect } from 'chai' import actions from '@/store/actions' -const { addTab } = actions +const { + addTab, + addInquiry, + deleteInquiries, + renameInquiry +} = actions describe('actions', () => { it('addTab adds new blank tab', async () => { @@ -81,4 +86,50 @@ describe('actions', () => { expect(state.tabs).to.have.lengthOf(2) expect(state.untitledLastIndex).to.equal(0) }) + + it('addInquiry', async () => { + const state = { + inquiries: [1,2,3] + } + + await addInquiry({ state }, 4) + expect(state.inquiries).to.eql([1,2,3,4]) + }) + + it('deleteInquiries', async () => { + const state = { + inquiries: [{ id: 1 }, { id: 2 }, { id: 3 }], + tabs: [{ id: 3 }, { id: 2 }] + } + const commit = sinon.spy() + + await deleteInquiries({ state, commit }, new Set().add(2)) + expect(state.inquiries).to.eql([{ id: 1 }, { id: 3 }]) + expect(commit.calledWith('deleteTab', { id: 2 })).to.equal(true) + }) + + it('renameInquiry', async () => { + const state = { + inquiries: [ + { id: 1, name: 'foo'}, + { id: 2, name: 'bar' }, + { id: 3, name: 'baz' }, + ], + tabs: [{ id: 1, name: 'foo'}, { id: 2, name: 'bar' }] + } + const commit = sinon.spy() + + await renameInquiry({ state, commit }, {inquiryId: 2, newName: 'new name'}) + expect(state.inquiries).to.eql([ + { id: 1, name: 'foo'}, + { id: 2, name: 'new name' }, + { id: 3, name: 'baz' }, + ]) + expect(commit.calledWith('updateTab', { + tab: { id: 2, name: 'bar' }, + newValues: { + name: 'new name' + } + })).to.equal(true) + }) }) diff --git a/tests/store/mutations.spec.js b/tests/store/mutations.spec.js index 9c61234..1171e71 100644 --- a/tests/store/mutations.spec.js +++ b/tests/store/mutations.spec.js @@ -8,7 +8,8 @@ const { updatePredefinedInquiries, setDb, setLoadingPredefinedInquiries, - setPredefinedInquiriesLoaded + setPredefinedInquiriesLoaded, + setInquiries } = mutations describe('mutations', () => { @@ -360,4 +361,13 @@ describe('mutations', () => { setPredefinedInquiriesLoaded(state, true) expect(state.predefinedInquiriesLoaded).to.equal(true) }) + + it('setInquiries', () => { + const state = { + inquiries: [] + } + + setInquiries(state, [1,2,3]) + expect(state.inquiries).to.eql([1,2,3]) + }) })