import render from '../src/jsx'; import { h, Component } from 'preact'; import chai, { expect } from 'chai'; import { spy, match } from 'sinon'; import sinonChai from 'sinon-chai'; chai.use(sinonChai); // tag to remove leading whitespace from tagged template literal function dedent([str]) { return str.split( '\n'+str.match(/^\n*(\s+)/)[1] ).join('\n').replace(/(^\n+|\n+\s*$)/g, ''); } describe('jsx', () => { let renderJsx = (jsx, opts) => render(jsx, null, opts).replace(/ {2}/g, '\t'); it('should render as JSX', () => { let rendered = renderJsx(
foo bar

hello

); expect(rendered).to.equal(dedent`
foo bar

hello

`); }); it('should not render empty class or style DOM attributes', () => { expect(renderJsx()).to.equal(''); expect(renderJsx()).to.equal(''); expect(renderJsx()).to.equal(''); expect(renderJsx()).to.equal(''); expect(renderJsx()).to.equal(''); expect(renderJsx()).to.equal(''); }); it('should render JSX attributes inline if short enough', () => { expect(renderJsx( bar )).to.equal(dedent` bar `); expect(renderJsx( bar )).to.equal(dedent` bar `); expect(renderJsx( bar )).to.equal(dedent` bar `); function F(){} expect(renderJsx( bar, { shallow:true, renderRootComponent:false } )).to.equal(dedent` bar `); }); it('should render JSX attributes as multiline if complex', () => { expect(renderJsx( bar )).to.equal(dedent` bar `); }); it('should skip null and undefined attributes', () => { expect(renderJsx( bar )).to.equal(`bar`); expect(renderJsx( bar )).to.equal(`bar`); }); it('should render attributes containing VNodes', () => { expect(renderJsx( }>bar )).to.equal(dedent` }>bar `); expect(renderJsx( , ]}>bar )).to.equal(dedent` , ] } > bar `); }); it('should render empty resolved children identically to no children', () => { const Empty = () => null; const False = () => false; expect(renderJsx(
{null} {false}
)).to.equal(dedent`
`); }); it('should skip null siblings', () => { expect(renderJsx( {null} )).to.deep.equal(dedent` `); }); it('should skip functions if functions=false', () => { expect(renderJsx(
{}} />, { functions:false } )).to.equal('
'); }); it('should skip function names if functionNames=false', () => { expect(renderJsx(
{}} />, { functionNames:false } )).to.equal('
'); expect(renderJsx(
, { functionNames:false } )).to.equal('
'); }); it('should render self-closing elements', () => { expect(renderJsx( )).to.deep.equal(dedent` `); }); });