Skip to content

Commit

Permalink
feat: Add 'file_loader' option to json parser
Browse files Browse the repository at this point in the history
  • Loading branch information
kantord committed Mar 24, 2018
1 parent 57b000b commit c51c5a9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import create_state_handler from '../state_handler.js'
/** Creates a function that parses a JSON component and compiles it into a
* Javascript component
* @param {Function} component_loader - A function that can load components*/
const parse = (component_loader) => (input) => {
const parse = (component_loader) => (input, file_loader) => {
if (!(typeof input === 'object')) throw new Error('An object is required')
validators.required('component')(input)
validators.regexp('component', /^[A-z]\w*$/)(input)

const component = component_loader(input.component)
const args = Object.assign({}, input.args)
if (input.component === 'root')
if (input.component === 'root') {
args.state_handler = create_state_handler()
args.file_loader = file_loader
}
const bind = component(args)

return (selection) => {
Expand Down
19 changes: 18 additions & 1 deletion src/parser/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,24 @@ describe('Parser', function() {
component(my_selection)
fake_component.should.be.calledWith({
'title': '',
'state_handler': fake_state_handler
'state_handler': fake_state_handler,
'file_loader': sinon.match.any
})
})

it('passes file_loader to root', () => {
const fake_component = sinon.stub().returns(
sinon.stub().returns(sinon.spy()))
const fake_component_loader = sinon.stub().returns(fake_component)
const my_selection = null
const my_file_loader = sinon.spy()
const component = parse(fake_component_loader)(
{'component': 'root', 'args': {'title': ''}}, my_file_loader)
component(my_selection)
fake_component.should.be.calledWith({
'title': '',
'state_handler': sinon.match.any,
'file_loader': my_file_loader,
})
})

Expand Down

0 comments on commit c51c5a9

Please sign in to comment.