Skip to content

Commit 99576cf

Browse files
committed
feat: DropdownComponent reports variable values
through args 'init_variable' and 'set_variable'
1 parent e007015 commit 99576cf

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

src/components/dropdown/Dropdown.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Component from '../base_component.js'
22
import { required } from '../../validators'
33
import { regexp } from '../../validators'
4+
import * as d3 from 'd3'
45

56
const set_values = (selection) => selection
67
.property('value', d => d.value)
@@ -26,10 +27,20 @@ const DropdownComponent = Component({
2627
'validators': [
2728
required('variable'), regexp('variable', /^[A-Za-z]([_A-Za-z0-9-]*[_A-Za-z0-9])?$/),
2829
required('default')],
29-
'init': (args, selection) => selection
30-
.append('select').attr('class', 'ds--select'),
31-
'render': (args, selection, data, item) => item
32-
.selectAll('option').data(data).call(update_pattern)
30+
'init': (args, selection) => {
31+
args.init_variable(args.variable, args.default)
32+
return selection
33+
.append('select').attr('class', 'ds--select')
34+
},
35+
'render': (args, selection, data, item) => {
36+
item
37+
.selectAll('option').data(data).call(update_pattern)
38+
39+
item
40+
.on('change', function() {
41+
args.set_variable(args.variable, d3.select(this).property('value'))
42+
})
43+
}
3344
})
3445

3546
export default DropdownComponent

src/components/dropdown/Dropdown.test.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import should from 'should' // eslint-disable-line no-unused-vars
22
import DropdownComponent from './Dropdown'
33
import assert from 'assert'
4+
import sinon from 'sinon'
45

56
describe('Text component', function() {
67
beforeEach(function () {
@@ -23,9 +24,15 @@ describe('Text component', function() {
2324

2425
const call_render_with = (args) => {
2526
const d3 = require('d3')
26-
const render = DropdownComponent(args.args)(d3.selection())
27+
const init_variable = sinon.spy()
28+
const set_variable = sinon.spy()
29+
const component_args = Object.assign(args.args, {
30+
'init_variable': init_variable,
31+
'set_variable': set_variable,
32+
})
33+
const render = DropdownComponent(component_args)(d3.selection())
2734
render(args.data)
28-
return { render }
35+
return { render, init_variable, set_variable, d3 }
2936
}
3037

3138
it('creates select item', () => {
@@ -89,5 +96,48 @@ describe('Text component', function() {
8996
assert.equal(d3.select('option.ds--select-option').node().value, -56)
9097
})
9198

99+
it('init_variable has to be called with default value', () => {
100+
const my_spy = sinon.spy()
101+
const { init_variable } = call_render_with({'args': {'variable': 'my_var', 'default': my_spy},
102+
'data': [{ 'text': 'foo', 'value': 0 }]})
103+
init_variable.should.be.calledWith('my_var', my_spy)
104+
})
105+
106+
it('init_variable has to be called with default value 2', () => {
107+
const my_spy = sinon.spy()
108+
const { init_variable } = call_render_with({'args': {'variable': 'my_var2', 'default': my_spy},
109+
'data': [{ 'text': 'foo', 'value': 0 }]})
110+
init_variable.should.be.calledWith('my_var2', my_spy)
111+
})
112+
113+
it('set_variable should not be called before change', () => {
114+
const { set_variable } = call_render_with({'args': {'variable': 'my_var', 'default': ''},
115+
'data': [{ 'text': 'foo', 'value': 0 }]})
116+
set_variable.should.not.be.called()
117+
})
118+
119+
it('set_variable should be called after change', () => {
120+
const { d3, set_variable } = call_render_with({'args': {'variable': 'var', 'default': ''},
121+
'data': [
122+
{ 'text': 'foo', 'value': 'fo' },
123+
{ 'text': 'bar', 'value': 'bar' },
124+
]})
125+
d3.select('select').property('value', 'fo')
126+
d3.select('select').dispatch('change')
127+
set_variable.should.be.calledWith('var', 'fo')
128+
})
129+
130+
it('set_variable should be called after change 2', () => {
131+
const { d3, set_variable } = call_render_with({'args': {'variable': 'my_var', 'default': ''},
132+
'data': [
133+
{ 'text': 'foo', 'value': 'fo' },
134+
{ 'text': 'bar', 'value': 'baz' },
135+
]})
136+
d3.select('select').property('value', 'fo')
137+
d3.select('select').property('value', 'baz')
138+
d3.select('select').dispatch('change')
139+
set_variable.should.be.calledWith('my_var', 'baz')
140+
})
141+
92142
})
93143

0 commit comments

Comments
 (0)