Skip to content

Commit

Permalink
Update to only warn if child contains components
Browse files Browse the repository at this point in the history
- Remove logic warning of any children from Habitat.js
- Add logic to check if element contains any elements with an attribute
matching the custom selector to Bootstrapper.js and add new warning
for this case
- Update tests and build

Fixes #5
  • Loading branch information
6stringbeliever committed Nov 29, 2016
1 parent bae55ba commit 552aa0b
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 34 deletions.
8 changes: 3 additions & 5 deletions dist/react-habitat.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ return /******/ (function(modules) { // webpackBootstrap
var component = container.resolve(componentName);

if (component) {
if (ele.querySelector('[' + componentSelector + ']')) {
_Logger2.default.warn('RHW08', 'React Habitat should not contain additional components.', ele);
}
factory.inject(component, _Habitat2.default.parseProps(ele), _Habitat2.default.create(ele, id));
} else {
_Logger2.default.error('RHW01', 'Cannot resolve component "' + componentName + '" for element.', ele);
Expand Down Expand Up @@ -710,11 +713,6 @@ return /******/ (function(modules) { // webpackBootstrap
// Not an input so assumed we don't need to keep the target
// element around

// Check it is empty first (ignoring white space and line breaks)
if (ele.innerHTML.replace(/( |\r\n|\n|\r)/g, '') !== '') {
_Logger2.default.warn('RHW05', 'React Habitat element not empty.', ele);
}

if (!replaceDisabled) {
// Detach it
var host = ele.parentNode.removeChild(ele);
Expand Down
2 changes: 1 addition & 1 deletion dist/react-habitat.min.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions lib/Bootstrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ function parseContainer(container, elements, componentSelector) {
var component = container.resolve(componentName);

if (component) {
if (ele.querySelector('[' + componentSelector + ']')) {
_Logger2.default.warn('RHW08', 'React Habitat should not contain additional components.', ele);
}
factory.inject(component, _Habitat2.default.parseProps(ele), _Habitat2.default.create(ele, id));
} else {
_Logger2.default.error('RHW01', 'Cannot resolve component "' + componentName + '" for element.', ele);
Expand Down
5 changes: 0 additions & 5 deletions lib/Habitat.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,6 @@ var Habitat = function () {
// Not an input so assumed we don't need to keep the target
// element around

// Check it is empty first (ignoring white space and line breaks)
if (ele.innerHTML.replace(/( |\r\n|\n|\r)/g, '') !== '') {
_Logger2.default.warn('RHW05', 'React Habitat element not empty.', ele);
}

if (!replaceDisabled) {
// Detach it
var host = ele.parentNode.removeChild(ele);
Expand Down
3 changes: 3 additions & 0 deletions src/Bootstrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ function parseContainer(container, elements, componentSelector, cb = null) {
const component = container.resolve(componentName);

if (component) {
if (ele.querySelector(`[${componentSelector}]`)) {
Logger.warn('RHW08', 'React Habitat should not contain additional components.', ele);
}
factory.inject(
component,
Habitat.parseProps(ele),
Expand Down
6 changes: 0 additions & 6 deletions src/Habitat.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/
import Logger from './Logger';


const HABITAT_HOST_KEY = 'habitatHostElement';
const HABITAT_NAMESPACE = 'data-habitat';
const ACTIVE_HABITAT_FLAG = 'data-has-habitat';
Expand Down Expand Up @@ -174,11 +173,6 @@ export default class Habitat {
// Not an input so assumed we don't need to keep the target
// element around

// Check it is empty first (ignoring white space and line breaks)
if (ele.innerHTML.replace(/( |\r\n|\n|\r)/g,'') !== '') {
Logger.warn('RHW05', 'React Habitat element not empty.', ele);
}

if (!replaceDisabled) {
// Detach it
const host = ele.parentNode.removeChild(ele);
Expand Down
44 changes: 42 additions & 2 deletions tests/Bootstrapper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ describe('Bootstrapper', () => {
expect(component2Lookup.length).toEqual(1);
});

it('should not render to elements that have children', () => {
it('should warn when rendering to elements that have components as children', () => {
spyOn(console, 'warn');

const html = '<div data-component="IMochComponent"><p>Child</p></div>';
const html = '<div data-component="IMochComponent"><div data-component="IMochComponent"></div></div>';
node.innerHTML = html;

// -- MOCH CONTAINER SET UP -- //
Expand All @@ -134,6 +134,46 @@ describe('Bootstrapper', () => {

});

it('should not warn when rendering to elements that have children that are not components', () => {
spyOn(console, 'warn');

const html = '<div data-component="IMochComponent"><p>Hello world</p></div>';
node.innerHTML = html;

// -- MOCH CONTAINER SET UP -- //
const container = new Container();
container.register('IMochComponent', MochComponent);
// --------------------------- //

const app = new App(container);

expect(console.warn).not.toHaveBeenCalled();

});

it('should render to elements that have children that are not components', () => {
spyOn(console, 'warn');

const html = '<div data-component="IMochComponent"><p>Child</p></div>';
node.innerHTML = html;

// -- MOCH CONTAINER SET UP -- //
const container = new Container();
container.register('IMochComponent', MochComponent);
// --------------------------- //

const app = new App(container);

expect(console.warn).not.toHaveBeenCalled();

const componentLookup = node.innerHTML.match(/\[component MochComponent\]/g);

expect(app).toBeDefined();
expect(componentLookup).not.toEqual(null);
expect(componentLookup.length).toEqual(1);

});

it('should render to elements with white space and line breaks', () => {
node.innerHTML =
'<div data-component="IMochComponent"> \n \n</div>';
Expand Down
15 changes: 0 additions & 15 deletions tests/Habitat.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,21 +188,6 @@ describe('Habitat create', () => {

});

it('should throw non empty target error', () => {

spyOn(console, 'warn');

const testElement = window.document.createElement('div');
testElement.innerHTML = '<p>test</p>';

node.appendChild(testElement);

Habitat.create(testElement, 'C01');

expect(console.warn).toHaveBeenCalled();

});

it('should leave inputs in the dom', () => {

const testElement = window.document.createElement('input');
Expand Down

0 comments on commit 552aa0b

Please sign in to comment.