Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #29 from whichdigital/child-components
Browse files Browse the repository at this point in the history
Modify React components to encapsulate child components
  • Loading branch information
katranci committed Aug 26, 2015
2 parents 284ea48 + 435092f commit fe7e7d1
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 2 deletions.
52 changes: 52 additions & 0 deletions spec/description_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'core-js/es5';
import React from 'react/addons';
import Description from '../src/book/description.jsx';

var ReactTestUtils = React.addons.TestUtils;

describe('Description', function() {
it('is a React element', function() {
let validation = ReactTestUtils.isElement(
<Description />
);

expect(validation).to.be.ok;
});

it('is a Description React element', function() {
let validation = ReactTestUtils.isElementOfType(
<Description />,
Description
);

expect(validation).to.be.ok;
});

it('is not a DOM component', function() {
let component = ReactTestUtils.renderIntoDocument(<Description />);
let validation = ReactTestUtils.isDOMComponent(component);

expect(validation).to.not.be.ok;
});

it('is a composite component', function() {
let component = ReactTestUtils.renderIntoDocument(<Description />);
let validation = ReactTestUtils.isCompositeComponent(component);

expect(validation).to.be.ok;
});

it('is a composite Description component', function() {
let component = ReactTestUtils.renderIntoDocument(<Description />);
let validation = ReactTestUtils.isCompositeComponentWithType(component, Description);

expect(validation).to.be.ok;
});

it('renders a <p> tag', function() {
let component = ReactTestUtils.renderIntoDocument(<Description />);
let descriptionElement = React.findDOMNode( component );

expect( descriptionElement.tagName ).to.equal( 'P' );
});
});
51 changes: 51 additions & 0 deletions spec/section_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'core-js/es5';
import React from 'react/addons';
import Section from '../src/book/section.jsx'
import Heading from '../src/book/heading.jsx'
import Description from '../src/book/description.jsx'

describe('Section', function() {
let sectionComponent;

before(function() {
let ReactTestUtils = React.addons.TestUtils,
shallowRenderer = ReactTestUtils.createRenderer();

shallowRenderer.render( <Section/> );
sectionComponent = shallowRenderer.getRenderOutput();
});

it('is wrapped in <section> element',function() {
expect( sectionComponent.type ).to.equal( 'section' );
});

describe('checks if children exist regardless of their order',function() {
let children;

before(function() {
children = sectionComponent.props.children;
});

it('includes Heading component',function() {
let childHeading = children.filter(
component => component.type === Heading
);
expect( childHeading.length ).to.be.ok;
});

it('includes Description component',function() {
let childDescription = children.filter(
component => component.type === Description
);
expect( childDescription.length ).to.be.ok;
});
});

// An alternative to the two tests above
it('includes Heading and Description components in particular order',function() {
expect( sectionComponent.props.children ).to.eql( [
<Heading />,
<Description />
] );
});
});
4 changes: 2 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import './navigation/links.scss';
import React from 'react';
import Book from './book/book.js';

import Heading from './book/heading.jsx'; // eslint-disable-line no-unused-vars
React.render( <Heading />, document.getElementById( 'heading-container' ) );
import Section from './book/section.jsx'; // eslint-disable-line no-unused-vars
React.render( <Section />, document.getElementById( 'heading-container' ) );

var book = new Book();
book.logSomething();
9 changes: 9 additions & 0 deletions src/book/description.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';

export default class extends React.Component {
render() {
return (
<p>This is the description!</p>
);
}
}
14 changes: 14 additions & 0 deletions src/book/section.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import Heading from './heading.jsx';
import Description from './description.jsx';

export default class extends React.Component {
render() {
return (
<section>
<Heading />
<Description />
</section>
)
}
}

0 comments on commit fe7e7d1

Please sign in to comment.