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

add tests for Chart.vue

This commit is contained in:
lana-k
2021-02-15 12:40:41 +01:00
parent f3beeb9f1c
commit 2974bc6d34
3 changed files with 79 additions and 8 deletions

View File

@@ -32,3 +32,9 @@ export function getChartStateForSave (state, dataSources) {
dereference(stateCopy.data, emptySources) dereference(stateCopy.data, emptySources)
return stateCopy return stateCopy
} }
export default {
getDataSourcesFromSqlResult,
getOptionsFromDataSources,
getChartStateForSave
}

View File

@@ -17,6 +17,7 @@
:debug="true" :debug="true"
:advancedTraceTypeSelector="true" :advancedTraceTypeSelector="true"
class="chart" class="chart"
ref="plotlyEditor"
:style="{ height: !sqlResult ? 'calc(100% - 40px)' : '100%' }" :style="{ height: !sqlResult ? 'calc(100% - 40px)' : '100%' }"
/> />
</div> </div>
@@ -27,11 +28,7 @@ import plotly from 'plotly.js/dist/plotly'
import 'react-chart-editor/lib/react-chart-editor.min.css' import 'react-chart-editor/lib/react-chart-editor.min.css'
import PlotlyEditor from 'react-chart-editor' import PlotlyEditor from 'react-chart-editor'
import { import chart from '@/chart'
getOptionsFromDataSources,
getDataSourcesFromSqlResult,
getChartStateForSave
} from '@/chart'
import dereference from 'react-chart-editor/lib/lib/dereference' import dereference from 'react-chart-editor/lib/lib/dereference'
export default { export default {
@@ -52,10 +49,10 @@ export default {
}, },
computed: { computed: {
dataSources () { dataSources () {
return getDataSourcesFromSqlResult(this.sqlResult) return chart.getDataSourcesFromSqlResult(this.sqlResult)
}, },
dataSourceOptions () { dataSourceOptions () {
return getOptionsFromDataSources(this.dataSources) return chart.getOptionsFromDataSources(this.dataSources)
} }
}, },
watch: { watch: {
@@ -74,7 +71,7 @@ export default {
this.$emit('update') this.$emit('update')
}, },
getChartStateForSave () { getChartStateForSave () {
return getChartStateForSave(this.state, this.dataSources) return chart.getChartStateForSave(this.state, this.dataSources)
} }
} }
} }

View File

@@ -0,0 +1,68 @@
import { expect } from 'chai'
import sinon from 'sinon'
import { mount, shallowMount } from '@vue/test-utils'
import Chart from '@/components/Chart.vue'
import chart from '@/chart.js'
import * as dereference from 'react-chart-editor/lib/lib/dereference'
describe('Chart.vue', () => {
afterEach(() => {
sinon.restore()
})
it('getChartStateForSave called with proper arguments', () => {
// mount the component
const wrapper = shallowMount(Chart)
const vm = wrapper.vm
const stub = sinon.stub(chart, 'getChartStateForSave').returns('result')
const chartData = vm.getChartStateForSave()
expect(stub.calledOnceWith(vm.state, vm.dataSources)).to.equal(true)
expect(chartData).to.equal('result')
})
it('is not visible when visible is false', () => {
// mount the component
const wrapper = shallowMount(Chart, {
propsData: { visible: false }
})
expect(wrapper.find('.chart-container').isVisible()).to.equal(false)
})
it('is visible when visible is true', () => {
// mount the component
const wrapper = shallowMount(Chart, {
propsData: { visible: true }
})
expect(wrapper.find('.chart-container').isVisible()).to.equal(true)
})
it('emits update when plotly updates', async () => {
// mount the component
const wrapper = mount(Chart)
wrapper.findComponent({ ref: 'plotlyEditor' }).vm.$emit('onUpdate')
expect(wrapper.emitted('update')).to.have.lengthOf(1)
})
it('calls dereference when sqlResult is changed', async () => {
sinon.stub(dereference, 'default')
const sqlResult = {
columns: ['id', 'name'],
values: [[1, 'foo']]
}
// mount the component
const wrapper = shallowMount(Chart, {
propsData: { sqlResult: sqlResult }
})
const newSqlResult = {
columns: ['id', 'name'],
values: [[2, 'bar']]
}
await wrapper.setProps({sqlResult: newSqlResult})
expect(dereference.default.called).to.equal(true)
})
})