Skip to content

Commit 332ead3

Browse files
committed
fix: select correct dropdown option
1 parent dc57dbc commit 332ead3

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/components/dropdown/Dropdown.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ const DropdownComponent = Component({
3535
'render': (args, selection, data, item) => {
3636
item
3737
.selectAll('option').data(data).call(update_pattern)
38-
38+
item
39+
.property('value', args.state_handler.get_state()[args.variable])
3940
item
4041
.on('change', function() {
4142
args.state_handler.set_variable(args.variable, d3.select(this).property('value'))

src/components/dropdown/Dropdown.test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ describe('Text component', function() {
2626
const d3 = require('d3')
2727
const init_variable = sinon.spy()
2828
const set_variable = sinon.spy()
29-
const state_handler = { init_variable, set_variable }
29+
const get_state = sinon.stub().returns(args.state || {})
30+
const subscribe = sinon.spy()
31+
const state_handler = { init_variable, set_variable, get_state, subscribe }
3032
const component_args = Object.assign(args.args, {
3133
state_handler
3234
})
@@ -139,5 +141,17 @@ describe('Text component', function() {
139141
set_variable.should.be.calledWith('my_var', 'baz')
140142
})
141143

144+
it('correct value should be selected based on state', () => {
145+
const d3 = require('d3')
146+
const { set_variable } = call_render_with({
147+
'args': {'variable': 'my_var', 'default': '0'},
148+
'state': {'my_var': 1},
149+
'data': [
150+
{ 'text': 'foo', 'value': '0' },
151+
{ 'text': 'bar', 'value': '1' },
152+
]})
153+
assert.equal(d3.select('select').node().value, '1')
154+
})
155+
142156
})
143157

0 commit comments

Comments
 (0)