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 => render(jsx).replace(/ {2}/g, '\t');

	it('should render as JSX', () => {
		let rendered = renderJsx(
			<section>
				<a href="/foo">foo</a>
				bar
				<p>hello</p>
			</section>
		);

		expect(rendered).to.equal(dedent`
			<section>
				<a href="/foo">foo</a>
				bar
				<p>hello</p>
			</section>
		`);
	});

	it('should render JSX attributes inline if short enough', () => {
		expect(renderJsx(
			<a b="c">bar</a>
		)).to.equal(dedent`
			<a b="c">bar</a>
		`);

		expect(renderJsx(
			<a b>bar</a>
		)).to.equal(dedent`
			<a b={true}>bar</a>
		`);

		expect(renderJsx(
			<a b={false}>bar</a>
		)).to.equal(dedent`
			<a b={false}>bar</a>
		`);
	});

	it('should render JSX attributes as multiline if complex', () => {
		expect(renderJsx(
			<a b={['a','b','c','d']}>bar</a>
		)).to.equal(dedent`
			<a
				b={
					Array [
						"a",
						"b",
						"c",
						"d"
					]
				}
			>
				bar
			</a>
		`);
	});

	it('should render attributes containing VNodes', () => {
		expect(renderJsx(
			<a b={<c />}>bar</a>
		)).to.equal(dedent`
			<a b={<c />}>bar</a>
		`);

		expect(renderJsx(
			<a b={[
				<c />,
				<d f="g" />
			]}>bar</a>
		)).to.equal(dedent`
			<a
				b={
					Array [
						<c />,
						<d f="g" />
					]
				}
			>
				bar
			</a>
		`);
	});
});

@@ -1345,32 +1345,245 @@
 /a%3E%0A%09%09%60);%0A%09%7D);%0A%0A
+%09it('should skip null and undefined attributes', () =%3E %7B%0A%09%09expect(renderJsx(%0A%09%09%09%3Ca b=%7Bnull%7D%3Ebar%3C/a%3E%0A%09%09)).to.equal(%60%3Ca%3Ebar%3C/a%3E%60);%0A%0A%09%09expect(renderJsx(%0A%09%09%09%3Ca b=%7Bundefined%7D%3Ebar%3C/a%3E%0A%09%09)).to.equal(%60%3Ca%3Ebar%3C/a%3E%60);%0A%09%7D);%0A%0A
 %09it('should rend

Add test for null/undefined skipping in JSX mode