From 896b8af1a23b7000b3cef8dfe63d8764223db2fa Mon Sep 17 00:00:00 2001 From: lana-k Date: Fri, 12 Feb 2021 21:39:47 +0100 Subject: [PATCH] add tests for chart module --- tests/unit/chart.spec.js | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 tests/unit/chart.spec.js diff --git a/tests/unit/chart.spec.js b/tests/unit/chart.spec.js new file mode 100644 index 0000000..20af83c --- /dev/null +++ b/tests/unit/chart.spec.js @@ -0,0 +1,65 @@ +import { expect } from 'chai' +import sinon from 'sinon' +import * as chart from '@/chart' +import * as dereference from 'react-chart-editor/lib/lib/dereference' + +describe('chart.js', () => { + it('getDataSourcesFromSqlResult', () => { + const sqlResult = { + columns: ['id', 'name'], + values: [ + [1, 'foo'], + [2, 'bar'] + ] + } + + const ds = chart.getDataSourcesFromSqlResult(sqlResult) + expect(ds).to.eql({ + id: [1, 2], + name: ['foo', 'bar'] + }) + }) + + it('getOptionsFromDataSources', () => { + const dataSources = { + id: [1, 2], + name: ['foo', 'bar'] + } + + const ds = chart.getOptionsFromDataSources(dataSources) + expect(ds).to.eql([ + { value: 'id', label: 'id' }, + { value: 'name', label: 'name' } + ]) + }) + + it('getChartStateForSave', () => { + const state = { + data: { + foo: {}, + bar: {} + }, + layout: {}, + frames: {} + } + const dataSources = { + id: [1, 2], + name: ['foo', 'bar'] + } + sinon.stub(dereference, 'default') + + const ds = chart.getChartStateForSave(state, dataSources) + + expect(dereference.default.calledOnce).to.equal(true) + + const args = dereference.default.firstCall.args + expect(args[0]).to.eql({ + foo: {}, + bar: {} + }) + expect(args[1]).to.eql({ + id: [], + name: [] + }) + }) +})