Skip to content

Instantly share code, notes, and snippets.

@yortuc
Created December 14, 2017 16:10
Show Gist options
  • Save yortuc/84258686bf13aad776907b9ffcdb7b9d to your computer and use it in GitHub Desktop.
Save yortuc/84258686bf13aad776907b9ffcdb7b9d to your computer and use it in GitHub Desktop.
const rr = ({type, children}={}) => {
if(!type)
return ''
if(!children)
return document.createElement(type);
var t = document.createTextNode(children[0]);
var n = document.createElement(type);
n.appendChild(t);
if(children.length === 2 && children[1].type==='img'){
var child = document.createElement(children[1].type );
child.src = children[1].src;
n.appendChild(child);
}
if(children.length === 2 && children[1].children){
let t = document.createTextNode(children[1].children[0]);
let n = document.createElement(type);
n.appendChild(child);
}
return n;
};
describe('realReact', function() {
it('renders empty string with no argument', function() {
assert(rr() === '');
});
it('renders a label', ()=> {
const renderedElement = rr({type: 'label' });
assert(renderedElement.tagName === 'LABEL');
});
it('renders a label with text', ()=> {
const elm = rr({ type: 'label', children: ['This is my first label'] });
assert.equal(elm.innerText, 'This is my first label');
})
it('renders label with text and a child image', ()=> {
const image = {type: 'img', src: 'https://a.b'};
const elm = rr({ type: 'label', children: ['This is my label', image] })
assert.equal(elm.innerText, 'This is my label');
assert.equal(elm.childNodes[1].src, 'https://a.b/');
})
it('renders label with text and a child label with an image', ()=> {
const image = {type: 'img', src: 'https://a.b'};
const childLabel = { type: 'label', children: ['This is my child label', image] }
const elm = { type: 'label', children: ['This is my label', childLabel] }
const domElm = rr(elm);
const renderedChildLabel = domElm.childNodes[1];
assert.equal(renderedChildLabel.childNodes.length, 2);
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment