From d45212733d571e29d2743c1c8391584e9b233a15 Mon Sep 17 00:00:00 2001 From: lana-k Date: Wed, 27 Jan 2021 21:00:22 +0100 Subject: [PATCH] storedQueries module is covered by tests --- src/components/Chart.vue | 2 +- src/components/Tab.vue | 4 +- src/storedQueries.js | 10 +-- tests/unit/store.spec.js | 6 +- tests/unit/storedQueries.spec.js | 109 +++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+), 13 deletions(-) diff --git a/src/components/Chart.vue b/src/components/Chart.vue index 8e8f793..d980a23 100644 --- a/src/components/Chart.vue +++ b/src/components/Chart.vue @@ -77,7 +77,7 @@ export default { this.state = { data, layout, frames } this.$emit('update') }, - getChartSatateForSave () { + getChartStateForSave () { // we don't need to save the data, only settings // so we modify state.data using dereference const stateCopy = JSON.parse(JSON.stringify(this.state)) diff --git a/src/components/Tab.vue b/src/components/Tab.vue index f30de25..7209a19 100644 --- a/src/components/Tab.vue +++ b/src/components/Tab.vue @@ -134,8 +134,8 @@ export default { const freeSpace = bottomPane.offsetHeight - 88 - 42 - 30 - 5 - 40 this.tableViewHeight = freeSpace - (freeSpace % 40) }, - getChartSatateForSave () { - return this.$refs.chart.getChartSatateForSave() + getChartStateForSave () { + return this.$refs.chart.getChartStateForSave() } } } diff --git a/src/storedQueries.js b/src/storedQueries.js index 1def683..3af6fe4 100644 --- a/src/storedQueries.js +++ b/src/storedQueries.js @@ -25,17 +25,15 @@ export default { const value = { id: queryTab.isPredefined ? nanoid() : queryTab.id, query: queryTab.query, - chart: queryTab.getChartSatateForSave(), + chart: queryTab.getChartStateForSave(), name: newName || queryTab.initName } - const isNeedName = this.isTabNeedName(queryTab) - // Get queries from local storage let myQueries = this.getStoredQueries() // Set createdAt - if (isNeedName) { + if (newName) { value.createdAt = new Date() } else { var queryIndex = myQueries.findIndex(oldQuery => oldQuery.id === queryTab.id) @@ -43,9 +41,7 @@ export default { } // Insert in queries list - if (!myQueries) { - myQueries = [value] - } else if (isNeedName) { + if (newName) { myQueries.push(value) } else { myQueries[queryIndex] = value diff --git a/tests/unit/store.spec.js b/tests/unit/store.spec.js index c3e9fef..e6a76e3 100644 --- a/tests/unit/store.spec.js +++ b/tests/unit/store.spec.js @@ -376,7 +376,7 @@ describe('mutations', () => { }) describe('actions', () => { - it('addTab (new)', async () => { + it('addTab adds new blank tab', async () => { // mock state const state = { tabs: [], @@ -391,7 +391,7 @@ describe('actions', () => { expect(state.untitledLastIndex).to.equal(1) }) - it('addTab (saved)', async () => { + it('addTab adds tab from saved queries', async () => { // mock state const state = { tabs: [], @@ -410,7 +410,7 @@ describe('actions', () => { 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 = { id: 1, name: 'test', diff --git a/tests/unit/storedQueries.spec.js b/tests/unit/storedQueries.spec.js index 387d689..82d698f 100644 --- a/tests/unit/storedQueries.spec.js +++ b/tests/unit/storedQueries.spec.js @@ -1,5 +1,6 @@ import { expect } from 'chai' import storedQueries from '@/storedQueries.js' +import fu from '@/fileUtils' describe('storedQueries.js', () => { beforeEach(() => { @@ -142,4 +143,112 @@ describe('storedQueries.js', () => { expect(queries[0].chart).to.eql(parsedStr[0].chart) 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) + }) }) + + \ No newline at end of file