mirror of
https://github.com/lana-k/sqliteviz.git
synced 2025-12-06 18:18:53 +08:00
Loading remote database and inquiries #109
This commit is contained in:
147
tests/views/LoadView.spec.js
Normal file
147
tests/views/LoadView.spec.js
Normal file
@@ -0,0 +1,147 @@
|
||||
import { expect } from 'chai'
|
||||
import sinon from 'sinon'
|
||||
import { mount } from '@vue/test-utils'
|
||||
import Vuex from 'vuex'
|
||||
import LoadView from '@/views/LoadView'
|
||||
import fu from '@/lib/utils/fileIo'
|
||||
import database from '@/lib/database'
|
||||
import realMutations from '@/store/mutations'
|
||||
import realActions from '@/store/actions'
|
||||
import flushPromises from 'flush-promises'
|
||||
import Tab from '@/lib/tab'
|
||||
|
||||
describe('LoadView.vue', () => {
|
||||
afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
it('Loads db and inquiries and redirects to workspace if no errors', async () => {
|
||||
const state = {
|
||||
tabs: []
|
||||
}
|
||||
const mutations = {
|
||||
setCurrentTabId: sinon.stub().callsFake(realMutations.setCurrentTabId),
|
||||
setDb: sinon.stub().callsFake(realMutations.setDb)
|
||||
}
|
||||
const actions = {
|
||||
addTab: sinon.stub().callsFake(realActions.addTab)
|
||||
}
|
||||
const store = new Vuex.Store({ state, mutations, actions })
|
||||
const $route = {
|
||||
path: '/workspace',
|
||||
query: {
|
||||
data_url: 'https://my-url/test.db',
|
||||
data_format: 'sqlite',
|
||||
inquiry_url: 'https://my-url/test_inquiries.json',
|
||||
inquiry_id: [1],
|
||||
maximize: 'dataView'
|
||||
}
|
||||
}
|
||||
|
||||
const $router = { push: sinon.stub() }
|
||||
|
||||
const readFile = sinon.stub(fu, 'readFile')
|
||||
const dataRes = new Response()
|
||||
dataRes.blob = sinon.stub().resolves({})
|
||||
readFile.onCall(0).returns(Promise.resolve(dataRes))
|
||||
|
||||
const inquiriesRes = new Response()
|
||||
inquiriesRes.json = sinon.stub().resolves({
|
||||
version: 2,
|
||||
inquiries: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }]
|
||||
})
|
||||
readFile.onCall(1).returns(Promise.resolve(inquiriesRes))
|
||||
const db = {
|
||||
loadDb: sinon.stub().resolves()
|
||||
}
|
||||
sinon.stub(database, 'getNewDatabase').returns(db)
|
||||
Tab.prototype.execute = sinon.stub()
|
||||
|
||||
const wrapper = mount(LoadView, {
|
||||
store,
|
||||
mocks: { $route, $router },
|
||||
stubs: ['router-link']
|
||||
})
|
||||
|
||||
await flushPromises()
|
||||
|
||||
// DB file is read
|
||||
expect(fu.readFile.firstCall.args[0]).to.equal('https://my-url/test.db')
|
||||
|
||||
// Db is loaded
|
||||
expect(db.loadDb.firstCall.args[0]).to.equal(await dataRes.blob.returnValues[0])
|
||||
|
||||
// Inquiries file is read
|
||||
expect(fu.readFile.secondCall.args[0])
|
||||
.to.equal('https://my-url/test_inquiries.json')
|
||||
|
||||
// Tab for inquiry is created
|
||||
expect(actions.addTab.calledOnce).to.equal(true)
|
||||
expect(actions.addTab.firstCall.args[1]).eql({
|
||||
id: undefined,
|
||||
name: 'foo',
|
||||
layout: {
|
||||
dataView: 'bottom',
|
||||
sqlEditor: 'hidden',
|
||||
table: 'above'
|
||||
},
|
||||
maximize: 'dataView'
|
||||
})
|
||||
const executedTab = Tab.prototype.execute.firstCall.thisValue
|
||||
expect(executedTab.tempName).to.equal('foo')
|
||||
expect(wrapper.find('#open-workspace-btn').exists()).to.equal(false)
|
||||
expect($router.push.called).to.equal(true)
|
||||
})
|
||||
|
||||
it('Doesn\'t redirect and show the button if there is an error', async () => {
|
||||
const state = {
|
||||
tabs: []
|
||||
}
|
||||
const mutations = {
|
||||
setCurrentTabId: sinon.stub().callsFake(realMutations.setCurrentTabId),
|
||||
setDb: sinon.stub().callsFake(realMutations.setDb)
|
||||
}
|
||||
const actions = {
|
||||
addTab: sinon.stub().callsFake(realActions.addTab)
|
||||
}
|
||||
const store = new Vuex.Store({ state, mutations, actions })
|
||||
const $route = {
|
||||
path: '/workspace',
|
||||
query: {
|
||||
data_url: 'https://my-url/test.db',
|
||||
data_format: 'sqlite',
|
||||
inquiry_url: 'https://my-url/test_inquiries.json',
|
||||
inquiry_id: [1],
|
||||
maximize: 'dataView'
|
||||
}
|
||||
}
|
||||
|
||||
const $router = { push: sinon.stub() }
|
||||
|
||||
const readFile = sinon.stub(fu, 'readFile')
|
||||
const dataRes = new Response()
|
||||
dataRes.blob = sinon.stub().rejects(new Error('Something is wrong'))
|
||||
readFile.onCall(0).returns(Promise.resolve(dataRes))
|
||||
|
||||
const inquiriesRes = new Response()
|
||||
inquiriesRes.json = sinon.stub().resolves({
|
||||
version: 2,
|
||||
inquiries: [{ id: 1 }]
|
||||
})
|
||||
readFile.onCall(1).returns(Promise.resolve(inquiriesRes))
|
||||
sinon.stub(database, 'getNewDatabase').returns({
|
||||
loadDb: sinon.stub().resolves()
|
||||
})
|
||||
|
||||
const wrapper = mount(LoadView, {
|
||||
store,
|
||||
mocks: { $route, $router },
|
||||
stubs: ['router-link']
|
||||
})
|
||||
|
||||
await flushPromises()
|
||||
expect(wrapper.find('#open-workspace-btn').exists()).to.equal(true)
|
||||
expect($router.push.called).to.equal(false)
|
||||
expect(wrapper.find('#logs').text()).to.include('Something is wrong')
|
||||
})
|
||||
})
|
||||
@@ -19,7 +19,9 @@ describe('Inquiries.vue', () => {
|
||||
predefinedInquiries: []
|
||||
}
|
||||
const mutations = {
|
||||
updatePredefinedInquiries: sinon.stub()
|
||||
setPredefinedInquiriesLoaded: sinon.stub(),
|
||||
updatePredefinedInquiries: sinon.stub(),
|
||||
setLoadingPredefinedInquiries: sinon.stub()
|
||||
}
|
||||
const store = new Vuex.Store({ state, mutations })
|
||||
const wrapper = shallowMount(Inquiries, { store })
|
||||
@@ -327,6 +329,7 @@ describe('Inquiries.vue', () => {
|
||||
sinon.stub(storedInquiries, 'getStoredInquiries').returns([inquiryInStorage])
|
||||
|
||||
const state = {
|
||||
tabs: [],
|
||||
predefinedInquiries: []
|
||||
}
|
||||
const actions = { addTab: sinon.stub().resolves(1) }
|
||||
|
||||
@@ -406,4 +406,119 @@ describe('Tab.vue', () => {
|
||||
expect(wrapper.find('.above .sql-editor-panel').exists()).to.equal(true)
|
||||
expect(wrapper.find('.bottomPane .run-result-panel').exists()).to.equal(true)
|
||||
})
|
||||
|
||||
it('Maximize top panel if maximized panel is above', () => {
|
||||
const state = {
|
||||
currentTabId: 1
|
||||
}
|
||||
const store = new Vuex.Store({ state, mutations })
|
||||
const tab = {
|
||||
id: 1,
|
||||
name: 'foo',
|
||||
query: 'SELECT * FROM foo; CREATE TABLE bar(a,b);',
|
||||
viewType: 'chart',
|
||||
viewOptions: {},
|
||||
layout: {
|
||||
sqlEditor: 'above',
|
||||
table: 'bottom',
|
||||
dataView: 'hidden'
|
||||
},
|
||||
maximize: 'sqlEditor',
|
||||
isPredefined: false,
|
||||
result: null,
|
||||
isGettingResults: false,
|
||||
error: null,
|
||||
time: 0,
|
||||
isSaved: true
|
||||
}
|
||||
|
||||
const wrapper = mount(Tab, {
|
||||
attachTo: place,
|
||||
store,
|
||||
stubs: ['chart'],
|
||||
propsData: {
|
||||
tab
|
||||
}
|
||||
})
|
||||
|
||||
expect(wrapper.find('.above').element.parentElement.style.height)
|
||||
.to.equal('100%')
|
||||
})
|
||||
|
||||
it('Maximize bottom panel if maximized panel is below', () => {
|
||||
const state = {
|
||||
currentTabId: 1
|
||||
}
|
||||
const store = new Vuex.Store({ state, mutations })
|
||||
const tab = {
|
||||
id: 1,
|
||||
name: 'foo',
|
||||
query: 'SELECT * FROM foo; CREATE TABLE bar(a,b);',
|
||||
viewType: 'chart',
|
||||
viewOptions: {},
|
||||
layout: {
|
||||
sqlEditor: 'above',
|
||||
table: 'bottom',
|
||||
dataView: 'hidden'
|
||||
},
|
||||
maximize: 'table',
|
||||
isPredefined: false,
|
||||
result: null,
|
||||
isGettingResults: false,
|
||||
error: null,
|
||||
time: 0,
|
||||
isSaved: true
|
||||
}
|
||||
|
||||
const wrapper = mount(Tab, {
|
||||
attachTo: place,
|
||||
store,
|
||||
stubs: ['chart'],
|
||||
propsData: {
|
||||
tab
|
||||
}
|
||||
})
|
||||
|
||||
expect(wrapper.find('.bottomPane').element.parentElement.style.height)
|
||||
.to.equal('100%')
|
||||
})
|
||||
|
||||
it('Panel size is 50 is nothing to maximize', () => {
|
||||
const state = {
|
||||
currentTabId: 1
|
||||
}
|
||||
const store = new Vuex.Store({ state, mutations })
|
||||
const tab = {
|
||||
id: 1,
|
||||
name: 'foo',
|
||||
query: 'SELECT * FROM foo; CREATE TABLE bar(a,b);',
|
||||
viewType: 'chart',
|
||||
viewOptions: {},
|
||||
layout: {
|
||||
sqlEditor: 'above',
|
||||
table: 'bottom',
|
||||
dataView: 'hidden'
|
||||
},
|
||||
isPredefined: false,
|
||||
result: null,
|
||||
isGettingResults: false,
|
||||
error: null,
|
||||
time: 0,
|
||||
isSaved: true
|
||||
}
|
||||
|
||||
const wrapper = mount(Tab, {
|
||||
attachTo: place,
|
||||
store,
|
||||
stubs: ['chart'],
|
||||
propsData: {
|
||||
tab
|
||||
}
|
||||
})
|
||||
|
||||
expect(wrapper.find('.above').element.parentElement.style.height)
|
||||
.to.equal('50%')
|
||||
expect(wrapper.find('.bottomPane').element.parentElement.style.height)
|
||||
.to.equal('50%')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -12,9 +12,11 @@ describe('Workspace.vue', () => {
|
||||
tabs: []
|
||||
}
|
||||
const store = new Vuex.Store({ state, actions, mutations })
|
||||
const $route = { path: '/workspace', query: {} }
|
||||
mount(Workspace, {
|
||||
store,
|
||||
stubs: ['router-link']
|
||||
stubs: ['router-link'],
|
||||
mocks: { $route }
|
||||
})
|
||||
|
||||
expect(state.tabs[0].query).to.include('Your database is empty.')
|
||||
@@ -24,4 +26,20 @@ describe('Workspace.vue', () => {
|
||||
expect(state.tabs[0].viewOptions).to.equal(undefined)
|
||||
expect(state.tabs[0].isSaved).to.equal(false)
|
||||
})
|
||||
|
||||
it('Collapse schema if hide_schema is 1', () => {
|
||||
const state = {
|
||||
db: {},
|
||||
tabs: []
|
||||
}
|
||||
const store = new Vuex.Store({ state, actions, mutations })
|
||||
const $route = { path: '/workspace', query: { hide_schema: '1' } }
|
||||
const vm = mount(Workspace, {
|
||||
store,
|
||||
stubs: ['router-link'],
|
||||
mocks: { $route }
|
||||
})
|
||||
|
||||
expect(vm.find('#schema-container').element.offsetWidth).to.equal(0)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user