1
0
mirror of https://github.com/lana-k/sqliteviz.git synced 2025-12-06 10:08:52 +08:00

use actions, add store tests

This commit is contained in:
lana-k
2025-01-05 22:30:12 +01:00
parent d6408bdd85
commit 87f9f9eb01
5 changed files with 102 additions and 41 deletions

View File

@@ -42,5 +42,40 @@ export default {
} }
return value 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
}
})
}
} }
} }

View File

@@ -63,40 +63,5 @@ export default {
}, },
setInquiries (state, value) { setInquiries (state, value) {
state.inquiries = 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
}
})
}
} }
} }

View File

@@ -331,7 +331,7 @@ export default {
this.errorMsg = "Inquiry name can't be empty" this.errorMsg = "Inquiry name can't be empty"
return return
} }
this.$store.commit('renameInquiry', { this.$store.dispatch('renameInquiry', {
inquiryId: this.processedInquiryId, inquiryId: this.processedInquiryId,
newName: this.newName newName: this.newName
}) })
@@ -341,7 +341,7 @@ export default {
}, },
duplicateInquiry (index) { duplicateInquiry (index) {
const newInquiry = storedInquiries.duplicateInquiry(this.showedInquiries[index]) const newInquiry = storedInquiries.duplicateInquiry(this.showedInquiries[index])
this.$store.commit('addInquiry', newInquiry) this.$store.dispatch('addInquiry', newInquiry)
}, },
showDeleteDialog (idsSet) { showDeleteDialog (idsSet) {
this.deleteGroup = idsSet.size > 1 this.deleteGroup = idsSet.size > 1
@@ -353,14 +353,14 @@ export default {
deleteInquiry () { deleteInquiry () {
this.$modal.hide('delete') this.$modal.hide('delete')
if (!this.deleteGroup) { if (!this.deleteGroup) {
this.$store.commit('deleteInquiries', new Set().add(this.processedInquiryId)) this.$store.dispatch('deleteInquiries', new Set().add(this.processedInquiryId))
// Clear checkbox // Clear checkbox
if (this.selectedInquiriesIds.has(this.processedInquiryId)) { if (this.selectedInquiriesIds.has(this.processedInquiryId)) {
this.selectedInquiriesIds.delete(this.processedInquiryId) this.selectedInquiriesIds.delete(this.processedInquiryId)
} }
} else { } else {
this.$store.commit('deleteInquiries', this.selectedInquiriesIds) this.$store.dispatch('deleteInquiries', this.selectedInquiriesIds)
// Clear checkboxes // Clear checkboxes
this.selectedInquiriesIds.clear() this.selectedInquiriesIds.clear()

View File

@@ -1,7 +1,12 @@
import { expect } from 'chai' import { expect } from 'chai'
import actions from '@/store/actions' import actions from '@/store/actions'
const { addTab } = actions const {
addTab,
addInquiry,
deleteInquiries,
renameInquiry
} = actions
describe('actions', () => { describe('actions', () => {
it('addTab adds new blank tab', async () => { it('addTab adds new blank tab', async () => {
@@ -81,4 +86,50 @@ describe('actions', () => {
expect(state.tabs).to.have.lengthOf(2) expect(state.tabs).to.have.lengthOf(2)
expect(state.untitledLastIndex).to.equal(0) 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)
})
}) })

View File

@@ -8,7 +8,8 @@ const {
updatePredefinedInquiries, updatePredefinedInquiries,
setDb, setDb,
setLoadingPredefinedInquiries, setLoadingPredefinedInquiries,
setPredefinedInquiriesLoaded setPredefinedInquiriesLoaded,
setInquiries
} = mutations } = mutations
describe('mutations', () => { describe('mutations', () => {
@@ -360,4 +361,13 @@ describe('mutations', () => {
setPredefinedInquiriesLoaded(state, true) setPredefinedInquiriesLoaded(state, true)
expect(state.predefinedInquiriesLoaded).to.equal(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])
})
}) })