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

Update to only warn if child contains components #6

Merged
merged 7 commits into from
Nov 30, 2016
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![Deloitte Digital](https://raw.githubusercontent.com/DeloitteDigital/DDBreakpoints/master/docs/deloittedigital-logo-white.png)

# React Habitat ![Build Status](https://travis-ci.org/DeloitteDigitalAPAC/react-habitat.svg?branch=develop)
# React Habitat ![Build Status](https://travis-ci.org/DeloitteDigitalAPAC/react-habitat.svg?branch=master)

## React Habitat <3 Your CMS

Expand Down
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: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-habitat",
"version": "0.3.1-beta",
"version": "0.3.1",
"description": "A React DOM Bootstrapper designed to harmonise a hybrid application",
"main": "./lib/index.js",
"repository": {
Expand Down Expand Up @@ -63,6 +63,7 @@
},
"files": [
"LICENCE.md",
"CHANGELOG.md",
"index.d.ts",
"readme.md",
"readme-in-es5.md",
Expand Down
2 changes: 1 addition & 1 deletion readme-in-es5.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![Deloitte Digital](https://raw.githubusercontent.com/DeloitteDigital/DDBreakpoints/master/docs/deloittedigital-logo-white.png)

# React Habitat ![Build Status](https://travis-ci.org/DeloitteDigitalAPAC/react-habitat.svg?branch=develop)
# React Habitat ![Build Status](https://travis-ci.org/DeloitteDigitalAPAC/react-habitat.svg?branch=master)

## React Habitat <3 Your CMS

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