Skip to content

Commit cef0a1f

Browse files
committed
feat: format data using state
1 parent acd5d25 commit cef0a1f

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/components/base_component.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const has_query = (instance_args) =>
4040
instance_args !== undefined && instance_args.hasOwnProperty('query')
4141

4242
const call_render_function = (args, instance_args, selection, element) => (data) =>
43-
args.render(instance_args, selection, data, element)
43+
args.render(instance_args, selection, format_data(instance_args, data), element)
4444

4545
const execute_query = (query, data) =>
4646
(callback) =>
@@ -106,6 +106,11 @@ const format_arguments = (args) => {
106106
return format_value(args, args.state_handler.get_state())
107107
}
108108

109+
const format_data = (args, data) => {
110+
if (!has_state_handler(args)) return data
111+
return format_value(data, args.state_handler.get_state())
112+
}
113+
109114
const create_component_function = (args) => (instance_args) => {
110115
execute_validations(args.validators)(format_arguments(instance_args))
111116
return create_bind_function(args, instance_args)

src/components/base_component.test.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ describe('Component', function() {
338338
'22': ['foo'],
339339
'state_handler': state_handler
340340
},
341-
'format_value_return2': {
341+
'format_value_return2': {
342342
'22': ['foo'],
343343
'state_handler': state_handler
344344
}
@@ -552,4 +552,37 @@ describe('Component', function() {
552552
my_init.should.be.calledOnce()
553553
})
554554

555+
it('calls format_value with proper data', () => {
556+
const state_handler = {
557+
'subscribe': sinon.spy(),
558+
'get_state': sinon.stub().returns({'x': '2'})}
559+
const format_value_return = {
560+
'state_handler': state_handler
561+
}
562+
const my_data = 'this is data that has to be formatted'
563+
const { format_value } = call_test_component_with({
564+
'instance_args': format_value_return,
565+
'format_value_return': format_value_return,
566+
'format_value_return2': format_value_return,
567+
'data': my_data
568+
})
569+
format_value.should.be.calledWith(my_data)
570+
})
571+
572+
it('calls render with formatted data', () => {
573+
const state_handler = {
574+
'subscribe': sinon.spy(),
575+
'get_state': sinon.stub().returns({'x': '2', 'y': 'foo'})}
576+
const format_value_return = {
577+
'state_handler': state_handler
578+
}
579+
const { my_render } = call_test_component_with({
580+
'instance_args': format_value_return,
581+
'format_value_return': format_value_return,
582+
'format_value_return2': format_value_return,
583+
'data': 'this is not formatted'
584+
})
585+
my_render.should.be.calledWith(sinon.match.any, sinon.match.any, format_value_return)
586+
})
587+
555588
})

0 commit comments

Comments
 (0)