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

add test for closing browser tab

This commit is contained in:
lana-k
2021-02-04 16:19:25 +01:00
parent 9d38f2a047
commit 5006f6935a
2 changed files with 43 additions and 3 deletions

View File

@@ -86,9 +86,6 @@ export default {
created () {
window.addEventListener('beforeunload', this.leavingSqliteviz)
},
unmounted () {
window.removeEventListener('beforeunload', this.leavingSqliteviz)
},
methods: {
leavingSqliteviz (event) {
if (this.tabs.some(tab => tab.isUnsaved)) {

View File

@@ -229,4 +229,47 @@ describe('Tabs.vue', () => {
// check that the dialog is closed
expect(wrapper.find('[data-modal="close-warn"]').exists()).to.equal(false)
})
it('Prevents closing a tab of a browser if there is unsaved query', () => {
// mock store state
const state = {
tabs: [
{ id: 1, name: 'foo', query: 'select * from foo', chart: [], isUnsaved: false },
{ id: 2, name: null, tempName: 'Untitled', query: '', chart: [], isUnsaved: true }
],
currentTabId: 2
}
const store = new Vuex.Store({ state, mutations })
// mount the component
const wrapper = shallowMount(Tabs, { store })
const event = new Event('beforeunload')
sinon.spy(event, 'preventDefault')
wrapper.vm.leavingSqliteviz(event)
expect(event.preventDefault.calledOnce).to.equal(true)
})
it("Doesn't prevent closing a tab of a browser if there is unsaved query", () => {
// mock store state
const state = {
tabs: [
{ id: 1, name: 'foo', query: 'select * from foo', chart: [], isUnsaved: false }
],
currentTabId: 1
}
const store = new Vuex.Store({ state, mutations })
// mount the component
const wrapper = shallowMount(Tabs, { store })
const event = new Event('beforeunload')
sinon.spy(event, 'preventDefault')
wrapper.vm.leavingSqliteviz(event)
expect(event.preventDefault.calledOnce).to.equal(false)
})
})