Skip to content

Commit f55a8ca

Browse files
committed
#121 tests
1 parent 87f9f9e commit f55a8ca

File tree

7 files changed

+421
-339
lines changed

7 files changed

+421
-339
lines changed

src/store/actions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Tab from '@/lib/tab'
2+
import { nanoid } from 'nanoid'
23

34
export default {
45
async addTab ({ state }, inquiry = {}) {

src/views/Main/Inquiries/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export default {
198198
},
199199
computed: {
200200
inquiries () {
201-
return this.$store.state.inquiries || []
201+
return this.$store.state.inquiries
202202
},
203203
predefinedInquiries () {
204204
return this.$store.state.predefinedInquiries.map(inquiry => {

tests/App.spec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { expect } from 'chai'
2+
import sinon from 'sinon'
3+
import { shallowMount } from '@vue/test-utils'
4+
import Vuex from 'vuex'
5+
import App from '@/App'
6+
import storedInquiries from '@/lib/storedInquiries'
7+
import mutations from '@/store/mutations'
8+
9+
describe('App.vue', () => {
10+
afterEach(() => {
11+
sinon.restore()
12+
})
13+
14+
it('Gets inquiries', () => {
15+
sinon.stub(storedInquiries, 'getStoredInquiries').returns([
16+
{id: 1}, {id: 2}, {id: 3}
17+
])
18+
const state = {
19+
predefinedInquiries: [],
20+
inquiries: []
21+
}
22+
const store = new Vuex.Store({ state, mutations })
23+
shallowMount(App, { store, stubs: ['router-view'] })
24+
25+
expect(state.inquiries).to.eql([{id: 1}, {id: 2}, {id: 3}])
26+
})
27+
28+
it('Updates inquiries when they change in store', async () => {
29+
sinon.stub(storedInquiries, 'getStoredInquiries').returns([
30+
{id: 1, name: 'foo'}, {id: 2, name: 'baz'}, {id: 3, name: 'bar'}
31+
])
32+
sinon.spy(storedInquiries, 'updateStorage')
33+
34+
35+
const state = {
36+
predefinedInquiries: [],
37+
inquiries: []
38+
}
39+
const store = new Vuex.Store({ state, mutations })
40+
const wrapper = shallowMount(App, { store, stubs: ['router-view'] })
41+
42+
store.state.inquiries.splice(0, 1, {id: 1, name: 'new foo name'})
43+
await wrapper.vm.$nextTick()
44+
45+
expect(storedInquiries.updateStorage.calledTwice).to.equal(true)
46+
47+
expect(storedInquiries.updateStorage.args[1][0]).to.eql([
48+
{id: 1, name: 'new foo name'}, {id: 2, name: 'baz'}, {id: 3, name: 'bar'}
49+
])
50+
})
51+
})

tests/lib/storedInquiries/storedInquiries.spec.js

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -342,87 +342,4 @@ describe('storedInquiries.js', () => {
342342
createdAt: '2020-11-03T14:17:49.524Z'
343343
}])
344344
})
345-
346-
it('save adds new inquiry in the storage', () => {
347-
const now = new Date()
348-
const nowPlusMinute = new Date(now.getTime() + 60 * 1000)
349-
const tab = {
350-
id: 1,
351-
query: 'select * from foo',
352-
viewType: 'chart',
353-
viewOptions: [],
354-
name: null,
355-
dataView: {
356-
getOptionsForSave () {
357-
return ['chart']
358-
}
359-
}
360-
361-
}
362-
const value = storedInquiries.save(tab, 'foo')
363-
expect(value.id).to.equal(tab.id)
364-
expect(value.name).to.equal('foo')
365-
expect(value.query).to.equal(tab.query)
366-
expect(value.viewOptions).to.eql(['chart'])
367-
expect(value).to.have.property('createdAt').which.within(now, nowPlusMinute)
368-
const inquiries = storedInquiries.getStoredInquiries()
369-
expect(JSON.stringify(inquiries)).to.equal(JSON.stringify([value]))
370-
})
371-
372-
it('save updates existing inquiry in the storage', () => {
373-
const tab = {
374-
id: 1,
375-
query: 'select * from foo',
376-
viewType: 'chart',
377-
viewOptions: [],
378-
name: null,
379-
dataView: {
380-
getOptionsForSave () {
381-
return ['chart']
382-
}
383-
}
384-
385-
}
386-
387-
const first = storedInquiries.save(tab, 'foo')
388-
389-
tab.name = 'foo'
390-
tab.query = 'select * from foo'
391-
storedInquiries.save(tab)
392-
const inquiries = storedInquiries.getStoredInquiries()
393-
const second = inquiries[0]
394-
expect(inquiries).has.lengthOf(1)
395-
expect(second.id).to.equal(first.id)
396-
expect(second.name).to.equal(first.name)
397-
expect(second.query).to.equal(tab.query)
398-
expect(second.viewOptions).to.eql(['chart'])
399-
expect(new Date(second.createdAt).getTime()).to.equal(first.createdAt.getTime())
400-
})
401-
402-
it("save adds a new inquiry with new id if it's based on predefined inquiry", () => {
403-
const now = new Date()
404-
const nowPlusMinute = new Date(now.getTime() + 60 * 1000)
405-
const tab = {
406-
id: 1,
407-
query: 'select * from foo',
408-
viewType: 'chart',
409-
viewOptions: [],
410-
name: 'foo predefined',
411-
dataView: {
412-
getOptionsForSave () {
413-
return ['chart']
414-
}
415-
},
416-
isPredefined: true
417-
}
418-
storedInquiries.save(tab, 'foo')
419-
420-
const inquiries = storedInquiries.getStoredInquiries()
421-
expect(inquiries).has.lengthOf(1)
422-
expect(inquiries[0]).to.have.property('id').which.not.equal(tab.id)
423-
expect(inquiries[0].name).to.equal('foo')
424-
expect(inquiries[0].query).to.equal(tab.query)
425-
expect(inquiries[0].viewOptions).to.eql(['chart'])
426-
expect(new Date(inquiries[0].createdAt)).to.be.within(now, nowPlusMinute)
427-
})
428345
})

tests/store/actions.spec.js

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const {
55
addTab,
66
addInquiry,
77
deleteInquiries,
8-
renameInquiry
8+
renameInquiry,
9+
saveInquiry
910
} = actions
1011

1112
describe('actions', () => {
@@ -132,4 +133,110 @@ describe('actions', () => {
132133
}
133134
})).to.equal(true)
134135
})
136+
137+
it('saveInquiry adds new inquiry in the storage', async () => {
138+
const now = new Date()
139+
const nowPlusMinute = new Date(now.getTime() + 60 * 1000)
140+
141+
const tab = {
142+
id: 1,
143+
query: 'select * from foo',
144+
viewType: 'chart',
145+
viewOptions: [],
146+
name: null,
147+
dataView: {
148+
getOptionsForSave () {
149+
return ['chart']
150+
}
151+
}
152+
}
153+
const state = {
154+
inquiries: [],
155+
tabs: [tab],
156+
}
157+
158+
const value = await saveInquiry({ state }, {
159+
inquiryTab: tab,
160+
newName: 'foo'
161+
})
162+
expect(value.id).to.equal(tab.id)
163+
expect(value.name).to.equal('foo')
164+
expect(value.query).to.equal(tab.query)
165+
expect(value.viewOptions).to.eql(['chart'])
166+
expect(value).to.have.property('createdAt').which.within(now, nowPlusMinute)
167+
expect(state.inquiries).to.eql([value])
168+
})
169+
170+
it('save updates existing inquiry in the storage', async () => {
171+
const tab = {
172+
id: 1,
173+
query: 'select * from foo',
174+
viewType: 'chart',
175+
viewOptions: [],
176+
name: null,
177+
dataView: {
178+
getOptionsForSave () {
179+
return ['chart']
180+
}
181+
}
182+
}
183+
184+
const state = {
185+
inquiries: [],
186+
tabs: [tab],
187+
}
188+
189+
const first = await saveInquiry({ state }, {
190+
inquiryTab: tab,
191+
newName: 'foo'
192+
})
193+
194+
tab.name = 'foo'
195+
tab.query = 'select * from foo'
196+
await saveInquiry({ state }, { inquiryTab: tab })
197+
const inquiries = state.inquiries
198+
const second = inquiries[0]
199+
expect(inquiries).has.lengthOf(1)
200+
expect(second.id).to.equal(first.id)
201+
expect(second.name).to.equal(first.name)
202+
expect(second.query).to.equal(tab.query)
203+
expect(second.viewOptions).to.eql(['chart'])
204+
expect(new Date(second.createdAt).getTime()).to.equal(first.createdAt.getTime())
205+
})
206+
207+
it("save adds a new inquiry with new id if it's based on predefined inquiry", async () => {
208+
const now = new Date()
209+
const nowPlusMinute = new Date(now.getTime() + 60 * 1000)
210+
const tab = {
211+
id: 1,
212+
query: 'select * from foo',
213+
viewType: 'chart',
214+
viewOptions: [],
215+
name: 'foo predefined',
216+
dataView: {
217+
getOptionsForSave () {
218+
return ['chart']
219+
}
220+
},
221+
isPredefined: true
222+
}
223+
224+
const state = {
225+
inquiries: [],
226+
tabs: [tab],
227+
}
228+
229+
await saveInquiry({ state }, {
230+
inquiryTab: tab,
231+
newName: 'foo'
232+
})
233+
234+
const inquiries = state.inquiries
235+
expect(inquiries).has.lengthOf(1)
236+
expect(inquiries[0]).to.have.property('id').which.not.equal(tab.id)
237+
expect(inquiries[0].name).to.equal('foo')
238+
expect(inquiries[0].query).to.equal(tab.query)
239+
expect(inquiries[0].viewOptions).to.eql(['chart'])
240+
expect(new Date(inquiries[0].createdAt)).to.be.within(now, nowPlusMinute)
241+
})
135242
})

0 commit comments

Comments
 (0)