Skip to content

Commit d971b88

Browse files
Garret Meierhswolff
authored andcommitted
feat:Add props as second arg to mapModelsToProps
Implements #9 This is a nice feature for deriving which models to map into your state based on existing props. It can be especially useful for retrieving a model from a collection based on a value (as in the tests). Just a two-line change in the main file for lots of increased functionality.
1 parent ee33d2c commit d971b88

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

lib/connect-backbone-to-react.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ module.exports = function connectBackboneToReact(
7171

7272
this.setModels(props, context);
7373

74-
this.state = mapModelsToProps(this.models);
74+
this.state = mapModelsToProps(this.models, this.props);
7575

7676
this.createNewProps = this.createNewProps.bind(this);
7777

@@ -114,7 +114,7 @@ module.exports = function connectBackboneToReact(
114114
return;
115115
}
116116

117-
this.setState(mapModelsToProps(this.models));
117+
this.setState(mapModelsToProps(this.models, this.props));
118118
}
119119

120120
componentWillReceiveProps(nextProps, nextContext) {

test/connect-backbone-to-react.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,63 @@ describe('connectBackboneToReact', function() {
486486
});
487487
});
488488

489+
describe('when using props in mapModelsToProps', function() {
490+
function mapWithProps({ coll }, { name }) {
491+
if (!name) return {};
492+
const user = coll.findWhere({ name });
493+
return {
494+
name: user.get('name'),
495+
age: user.get('age'),
496+
hungry: user.get('hungry'),
497+
};
498+
}
499+
500+
let a;
501+
let b;
502+
let setStateSpy;
503+
beforeEach(function() {
504+
let ConnectedTest = connectBackboneToReact(mapWithProps)(TestComponent);
505+
setStateSpy = sandbox.spy(ConnectedTest.prototype, 'setState');
506+
a = new UserModel({
507+
name: 'A',
508+
age: '10',
509+
hungry: false,
510+
});
511+
b = new UserModel({
512+
name: 'B',
513+
age: '20',
514+
hungry: false,
515+
});
516+
517+
const models = {
518+
coll: new UserCollection([a, b]),
519+
};
520+
521+
wrapper = mount(<ConnectedTest models={models} name={'A'} />);
522+
stub = wrapper.find(TestComponent);
523+
});
524+
525+
afterEach(function() {
526+
wrapper.unmount();
527+
});
528+
529+
it('retrieves the correct model based on props', function() {
530+
assert.equal(stub.find('.name').text(), a.get('name'));
531+
assert.equal(stub.find('.age').text(), a.get('age'));
532+
533+
// Using props should not increase the number of times setState is called.
534+
assert.equal(setStateSpy.calledOnce, false);
535+
});
536+
537+
it('update the models based on new props', function() {
538+
wrapper.setProps({ name: 'B'});
539+
b.set('hungry', true);
540+
541+
assert.equal(stub.find('.name').text(), b.get('name'));
542+
assert.equal(stub.find('.age').text(), b.get('age'));
543+
});
544+
});
545+
489546
describe('when passed props change', function() {
490547
let setStateSpy;
491548
let newName;

0 commit comments

Comments
 (0)