Skip to content

Commit

Permalink
#2 Initial TODO List
Browse files Browse the repository at this point in the history
- extracted plugin to separate module
- use index as composition root for library
  • Loading branch information
kuzya-zz committed Jul 22, 2017
1 parent bc2bfa7 commit 9808b79
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 41 deletions.
39 changes: 15 additions & 24 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
import { parse, View, Warn } from 'vega'
import { compile } from 'vega-lite'
import createVegaLiteMixin from 'src/mixin/createVegaLiteMixin'
import vueExtendProxy from 'src/util/vueExtendProxy'
import vueOptionSpec from 'src/util/vueOptionSpec'
import VegaLitePlugin from 'src/plugin/VegaLitePlugin'

const VueVegaPlugin = {
install (Vue) {
const vegaLiteMixin = createVegaLiteMixin({
compile: compile,
parse: parse,
View: View,
logLevel: Warn,
vueOptionSpec: vueOptionSpec
})
const mixin = createVegaLiteMixin({
compile: compile,
parse: parse,
View: View,
logLevel: Warn,
vueOptionSpec: vueOptionSpec
})

Vue.mixin(vegaLiteMixin)
const vegaLitePlugin = new VegaLitePlugin({
mixin,
vueExtendProxy,
vueOptionSpec
})

let originalExtend = Vue.extend
Vue.extend = function (options) {
if (vueOptionSpec.isVegaLite(options)) {
if (!options.template && !options.el) {
options.template = '<div></div>'
}
}

return originalExtend.apply(this, arguments)
}
}
}

export default VueVegaPlugin
export default vegaLitePlugin
8 changes: 6 additions & 2 deletions src/plugin/VegaLitePlugin.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
export default class VegaLitePlugin {
constructor ({mixin, vueExtendProxy}) {
constructor ({mixin, vueExtendProxy, vueOptionSpec}) {
this.mixin = mixin
this.vueExtendProxy = vueExtendProxy
this.vueOptionSpec = vueOptionSpec
}

install (Vue) {
Vue.mixin(this.mixin)

Vue.extend = this.vueExtendProxy(Vue.extend)
Vue.extend = this.vueExtendProxy({
extendFn: Vue.extend,
vueOptionSpec: this.vueOptionSpec
})
}
}
23 changes: 16 additions & 7 deletions src/util/vueOptionSpec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
export default {
isVegaLite (options) {
const isDataAvailable = Boolean(options.data)
const isMarkAvailable = Boolean(options.mark)
const isEncodingAvailable = Boolean(options.encoding)
function isVegaLite (options) {
const isDataAvailable = Boolean(options.data)
const isMarkAvailable = Boolean(options.mark)
const isEncodingAvailable = Boolean(options.encoding)

return isDataAvailable && isMarkAvailable && isEncodingAvailable
}

function isTemplateRequired (options) {
const isPossibleToRenderComponent = options.el || options.template

return isDataAvailable && isMarkAvailable && isEncodingAvailable
}
return isVegaLite(options) && !isPossibleToRenderComponent
}

export default {
isVegaLite,
isTemplateRequired
}
19 changes: 13 additions & 6 deletions test/unit/specs/plugin/VegaLitePlugin.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import VegaLitePlugin from 'src/plugin/VegaLitePlugin';

describe('VegaLitePlugin', () => {
const sandbox = sinon.sandbox.create()
let vegaLitePlugin
let vegaLiteMixin
let vueExtendProxy
let Vue
let proxiedExtend
let originalExtend
let vueOptionSpec
const sandbox = sinon.sandbox.create()

beforeEach(() => {
vegaLiteMixin = sandbox.stub()
Expand All @@ -20,13 +21,16 @@ describe('VegaLitePlugin', () => {
extend: originalExtend
}

vueExtendProxy
.withArgs(Vue.extend)
.returns(proxiedExtend)
vueOptionSpec = {
isTemplateRequired: sandbox.stub()
}

vueExtendProxy.returns(proxiedExtend)

vegaLitePlugin = new VegaLitePlugin({
mixin: vegaLiteMixin,
vueExtendProxy: vueExtendProxy
vueExtendProxy: vueExtendProxy,
vueOptionSpec: vueOptionSpec
})
})

Expand All @@ -44,7 +48,10 @@ describe('VegaLitePlugin', () => {
it('should call hook with original Vue extend', () => {
vegaLitePlugin.install(Vue)

expect(vueExtendProxy).to.have.been.calledWith(originalExtend);
expect(vueExtendProxy).to.have.been.calledWith({
extendFn: originalExtend,
vueOptionSpec: vueOptionSpec
});
})

it('should replace original extend with proxied version', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import vueOptionSpec from 'src/util/vueOptionSpec'

const sandbox = sinon.sandbox.create()

describe('vueOptionSpec', () => {
let options
const sandbox = sinon.sandbox.create()

beforeEach(() => {
options = {
Expand Down Expand Up @@ -35,4 +34,32 @@ describe('vueOptionSpec', () => {
expect(vueOptionSpec.isVegaLite(options)).to.be.false
})
})

describe('isTemplateRequired', () => {
beforeEach(() => {
options = Object.assign({mark: 'blabla', encoding: {}}, options)
})

it('should be true if template or el doesn`t present in options', () => {
expect(vueOptionSpec.isTemplateRequired(options)).to.be.true
})

it('should be false if template present in options', () => {
options.template = 'template'

expect(vueOptionSpec.isTemplateRequired(options)).to.be.false
})

it('should be false if el present in options', () => {
options.el = 'el'

expect(vueOptionSpec.isTemplateRequired(options)).to.be.false
})

it('should be false if it`s not a vega lite', () => {
options = {template: 'template'}

expect(vueOptionSpec.isTemplateRequired(options)).to.be.false
})
})
})

0 comments on commit 9808b79

Please sign in to comment.