1
0
mirror of https://github.com/lana-k/sqliteviz.git synced 2025-12-07 02:28:54 +08:00

storedQueries module is covered by tests

This commit is contained in:
lana-k
2021-01-27 21:00:22 +01:00
parent f640727d96
commit d45212733d
5 changed files with 118 additions and 13 deletions

View File

@@ -77,7 +77,7 @@ export default {
this.state = { data, layout, frames } this.state = { data, layout, frames }
this.$emit('update') this.$emit('update')
}, },
getChartSatateForSave () { getChartStateForSave () {
// we don't need to save the data, only settings // we don't need to save the data, only settings
// so we modify state.data using dereference // so we modify state.data using dereference
const stateCopy = JSON.parse(JSON.stringify(this.state)) const stateCopy = JSON.parse(JSON.stringify(this.state))

View File

@@ -134,8 +134,8 @@ export default {
const freeSpace = bottomPane.offsetHeight - 88 - 42 - 30 - 5 - 40 const freeSpace = bottomPane.offsetHeight - 88 - 42 - 30 - 5 - 40
this.tableViewHeight = freeSpace - (freeSpace % 40) this.tableViewHeight = freeSpace - (freeSpace % 40)
}, },
getChartSatateForSave () { getChartStateForSave () {
return this.$refs.chart.getChartSatateForSave() return this.$refs.chart.getChartStateForSave()
} }
} }
} }

View File

@@ -25,17 +25,15 @@ export default {
const value = { const value = {
id: queryTab.isPredefined ? nanoid() : queryTab.id, id: queryTab.isPredefined ? nanoid() : queryTab.id,
query: queryTab.query, query: queryTab.query,
chart: queryTab.getChartSatateForSave(), chart: queryTab.getChartStateForSave(),
name: newName || queryTab.initName name: newName || queryTab.initName
} }
const isNeedName = this.isTabNeedName(queryTab)
// Get queries from local storage // Get queries from local storage
let myQueries = this.getStoredQueries() let myQueries = this.getStoredQueries()
// Set createdAt // Set createdAt
if (isNeedName) { if (newName) {
value.createdAt = new Date() value.createdAt = new Date()
} else { } else {
var queryIndex = myQueries.findIndex(oldQuery => oldQuery.id === queryTab.id) var queryIndex = myQueries.findIndex(oldQuery => oldQuery.id === queryTab.id)
@@ -43,9 +41,7 @@ export default {
} }
// Insert in queries list // Insert in queries list
if (!myQueries) { if (newName) {
myQueries = [value]
} else if (isNeedName) {
myQueries.push(value) myQueries.push(value)
} else { } else {
myQueries[queryIndex] = value myQueries[queryIndex] = value

View File

@@ -376,7 +376,7 @@ describe('mutations', () => {
}) })
describe('actions', () => { describe('actions', () => {
it('addTab (new)', async () => { it('addTab adds new blank tab', async () => {
// mock state // mock state
const state = { const state = {
tabs: [], tabs: [],
@@ -391,7 +391,7 @@ describe('actions', () => {
expect(state.untitledLastIndex).to.equal(1) expect(state.untitledLastIndex).to.equal(1)
}) })
it('addTab (saved)', async () => { it('addTab adds tab from saved queries', async () => {
// mock state // mock state
const state = { const state = {
tabs: [], tabs: [],
@@ -410,7 +410,7 @@ describe('actions', () => {
expect(state.untitledLastIndex).to.equal(0) expect(state.untitledLastIndex).to.equal(0)
}) })
it('addTab (existed)', async () => { it("addTab doesn't add anything when the query is already opened", async () => {
const tab1 = { const tab1 = {
id: 1, id: 1,
name: 'test', name: 'test',

View File

@@ -1,5 +1,6 @@
import { expect } from 'chai' import { expect } from 'chai'
import storedQueries from '@/storedQueries.js' import storedQueries from '@/storedQueries.js'
import fu from '@/fileUtils'
describe('storedQueries.js', () => { describe('storedQueries.js', () => {
beforeEach(() => { beforeEach(() => {
@@ -142,4 +143,112 @@ describe('storedQueries.js', () => {
expect(queries[0].chart).to.eql(parsedStr[0].chart) expect(queries[0].chart).to.eql(parsedStr[0].chart)
expect(queries[0].createdAt).to.equal(parsedStr[0].createdAt) expect(queries[0].createdAt).to.equal(parsedStr[0].createdAt)
}) })
it('importQueries', async () => {
const str = `
{
"id": 1,
"name": "foo",
"query": "select * from foo",
"chart": [],
"createdAt": "2020-11-03T14:17:49.524Z"
}
`
sinon.stub(fu, 'importFile').returns(Promise.resolve(str))
const queries = await storedQueries.importQueries()
fu.importFile.restore()
expect(queries).to.eql([JSON.parse(str)])
})
it('readPredefinedQueries', async () => {
const str = `
{
"id": 1,
"name": "foo",
"query": "select * from foo",
"chart": [],
"createdAt": "2020-11-03T14:17:49.524Z"
}
`
sinon.stub(fu, 'readFile').returns(Promise.resolve(new Response(str)))
const queries = await storedQueries.readPredefinedQueries()
expect(fu.readFile.calledOnceWith('./queries.json')).to.equal(true)
expect(queries).to.eql(JSON.parse(str))
fu.readFile.restore()
})
it('save adds new query in the storage', () => {
const now = new Date()
const nowPlusMinute = new Date(now.getTime() + 60 * 1000)
const tab = {
id: 1,
query: 'select * from foo',
chart: [],
initName: null,
getChartStateForSave () {
return []
}
}
const value = storedQueries.save(tab, 'foo')
expect(value.id).to.equal(tab.id)
expect(value.name).to.equal('foo')
expect(value.query).to.equal(tab.query)
expect(value.chart).to.eql(tab.getChartStateForSave())
expect(value).to.have.property('createdAt').which.within(now, nowPlusMinute)
const queries = storedQueries.getStoredQueries()
expect(JSON.stringify(queries)).to.equal(JSON.stringify([value]))
})
it('save updates existing query in the storage', () => {
const tab = {
id: 1,
query: 'select * from foo',
chart: [],
initName: null,
getChartStateForSave () {
return []
}
}
const first = storedQueries.save(tab, 'foo')
tab.initName = 'foo'
tab.query = 'select * from foo'
storedQueries.save(tab)
const queries = storedQueries.getStoredQueries()
const second = queries[0]
expect(queries).has.lengthOf(1)
expect(second.id).to.equal(first.id)
expect(second.name).to.equal(first.name)
expect(second.query).to.equal(tab.query)
expect(second.chart).to.eql(first.chart)
expect(new Date(second.createdAt).getTime()).to.equal(first.createdAt.getTime())
})
it("save adds a new query with new id if it's based on predefined query", () => {
const now = new Date()
const nowPlusMinute = new Date(now.getTime() + 60 * 1000)
const tab = {
id: 1,
query: 'select * from foo',
chart: [],
initName: 'foo predefined',
getChartStateForSave () {
return []
},
isPredefined: true
}
storedQueries.save(tab, 'foo')
const queries = storedQueries.getStoredQueries()
expect(queries).has.lengthOf(1)
expect(queries[0]).to.have.property('id').which.not.equal(tab.id)
expect(queries[0].name).to.equal('foo')
expect(queries[0].query).to.equal(tab.query)
expect(queries[0].chart).to.eql(tab.getChartStateForSave())
expect(new Date(queries[0].createdAt)).to.be.within(now, nowPlusMinute)
})
}) })