From f55a8caa92bb90c930f154cc4b5ef3e03c8a8965 Mon Sep 17 00:00:00 2001 From: lana-k Date: Sun, 12 Jan 2025 21:42:17 +0100 Subject: [PATCH] #121 tests --- src/store/actions.js | 1 + src/views/Main/Inquiries/index.vue | 2 +- tests/App.spec.js | 51 +++ .../storedInquiries/storedInquiries.spec.js | 83 ---- tests/store/actions.spec.js | 109 ++++- tests/views/Main/Inquiries/Inquiries.spec.js | 396 +++++++++--------- tests/views/Main/MainMenu.spec.js | 118 ++++-- 7 files changed, 421 insertions(+), 339 deletions(-) create mode 100644 tests/App.spec.js diff --git a/src/store/actions.js b/src/store/actions.js index 7c65899..09e3979 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -1,4 +1,5 @@ import Tab from '@/lib/tab' +import { nanoid } from 'nanoid' export default { async addTab ({ state }, inquiry = {}) { diff --git a/src/views/Main/Inquiries/index.vue b/src/views/Main/Inquiries/index.vue index 0b174ed..b412c9b 100644 --- a/src/views/Main/Inquiries/index.vue +++ b/src/views/Main/Inquiries/index.vue @@ -198,7 +198,7 @@ export default { }, computed: { inquiries () { - return this.$store.state.inquiries || [] + return this.$store.state.inquiries }, predefinedInquiries () { return this.$store.state.predefinedInquiries.map(inquiry => { diff --git a/tests/App.spec.js b/tests/App.spec.js new file mode 100644 index 0000000..e466b37 --- /dev/null +++ b/tests/App.spec.js @@ -0,0 +1,51 @@ +import { expect } from 'chai' +import sinon from 'sinon' +import { shallowMount } from '@vue/test-utils' +import Vuex from 'vuex' +import App from '@/App' +import storedInquiries from '@/lib/storedInquiries' +import mutations from '@/store/mutations' + +describe('App.vue', () => { + afterEach(() => { + sinon.restore() + }) + + it('Gets inquiries', () => { + sinon.stub(storedInquiries, 'getStoredInquiries').returns([ + {id: 1}, {id: 2}, {id: 3} + ]) + const state = { + predefinedInquiries: [], + inquiries: [] + } + const store = new Vuex.Store({ state, mutations }) + shallowMount(App, { store, stubs: ['router-view'] }) + + expect(state.inquiries).to.eql([{id: 1}, {id: 2}, {id: 3}]) + }) + + it('Updates inquiries when they change in store', async () => { + sinon.stub(storedInquiries, 'getStoredInquiries').returns([ + {id: 1, name: 'foo'}, {id: 2, name: 'baz'}, {id: 3, name: 'bar'} + ]) + sinon.spy(storedInquiries, 'updateStorage') + + + const state = { + predefinedInquiries: [], + inquiries: [] + } + const store = new Vuex.Store({ state, mutations }) + const wrapper = shallowMount(App, { store, stubs: ['router-view'] }) + + store.state.inquiries.splice(0, 1, {id: 1, name: 'new foo name'}) + await wrapper.vm.$nextTick() + + expect(storedInquiries.updateStorage.calledTwice).to.equal(true) + + expect(storedInquiries.updateStorage.args[1][0]).to.eql([ + {id: 1, name: 'new foo name'}, {id: 2, name: 'baz'}, {id: 3, name: 'bar'} + ]) + }) +}) diff --git a/tests/lib/storedInquiries/storedInquiries.spec.js b/tests/lib/storedInquiries/storedInquiries.spec.js index cc3f398..123ecc6 100644 --- a/tests/lib/storedInquiries/storedInquiries.spec.js +++ b/tests/lib/storedInquiries/storedInquiries.spec.js @@ -342,87 +342,4 @@ describe('storedInquiries.js', () => { createdAt: '2020-11-03T14:17:49.524Z' }]) }) - - it('save adds new inquiry in the storage', () => { - const now = new Date() - const nowPlusMinute = new Date(now.getTime() + 60 * 1000) - const tab = { - id: 1, - query: 'select * from foo', - viewType: 'chart', - viewOptions: [], - name: null, - dataView: { - getOptionsForSave () { - return ['chart'] - } - } - - } - const value = storedInquiries.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.viewOptions).to.eql(['chart']) - expect(value).to.have.property('createdAt').which.within(now, nowPlusMinute) - const inquiries = storedInquiries.getStoredInquiries() - expect(JSON.stringify(inquiries)).to.equal(JSON.stringify([value])) - }) - - it('save updates existing inquiry in the storage', () => { - const tab = { - id: 1, - query: 'select * from foo', - viewType: 'chart', - viewOptions: [], - name: null, - dataView: { - getOptionsForSave () { - return ['chart'] - } - } - - } - - const first = storedInquiries.save(tab, 'foo') - - tab.name = 'foo' - tab.query = 'select * from foo' - storedInquiries.save(tab) - const inquiries = storedInquiries.getStoredInquiries() - const second = inquiries[0] - expect(inquiries).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.viewOptions).to.eql(['chart']) - expect(new Date(second.createdAt).getTime()).to.equal(first.createdAt.getTime()) - }) - - it("save adds a new inquiry with new id if it's based on predefined inquiry", () => { - const now = new Date() - const nowPlusMinute = new Date(now.getTime() + 60 * 1000) - const tab = { - id: 1, - query: 'select * from foo', - viewType: 'chart', - viewOptions: [], - name: 'foo predefined', - dataView: { - getOptionsForSave () { - return ['chart'] - } - }, - isPredefined: true - } - storedInquiries.save(tab, 'foo') - - const inquiries = storedInquiries.getStoredInquiries() - expect(inquiries).has.lengthOf(1) - expect(inquiries[0]).to.have.property('id').which.not.equal(tab.id) - expect(inquiries[0].name).to.equal('foo') - expect(inquiries[0].query).to.equal(tab.query) - expect(inquiries[0].viewOptions).to.eql(['chart']) - expect(new Date(inquiries[0].createdAt)).to.be.within(now, nowPlusMinute) - }) }) diff --git a/tests/store/actions.spec.js b/tests/store/actions.spec.js index e18ade5..22bb8d1 100644 --- a/tests/store/actions.spec.js +++ b/tests/store/actions.spec.js @@ -5,7 +5,8 @@ const { addTab, addInquiry, deleteInquiries, - renameInquiry + renameInquiry, + saveInquiry } = actions describe('actions', () => { @@ -132,4 +133,110 @@ describe('actions', () => { } })).to.equal(true) }) + + it('saveInquiry adds new inquiry in the storage', async () => { + const now = new Date() + const nowPlusMinute = new Date(now.getTime() + 60 * 1000) + + const tab = { + id: 1, + query: 'select * from foo', + viewType: 'chart', + viewOptions: [], + name: null, + dataView: { + getOptionsForSave () { + return ['chart'] + } + } + } + const state = { + inquiries: [], + tabs: [tab], + } + + const value = await saveInquiry({ state }, { + inquiryTab: tab, + newName: 'foo' + }) + expect(value.id).to.equal(tab.id) + expect(value.name).to.equal('foo') + expect(value.query).to.equal(tab.query) + expect(value.viewOptions).to.eql(['chart']) + expect(value).to.have.property('createdAt').which.within(now, nowPlusMinute) + expect(state.inquiries).to.eql([value]) + }) + + it('save updates existing inquiry in the storage', async () => { + const tab = { + id: 1, + query: 'select * from foo', + viewType: 'chart', + viewOptions: [], + name: null, + dataView: { + getOptionsForSave () { + return ['chart'] + } + } + } + + const state = { + inquiries: [], + tabs: [tab], + } + + const first = await saveInquiry({ state }, { + inquiryTab: tab, + newName: 'foo' + }) + + tab.name = 'foo' + tab.query = 'select * from foo' + await saveInquiry({ state }, { inquiryTab: tab }) + const inquiries = state.inquiries + const second = inquiries[0] + expect(inquiries).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.viewOptions).to.eql(['chart']) + expect(new Date(second.createdAt).getTime()).to.equal(first.createdAt.getTime()) + }) + + it("save adds a new inquiry with new id if it's based on predefined inquiry", async () => { + const now = new Date() + const nowPlusMinute = new Date(now.getTime() + 60 * 1000) + const tab = { + id: 1, + query: 'select * from foo', + viewType: 'chart', + viewOptions: [], + name: 'foo predefined', + dataView: { + getOptionsForSave () { + return ['chart'] + } + }, + isPredefined: true + } + + const state = { + inquiries: [], + tabs: [tab], + } + + await saveInquiry({ state }, { + inquiryTab: tab, + newName: 'foo' + }) + + const inquiries = state.inquiries + expect(inquiries).has.lengthOf(1) + expect(inquiries[0]).to.have.property('id').which.not.equal(tab.id) + expect(inquiries[0].name).to.equal('foo') + expect(inquiries[0].query).to.equal(tab.query) + expect(inquiries[0].viewOptions).to.eql(['chart']) + expect(new Date(inquiries[0].createdAt)).to.be.within(now, nowPlusMinute) + }) }) diff --git a/tests/views/Main/Inquiries/Inquiries.spec.js b/tests/views/Main/Inquiries/Inquiries.spec.js index 570d350..8574260 100644 --- a/tests/views/Main/Inquiries/Inquiries.spec.js +++ b/tests/views/Main/Inquiries/Inquiries.spec.js @@ -5,6 +5,7 @@ import Vuex from 'vuex' import Inquiries from '@/views/Main/Inquiries' import storedInquiries from '@/lib/storedInquiries' import mutations from '@/store/mutations' +import actions from '@/store/actions' import fu from '@/lib/utils/fileIo' describe('Inquiries.vue', () => { @@ -14,16 +15,16 @@ describe('Inquiries.vue', () => { it('Shows start-guide message if there are no saved and predefined inquiries', () => { sinon.stub(storedInquiries, 'readPredefinedInquiries').resolves([]) - sinon.stub(storedInquiries, 'getStoredInquiries').returns([]) const state = { - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [] } const mutations = { setPredefinedInquiriesLoaded: sinon.stub(), updatePredefinedInquiries: sinon.stub(), setLoadingPredefinedInquiries: sinon.stub() } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = shallowMount(Inquiries, { store }) expect(wrapper.find('#start-guide').exists()).to.equal(true) @@ -40,32 +41,32 @@ describe('Inquiries.vue', () => { createdAt: '2020-03-08T19:57:56.299Z' } ]) - sinon.stub(storedInquiries, 'getStoredInquiries').returns([ - { - id: 1, - name: 'foo', - query: '', - viewType: 'chart', - viewOptions: [], - createdAt: '2020-11-03T19:57:56.299Z' - }, - { - id: 2, - name: 'bar', - query: '', - viewType: 'chart', - viewOptions: [], - createdAt: '2020-12-04T18:53:56.299Z' - } - ]) + const state = { - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [ + { + id: 1, + name: 'foo', + query: '', + viewType: 'chart', + viewOptions: [], + createdAt: '2020-11-03T19:57:56.299Z' + }, + { + id: 2, + name: 'bar', + query: '', + viewType: 'chart', + viewOptions: [], + createdAt: '2020-12-04T18:53:56.299Z' + } + ] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = shallowMount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() expect(wrapper.find('#start-guide').exists()).to.equal(false) @@ -94,29 +95,30 @@ describe('Inquiries.vue', () => { createdAt: '2020-03-08T19:57:56.299Z' } ]) - sinon.stub(storedInquiries, 'getStoredInquiries').returns([ - { - id: 1, - name: 'foo', - query: '', - viewType: 'chart', - viewOptions: [], - createdAt: '2020-11-03T19:57:56.299Z' - }, - { - id: 2, - name: 'bar', - query: '', - viewType: 'chart', - viewOptions: [], - createdAt: '2020-12-04T18:53:56.299Z' - } - ]) + const state = { - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [ + { + id: 1, + name: 'foo', + query: '', + viewType: 'chart', + viewOptions: [], + createdAt: '2020-11-03T19:57:56.299Z' + }, + { + id: 2, + name: 'bar', + query: '', + viewType: 'chart', + viewOptions: [], + createdAt: '2020-12-04T18:53:56.299Z' + } + ] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = mount(Inquiries, { store }) await wrapper.find('#toolbar-search input').setValue('OO') await wrapper.vm.$nextTick() @@ -138,29 +140,30 @@ describe('Inquiries.vue', () => { createdAt: '2020-03-08T19:57:56.299Z' } ]) - sinon.stub(storedInquiries, 'getStoredInquiries').returns([ - { - id: 1, - name: 'foo', - query: '', - viewType: 'chart', - viewOptions: [], - createdAt: '2020-11-03T19:57:56.299Z' - }, - { - id: 2, - name: 'bar', - query: '', - viewType: 'chart', - viewOptions: [], - createdAt: '2020-12-04T18:53:56.299Z' - } - ]) + const state = { - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [ + { + id: 1, + name: 'foo', + query: '', + viewType: 'chart', + viewOptions: [], + createdAt: '2020-11-03T19:57:56.299Z' + }, + { + id: 2, + name: 'bar', + query: '', + viewType: 'chart', + viewOptions: [], + createdAt: '2020-12-04T18:53:56.299Z' + } + ] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = mount(Inquiries, { store }) await wrapper.find('#toolbar-search input').setValue('baz') await wrapper.vm.$nextTick() @@ -181,24 +184,24 @@ describe('Inquiries.vue', () => { createdAt: '2020-03-08T19:57:56.299Z' } ]) - sinon.stub(storedInquiries, 'getStoredInquiries').returns([ - { - id: 1, - name: 'foo', - query: '', - viewType: 'chart', - viewOptions: [], - createdAt: '2020-11-03T19:57:56.299Z' - } - ]) + const state = { - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [ + { + id: 1, + name: 'foo', + query: '', + viewType: 'chart', + viewOptions: [], + createdAt: '2020-11-03T19:57:56.299Z' + } + ] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = shallowMount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() const rows = wrapper.findAll('tbody tr') @@ -208,26 +211,25 @@ describe('Inquiries.vue', () => { it('Exports one inquiry', async () => { sinon.stub(storedInquiries, 'readPredefinedInquiries').resolves([]) - sinon.stub(storedInquiries, 'getStoredInquiries').returns([ - { - id: 1, - name: 'foo', - query: '', - viewType: 'chart', - viewOptions: [], - createdAt: '2020-11-03T19:57:56.299Z' - } - ]) sinon.stub(storedInquiries, 'serialiseInquiries').returns('I am a serialized inquiry') sinon.stub(fu, 'exportToFile') const state = { - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [ + { + id: 1, + name: 'foo', + query: '', + viewType: 'chart', + viewOptions: [], + createdAt: '2020-11-03T19:57:56.299Z' + } + ] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = mount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() await wrapper.findComponent({ name: 'ExportIcon' }).find('svg').trigger('click') expect(fu.exportToFile.calledOnceWith('I am a serialized inquiry', 'foo.json')).to.equals(true) @@ -243,7 +245,6 @@ describe('Inquiries.vue', () => { viewOptions: [], createdAt: '2020-11-03T19:57:56.299Z' } - sinon.stub(storedInquiries, 'getStoredInquiries').returns([inquiryInStorage]) sinon.stub(storedInquiries, 'updateStorage') const newInquiry = { id: 2, @@ -255,13 +256,13 @@ describe('Inquiries.vue', () => { } sinon.stub(storedInquiries, 'duplicateInquiry').returns(newInquiry) const state = { - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [inquiryInStorage] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = mount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() await wrapper.findComponent({ name: 'CopyIcon' }).find('svg').trigger('click') @@ -271,9 +272,7 @@ describe('Inquiries.vue', () => { expect(rows).to.have.lengthOf(2) expect(rows.at(1).findAll('td').at(0).text()).to.equals('foo copy') expect(rows.at(1).findAll('td').at(1).text()).to.contains('3 December 2020 20:57') - expect( - storedInquiries.updateStorage.calledOnceWith(sinon.match([inquiryInStorage, newInquiry])) - ).to.equals(true) + expect(state.inquiries).to.eql([inquiryInStorage, newInquiry]) }) it('The copy of the inquiry is not selected if all inquiries were selected before duplication', @@ -287,8 +286,6 @@ describe('Inquiries.vue', () => { viewOptions: [], createdAt: '2020-11-03T19:57:56.299Z' } - sinon.stub(storedInquiries, 'getStoredInquiries').returns([inquiryInStorage]) - sinon.stub(storedInquiries, 'updateStorage') const newInquiry = { id: 2, name: 'foo copy', @@ -299,13 +296,13 @@ describe('Inquiries.vue', () => { } sinon.stub(storedInquiries, 'duplicateInquiry').returns(newInquiry) const state = { - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [inquiryInStorage] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = mount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() await wrapper.findComponent({ ref: 'mainCheckBox' }).find('.checkbox-container') .trigger('click') @@ -326,11 +323,11 @@ describe('Inquiries.vue', () => { viewOptions: [], createdAt: '2020-11-03T19:57:56.299Z' } - sinon.stub(storedInquiries, 'getStoredInquiries').returns([inquiryInStorage]) const state = { tabs: [], - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [inquiryInStorage] } const actions = { addTab: sinon.stub().resolves(1) } sinon.spy(mutations, 'setCurrentTabId') @@ -342,7 +339,6 @@ describe('Inquiries.vue', () => { mocks: { $router } }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() await wrapper.find('tbody tr').trigger('click') @@ -364,42 +360,40 @@ describe('Inquiries.vue', () => { createdAt: '2020-03-08T19:57:56.299Z' } ]) - sinon.stub(storedInquiries, 'getStoredInquiries').returns([]) const state = { - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = mount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() expect(wrapper.findComponent({ name: 'RenameIcon' }).exists()).to.equals(false) }) it('Renames an inquiry', async () => { sinon.stub(storedInquiries, 'readPredefinedInquiries').resolves([]) - sinon.stub(storedInquiries, 'getStoredInquiries').returns([ - { - id: 1, - name: 'foo', - query: '', - viewType: 'chart', - viewOptions: [], - createdAt: '2020-11-03T19:57:56.299Z' - } - ]) sinon.stub(storedInquiries, 'updateStorage') const state = { tabs: [{ id: 1, name: 'foo' }], - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [ + { + id: 1, + name: 'foo', + query: '', + viewType: 'chart', + viewOptions: [], + createdAt: '2020-11-03T19:57:56.299Z' + } + ] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = mount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() // click Rename icon in the grid @@ -419,19 +413,20 @@ describe('Inquiries.vue', () => { .findAll('.dialog-buttons-container button').wrappers .find(button => button.text() === 'Rename') .trigger('click') + await wrapper.vm.$nextTick() // check that the name in the grid is changed expect(wrapper.find('tbody tr td').text()).to.equals('bar') // check that storage is updated - expect(storedInquiries.updateStorage.calledOnceWith(sinon.match([{ + expect(state.inquiries).to.eql([{ id: 1, name: 'bar', query: '', viewType: 'chart', viewOptions: [], createdAt: '2020-11-03T19:57:56.299Z' - }]))).to.equals(true) + }]) // check that coresponding tab also changed the name expect(state.tabs[0].name).to.equals('bar') @@ -442,26 +437,25 @@ describe('Inquiries.vue', () => { it('Shows an error if try to rename to empty string', async () => { sinon.stub(storedInquiries, 'readPredefinedInquiries').resolves([]) - sinon.stub(storedInquiries, 'getStoredInquiries').returns([ - { - id: 1, - name: 'foo', - query: '', - viewType: 'chart', - viewOptions: [], - createdAt: '2020-11-03T19:57:56.299Z' - } - ]) sinon.stub(storedInquiries, 'updateStorage') const state = { tabs: [{ id: 1, name: 'foo' }], - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [ + { + id: 1, + name: 'foo', + query: '', + viewType: 'chart', + viewOptions: [], + createdAt: '2020-11-03T19:57:56.299Z' + } + ] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = mount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() // click Rename icon in the grid @@ -492,7 +486,6 @@ describe('Inquiries.vue', () => { viewOptions: [], createdAt: '2020-11-03T19:57:56.299Z' } - sinon.stub(storedInquiries, 'getStoredInquiries').returns([inquiryInStorage]) sinon.stub(storedInquiries, 'updateStorage') const importedInquiry = { id: 2, @@ -504,13 +497,13 @@ describe('Inquiries.vue', () => { } sinon.stub(storedInquiries, 'importInquiries').resolves([importedInquiry]) const state = { - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [inquiryInStorage] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = shallowMount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() // click Import @@ -520,9 +513,7 @@ describe('Inquiries.vue', () => { expect(rows).to.have.lengthOf(2) expect(rows.at(1).findAll('td').at(0).text()).to.equals('bar') expect(rows.at(1).findAll('td').at(1).text()).to.equals('3 December 2020 20:57') - expect(storedInquiries.updateStorage.calledOnceWith( - sinon.match([inquiryInStorage, importedInquiry]) - )).to.equals(true) + expect(state.inquiries).to.eql([inquiryInStorage, importedInquiry]) }) it('Imported inquiries are not selected if master check box was checked', async () => { @@ -535,7 +526,6 @@ describe('Inquiries.vue', () => { viewOptions: [], createdAt: '2020-11-03T19:57:56.299Z' } - sinon.stub(storedInquiries, 'getStoredInquiries').returns([inquiryInStorage]) sinon.stub(storedInquiries, 'updateStorage') const importedInquiry = { id: 2, @@ -547,13 +537,13 @@ describe('Inquiries.vue', () => { } sinon.stub(storedInquiries, 'importInquiries').resolves([importedInquiry]) const state = { - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [inquiryInStorage] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = mount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() // click on master checkbox @@ -580,16 +570,15 @@ describe('Inquiries.vue', () => { createdAt: '2020-03-08T19:57:56.299Z' } ]) - sinon.stub(storedInquiries, 'getStoredInquiries').returns([]) const state = { - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = mount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() expect(wrapper.findComponent({ name: 'DeleteIcon' }).exists()).to.equals(false) }) @@ -612,18 +601,17 @@ describe('Inquiries.vue', () => { viewOptions: [], createdAt: '2020-11-03T19:57:56.299Z' } - sinon.stub(storedInquiries, 'getStoredInquiries').returns([foo, bar]) sinon.stub(storedInquiries, 'updateStorage') const state = { tabs: [{ id: 1 }, { id: 2 }], - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [foo, bar] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = mount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() // click Delete icon in the first row of the grid await wrapper.findComponent({ name: 'DeleteIcon' }).find('svg').trigger('click') @@ -649,7 +637,7 @@ describe('Inquiries.vue', () => { expect(state.tabs[0].id).to.equals(2) // check that storage is updated - expect(storedInquiries.updateStorage.calledOnceWith(sinon.match([bar]))).to.equals(true) + expect(state.inquiries).to.eql([bar]) // check that delete dialog is closed expect(wrapper.find('[data-modal="delete"]').exists()).to.equal(false) @@ -666,25 +654,24 @@ describe('Inquiries.vue', () => { createdAt: '2020-03-08T19:57:56.299Z' } ]) - sinon.stub(storedInquiries, 'getStoredInquiries').returns([ - { - id: 1, - name: 'foo', - query: '', - viewType: 'chart', - viewOptions: [], - createdAt: '2020-11-03T19:57:56.299Z' - } - ]) const state = { - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [ + { + id: 1, + name: 'foo', + query: '', + viewType: 'chart', + viewOptions: [], + createdAt: '2020-11-03T19:57:56.299Z' + } + ] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = mount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() expect(wrapper.find('#toolbar-btns-export').isVisible()).to.equal(false) @@ -726,26 +713,25 @@ describe('Inquiries.vue', () => { viewOptions: [], createdAt: '2020-03-08T19:57:56.299Z' } - sinon.stub(storedInquiries, 'getStoredInquiries').returns([inquiryInStore, { - id: 2, - name: 'bar', - query: '', - viewType: 'chart', - viewOptions: [], - createdAt: '2020-03-08T19:57:56.299Z' - }]) sinon.stub(storedInquiries, 'serialiseInquiries').returns('I am a serialized inquiries') sinon.stub(fu, 'exportToFile') const state = { - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [inquiryInStore, { + id: 2, + name: 'bar', + query: '', + viewType: 'chart', + viewOptions: [], + createdAt: '2020-03-08T19:57:56.299Z' + }] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = mount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() const rows = wrapper.findAll('tbody tr') @@ -783,19 +769,18 @@ describe('Inquiries.vue', () => { viewOptions: [], createdAt: '2020-03-08T19:57:56.299Z' } - sinon.stub(storedInquiries, 'getStoredInquiries').returns([inquiryInStore]) sinon.stub(storedInquiries, 'serialiseInquiries').returns('I am a serialized inquiries') sinon.stub(fu, 'exportToFile') const state = { - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [inquiryInStore] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = mount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() await wrapper.findComponent({ ref: 'mainCheckBox' }).find('.checkbox-container') @@ -846,19 +831,18 @@ describe('Inquiries.vue', () => { viewOptions: [], createdAt: '2020-03-08T19:57:56.299Z' } - sinon.stub(storedInquiries, 'getStoredInquiries').returns([foo, bar, baz]) sinon.stub(storedInquiries, 'updateStorage') const state = { tabs: [{ id: 1 }, { id: 2 }, { id: 0 }, { id: 3 }], - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [foo, bar, baz] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = mount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() const rows = wrapper.findAll('tbody tr') @@ -893,7 +877,7 @@ describe('Inquiries.vue', () => { expect(state.tabs[1].id).to.equals(3) // check that storage is updated - expect(storedInquiries.updateStorage.calledOnceWith(sinon.match([baz]))).to.equals(true) + expect(state.inquiries).to.eql([baz]) // check that delete dialog is closed expect(wrapper.find('[data-modal="delete"]').exists()).to.equal(false) @@ -925,18 +909,17 @@ describe('Inquiries.vue', () => { viewOptions: [], createdAt: '2020-03-08T19:57:56.299Z' } - sinon.stub(storedInquiries, 'getStoredInquiries').returns([foo, bar]) sinon.stub(storedInquiries, 'updateStorage') const state = { tabs: [], - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [foo, bar] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = mount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() const rows = wrapper.findAll('tbody tr') @@ -968,7 +951,7 @@ describe('Inquiries.vue', () => { expect(wrapper.findAll('tbody tr').at(1).find('td').text()).to.equals('bar') // check that storage is updated - expect(storedInquiries.updateStorage.calledOnceWith(sinon.match([bar]))).to.equals(true) + expect(state.inquiries).to.eql([bar]) // check that delete dialog is closed expect(wrapper.find('[data-modal="delete"]').exists()).to.equal(false) @@ -1000,18 +983,17 @@ describe('Inquiries.vue', () => { viewOptions: [], createdAt: '2020-03-08T19:57:56.299Z' } - sinon.stub(storedInquiries, 'getStoredInquiries').returns([foo, bar]) sinon.stub(storedInquiries, 'updateStorage') const state = { tabs: [], - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [foo, bar] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = mount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() await wrapper.findComponent({ ref: 'mainCheckBox' }).find('.checkbox-container') @@ -1039,7 +1021,7 @@ describe('Inquiries.vue', () => { expect(wrapper.findAll('tbody tr').at(0).find('td').text()).to.contains('hello_world') // check that storage is updated - expect(storedInquiries.updateStorage.calledOnceWith(sinon.match([]))).to.equals(true) + expect(state.inquiries).to.eql([]) // check that delete dialog is closed expect(wrapper.find('[data-modal="delete"]').exists()).to.equal(false) @@ -1063,16 +1045,15 @@ describe('Inquiries.vue', () => { viewOptions: [], createdAt: '2020-03-08T19:57:56.299Z' } - sinon.stub(storedInquiries, 'getStoredInquiries').returns([foo, bar]) const state = { - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [foo, bar] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = mount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() const mainCheckBox = wrapper.findComponent({ ref: 'mainCheckBox' }) @@ -1122,16 +1103,15 @@ describe('Inquiries.vue', () => { viewOptions: [], createdAt: '2020-03-08T19:57:56.299Z' } - sinon.stub(storedInquiries, 'getStoredInquiries').returns([foo, bar]) const state = { - predefinedInquiries: [] + predefinedInquiries: [], + inquiries: [foo, bar] } - const store = new Vuex.Store({ state, mutations }) + const store = new Vuex.Store({ state, mutations, actions }) const wrapper = mount(Inquiries, { store }) await storedInquiries.readPredefinedInquiries.returnValues[0] - await storedInquiries.getStoredInquiries.returnValues[0] await wrapper.vm.$nextTick() const mainCheckBox = wrapper.findComponent({ ref: 'mainCheckBox' }) diff --git a/tests/views/Main/MainMenu.spec.js b/tests/views/Main/MainMenu.spec.js index a3f7448..9651ff3 100644 --- a/tests/views/Main/MainMenu.spec.js +++ b/tests/views/Main/MainMenu.spec.js @@ -349,16 +349,19 @@ describe('MainMenu.vue', () => { const mutations = { updateTab: sinon.stub() } - const store = new Vuex.Store({ state, mutations }) + const actions = { + saveInquiry: sinon.stub().returns({ + name: 'foo', + id: 1, + query: 'SELECT * FROM foo', + viewType: 'chart', + viewOptions: [] + }) + } + const store = new Vuex.Store({ state, mutations, actions }) const $route = { path: '/workspace' } sinon.stub(storedInquiries, 'isTabNeedName').returns(false) - sinon.stub(storedInquiries, 'save').returns({ - name: 'foo', - id: 1, - query: 'SELECT * FROM foo', - viewType: 'chart', - viewOptions: [] - }) + wrapper = mount(MainMenu, { store, @@ -371,8 +374,11 @@ describe('MainMenu.vue', () => { // check that the dialog is closed expect(wrapper.find('[data-modal="save"]').exists()).to.equal(false) - // check that the inquiry was saved via storedInquiries.save (newName='') - expect(storedInquiries.save.calledOnceWith(state.currentTab, '')).to.equal(true) + // check that the inquiry was saved via saveInquiry (newName='') + expect(actions.saveInquiry.calledOnce).to.equal(true) + expect(actions.saveInquiry.args[0][1]).to.eql({ + inquiryTab:state.currentTab, newName: '' + }) // check that the tab was updated expect(mutations.updateTab.calledOnceWith(state, sinon.match({ @@ -408,16 +414,18 @@ describe('MainMenu.vue', () => { const mutations = { updateTab: sinon.stub() } - const store = new Vuex.Store({ state, mutations }) + const actions = { + saveInquiry: sinon.stub().returns({ + name: 'foo', + id: 1, + query: 'SELECT * FROM foo', + viewType: 'chart', + viewOptions: [] + }) + } + const store = new Vuex.Store({ state, mutations, actions }) const $route = { path: '/workspace' } sinon.stub(storedInquiries, 'isTabNeedName').returns(true) - sinon.stub(storedInquiries, 'save').returns({ - name: 'foo', - id: 1, - query: 'SELECT * FROM foo', - viewType: 'chart', - viewOptions: [] - }) wrapper = mount(MainMenu, { store, @@ -458,16 +466,18 @@ describe('MainMenu.vue', () => { const mutations = { updateTab: sinon.stub() } - const store = new Vuex.Store({ state, mutations }) + const actions = { + saveInquiry: sinon.stub().returns({ + name: 'foo', + id: 1, + query: 'SELECT * FROM foo', + viewType: 'chart', + viewOptions: [] + }) + } + const store = new Vuex.Store({ state, mutations, actions }) const $route = { path: '/workspace' } sinon.stub(storedInquiries, 'isTabNeedName').returns(true) - sinon.stub(storedInquiries, 'save').returns({ - name: 'foo', - id: 1, - query: 'SELECT * FROM foo', - viewType: 'chart', - viewOptions: [] - }) wrapper = mount(MainMenu, { store, @@ -489,11 +499,17 @@ describe('MainMenu.vue', () => { .find(button => button.text() === 'Save') .trigger('click') + await wrapper.vm.$nextTick() + // check that the dialog is closed expect(wrapper.find('[data-modal="save"]').exists()).to.equal(false) - // check that the inquiry was saved via storedInquiries.save (newName='foo') - expect(storedInquiries.save.calledOnceWith(state.currentTab, 'foo')).to.equal(true) + // check that the inquiry was saved via saveInquiry (newName='foo') + expect(actions.saveInquiry.calledOnce).to.equal(true) + expect(actions.saveInquiry.args[0][1]).to.eql({ + inquiryTab:state.currentTab, + newName:'foo' + }) // check that the tab was updated expect(mutations.updateTab.calledOnceWith(state, sinon.match({ @@ -538,16 +554,18 @@ describe('MainMenu.vue', () => { const mutations = { updateTab: sinon.stub() } - const store = new Vuex.Store({ state, mutations }) + const actions = { + saveInquiry: sinon.stub().returns({ + name: 'bar', + id: 2, + query: 'SELECT * FROM foo', + viewType: 'chart', + viewOptions: [] + }) + } + const store = new Vuex.Store({ state, mutations, actions }) const $route = { path: '/workspace' } sinon.stub(storedInquiries, 'isTabNeedName').returns(true) - sinon.stub(storedInquiries, 'save').returns({ - name: 'bar', - id: 2, - query: 'SELECT * FROM foo', - viewType: 'chart', - viewOptions: [] - }) wrapper = mount(MainMenu, { store, @@ -572,11 +590,17 @@ describe('MainMenu.vue', () => { .find(button => button.text() === 'Save') .trigger('click') + await wrapper.vm.$nextTick() + // check that the dialog is closed expect(wrapper.find('[data-modal="save"]').exists()).to.equal(false) - // check that the inquiry was saved via storedInquiries.save (newName='bar') - expect(storedInquiries.save.calledOnceWith(state.currentTab, 'bar')).to.equal(true) + // check that the inquiry was saved via saveInquiry (newName='bar') + expect(actions.saveInquiry.calledOnce).to.equal(true) + expect(actions.saveInquiry.args[0][1]).to.eql({ + inquiryTab:state.currentTab, + newName: 'bar' + }) // check that the tab was updated expect(mutations.updateTab.calledOnceWith(state, sinon.match({ @@ -625,15 +649,17 @@ describe('MainMenu.vue', () => { const mutations = { updateTab: sinon.stub() } - const store = new Vuex.Store({ state, mutations }) + const actions = { + saveInquiry: sinon.stub().returns({ + name: 'bar', + id: 2, + query: 'SELECT * FROM foo', + chart: [] + }) + } + const store = new Vuex.Store({ state, mutations, actions }) const $route = { path: '/workspace' } sinon.stub(storedInquiries, 'isTabNeedName').returns(true) - sinon.stub(storedInquiries, 'save').returns({ - name: 'bar', - id: 2, - query: 'SELECT * FROM foo', - chart: [] - }) wrapper = mount(MainMenu, { store, @@ -656,7 +682,7 @@ describe('MainMenu.vue', () => { expect(wrapper.find('[data-modal="save"]').exists()).to.equal(false) // check that the inquiry was not saved via storedInquiries.save - expect(storedInquiries.save.called).to.equal(false) + expect(actions.saveInquiry.called).to.equal(false) // check that the tab was not updated expect(mutations.updateTab.called).to.equal(false)