Skip to content

Commit

Permalink
feat(Injector): The inject property for Injectors can now resolve to …
Browse files Browse the repository at this point in the history
…null/undefined.

The inject property for Injectors can now also resolve to a null or undefined.  This ends up in
nothing being injected into the Injectable target.  This allows you to wrap some logic within an
inject function to perhaps hide or show content for example.
  • Loading branch information
ctrlplusb committed Apr 6, 2016
1 parent 8624f2b commit 7085201
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/Injectable.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ const Injectable = (WrappedComponent) => {
}

render() {
const keyed = keyedElements(`injected`, this.state.injected);

return (
<WrappedComponent
injected={keyedElements(`injected`, this.state.injected)}
injected={keyed}
{...this.props}
/>
);
Expand Down
3 changes: 2 additions & 1 deletion src/Provider.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Children, Component, PropTypes } from 'react';
import { compose, find, map, uniqBy, without, withoutAll } from './utils';
import { compose, find, filter, map, uniqBy, without, withoutAll } from './utils';

class InjectablesProvider extends Component {
static childContextTypes = {
Expand Down Expand Up @@ -55,6 +55,7 @@ class InjectablesProvider extends Component {
const { injectables, injections } = registration;

const elements = compose(
filter(x => x !== null && x !== undefined),
map(x => x.injector.getInjectElement()),
uniqBy(`injectorId`)
)(injections);
Expand Down
28 changes: 25 additions & 3 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { expect } from 'chai';
import $ from 'teaspoon';

describeWithDOM(`Given an Injectables configuration`, () => {
const { Provider, Injectable, Injector } = require(`../src/index.js`);

let InjectableHeader;
let Layout;
let HeaderInjectingSectionOne;
let HeaderInjectingSectionTwo;
let render;

before(() => {
const { Provider, Injectable, Injector } = require(`../src/index.js`);

render = elements => $(
<Provider>
{elements}
Expand All @@ -21,7 +21,6 @@ describeWithDOM(`Given an Injectables configuration`, () => {

const Header = ({ injected }) => (
<div id="Header">
<span>These are the injections:</span>
{injected}
</div>
);
Expand Down Expand Up @@ -171,4 +170,27 @@ describeWithDOM(`Given an Injectables configuration`, () => {
expect(actual).to.equal(expected, `The injected content was not found.`);
});
});

describe(`When rendering a null/undefined from an Injector`, () => {
it(`Then nothing should be rendered`, () => {
const NullInjector = Injector({
to: InjectableHeader,
inject: () => null
})(() => <noscript />);

const rendered = render(
<Layout>
<NullInjector />
</Layout>
);

const actual = rendered
.find($.s`${InjectableHeader}`)
.find(`div[id=Header]`)
.children()
.length;

expect(actual).to.equal(0);
});
});
});

0 comments on commit 7085201

Please sign in to comment.