Skip to content

Commit 34ae96b

Browse files
committed
fix: after-re init, render is now called with new element
1 parent 0bcfdc9 commit 34ae96b

File tree

5 files changed

+48
-12
lines changed

5 files changed

+48
-12
lines changed

src/components/base_component.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ const handle_external_data = (instance_args, selection, raw_data) => (resolve) =
7676

7777
const create_bind_function = (args, instance_args) => (selection) => {
7878
validate_selection(selection)
79-
const element = create_element(args.init, format_arguments(instance_args), selection)
79+
let element = create_element(args.init, format_arguments(instance_args), selection)
8080

8181
return (raw_data) => {
8282
const render_ = () => handle_external_data(instance_args, selection, raw_data)(
8383
render_component(args, format_arguments(instance_args), selection, element))
8484
if (has_state_handler(instance_args)) {
8585
instance_args.state_handler.subscribe((state_handler, me) => {
8686
element.remove()
87-
create_element(args.init, format_arguments(instance_args), selection)
87+
element = create_element(args.init, format_arguments(instance_args), selection)
8888
state_handler.subscribe(me)
8989
render_()
9090
})

src/components/base_component.test.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ describe('Component', function() {
2727
'../interpolation.js': {'format_value': format_value}
2828
}).default
2929

30-
const my_init = sinon.stub().returns(args.element)
30+
const my_init = sinon.stub()
31+
.onCall(1).returns(args.element2)
32+
.returns(args.element)
3133
const my_render = (args.render_func === undefined ) ? sinon.spy() : args.render_func
3234
const my_component = (args.has_init === true) ? Component({
3335
'render': my_render, 'validators': [], 'init': my_init
@@ -473,4 +475,30 @@ describe('Component', function() {
473475
my_render.should.be.calledTwice()
474476
})
475477

478+
it('correct element is passed to render when re-init happens', () => {
479+
let callback
480+
const element = {'remove': sinon.spy()}
481+
const element2 = {'remove': sinon.spy()}
482+
const state_handler = {
483+
'get_state': sinon.spy(),
484+
'subscribe': sinon.spy(function(f) {callback = f})}
485+
const { my_render } = call_test_component_with({
486+
'instance_args': {
487+
'${x}': '${y}',
488+
'state_handler': state_handler
489+
},
490+
'has_init': true,
491+
'element': element,
492+
'element2': element2,
493+
'format_value_return2': {
494+
'foo': 'bar',
495+
'state_handler': sinon.match.any
496+
}
497+
})
498+
state_handler.get_state = () => ({'x': 'foo', 'y': 'bar'})
499+
callback(state_handler, callback)
500+
my_render.should.be.calledWith(sinon.match.any, sinon.match.any, sinon.match.any, element2)
501+
})
502+
503+
476504
})

src/components/dropdown/Dropdown.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ const DropdownComponent = Component({
3333
.append('select').attr('class', 'ds--select')
3434
},
3535
'render': (args, selection, data, item) => {
36-
console.log("render", args, selection, data, item, args.state_handler.get_state()) // eslint-disable-line
3736
item
3837
.selectAll('option').data(data).call(update_pattern)
3938
item

src/components/dropdown/Dropdown.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('Text component', function() {
2727
const init_variable = sinon.spy()
2828
const set_variable = sinon.spy()
2929
const get_state = sinon.stub().returns(args.state || {})
30-
const subscribe = sinon.spy()
30+
const subscribe = (args.subscribe === undefined) ? sinon.spy() : args.subscribe
3131
const state_handler = { init_variable, set_variable, get_state, subscribe }
3232
const component_args = Object.assign(args.args, {
3333
state_handler
@@ -153,5 +153,21 @@ describe('Text component', function() {
153153
assert.equal(d3.select('select').node().value, '1')
154154
})
155155

156+
it('correct number of items after state change', () => {
157+
let callback
158+
const { d3, set_variable } = call_render_with({
159+
'subscribe': (f) => {callback = f},
160+
'args': {'variable': 'my_var', 'default': ''},
161+
'data': [
162+
{ 'text': 'foo', 'value': 'fo' },
163+
{ 'text': 'bar', 'value': 'baz' },
164+
]})
165+
assert.equal(d3.selectAll('option.ds--select-option').size(), 2, 'Correct number of items before change')
166+
d3.select('select').property('value', 'baz')
167+
d3.select('select').dispatch('change')
168+
callback({'subscribe': sinon.spy()}, callback)
169+
assert.equal(d3.selectAll('option.ds--select-option').size(), 2, 'Correct number of items after change')
170+
})
171+
156172
})
157173

src/manual_test.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@ dashboard "Cereals":
1616
- {"value": "bar", "text": "bar"}
1717
- {"value": "baz", "text": "baz"}
1818
- h2 text: "By calories"
19-
- bar chart:
20-
- attr:query: '{"columns": [(sort_by(-(.calories | tonumber)) | .[] | [.name, .calories])]}'
21-
- data: https://gist.githubusercontent.com/ZeningQu/6184eaf8faa533e320abc938c4738c3e/raw/40f237de825061faa8721c2293b79c46979780b4/cereals.csv
22-
- h2 text: "By nutritional profile"
23-
- 4 columns:
24-
- attr:query: '.[] | {"component": "rows", "data": [{"component": "text", "args": {"tagName": "h3"}, "data": .name}, {"component": "chart", "args": {"type": "pie"}, "data": {"columns": [["protein", .protein], ["carbo", .carbo], ["sugars", .sugars], ["fat", .fat]]}}]}'
25-
- data: https://gist.githubusercontent.com/ZeningQu/6184eaf8faa533e320abc938c4738c3e/raw/40f237de825061faa8721c2293b79c46979780b4/cereals.csv
2619
`)
2720

2821
//render_dashboard(

0 commit comments

Comments
 (0)