Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Container improvements #20

Merged
merged 2 commits into from
Jun 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/container/__tests__/containerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('Container', () => {
});
});

describe('component lifestyle', () => {
describe('component lifecycle', () => {
var ParentComponent;
var componentWillReceiveProps;
var componentWillUpdate;
Expand Down Expand Up @@ -276,7 +276,7 @@ describe('Container', () => {
});
});

describe('when the parent updates its props then it should update its childrens', () => {
describe('when the parent updates its props then it should update its children\'s', () => {
var ParentComponent, fetch;

beforeEach(() => {
Expand Down Expand Up @@ -428,6 +428,12 @@ describe('Container', () => {
ContainerComponent = wrap(InnerComponent, {
something() {
return [this, 'foo'];
},

statics: {
somethingElse() {
return [this, 'bar'];
}
}
});

Expand All @@ -437,6 +443,11 @@ describe('Container', () => {
it('should expose the function with the element as the context', () => {
expect(element.something()).to.eql([element, 'foo']);
});

it('should expose the static function on the component class', () => {
expect(ContainerComponent.somethingElse())
.to.eql([ContainerComponent, 'bar']);
});
});

describe('when the component is bound to an application', () => {
Expand Down Expand Up @@ -670,6 +681,19 @@ describe('Container', () => {
});
});

describe('injectApp', () => {
it('should inject app as a property on the component', () => {
class Component extends React.Component {
render() {
return null;
}
}
Marty.injectApp(Component);

expect(render(Component).app).to.eql(app);
});
});

function withoutApp(props) {
return _.omit(props, 'app');
}
Expand Down
45 changes: 27 additions & 18 deletions src/container/createContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,21 @@ let RESERVED_FUNCTIONS = [
];

module.exports = function (React) {
let DEFAULT_CONTEXT_TYPES = {
app: React.PropTypes.object,
const DEFAULT_CONTEXT_TYPES = {
app: React.PropTypes.object
};

return function createContainer(InnerComponent, config) {
function injectApp(Component) {
Component.contextTypes = _.extend(
{},
DEFAULT_CONTEXT_TYPES,
Component.contextTypes
);

appProperty(Component.prototype);
}

function createContainer(InnerComponent, config) {
config = config || {};

if (!InnerComponent) {
Expand All @@ -37,13 +47,7 @@ module.exports = function (React) {
config.contextTypes
);

InnerComponent.contextTypes = _.extend(
{},
DEFAULT_CONTEXT_TYPES,
InnerComponent.contextTypes
);

appProperty(InnerComponent.prototype);
injectApp(InnerComponent);

let specification = _.extend({
contextTypes: contextTypes,
Expand Down Expand Up @@ -135,12 +139,15 @@ module.exports = function (React) {
specification.componentWillUnmount, config.componentWillUnmount
);

var Container = React.createClass(specification);

Container.InnerComponent = InnerComponent;
Container.displayName = innerComponentDisplayName + 'Container';

return Container;
const Container = React.createClass(specification);
return _.extend(
Container,
config.statics,
{
InnerComponent,
displayName: `${innerComponentDisplayName}Container`
}
);

function callBoth(func1, func2) {
if (_.isFunction(func2)) {
Expand All @@ -163,5 +170,7 @@ module.exports = function (React) {
return func1;
}
}
};
};
}

return {injectApp, createContainer};
};
6 changes: 4 additions & 2 deletions src/container/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const _ = require('../mindash');

module.exports = function (marty, React) {
marty.createContainer = require('./createContainer')(React);
};
_.extend(marty, require('./createContainer')(React));
};