summaryrefslogtreecommitdiff
path: root/preact/debug/test/browser/serializeVNode.test.js
blob: f8c851527fc49f3775e258c2f6c93d81e7a25528 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { createElement, Component } from 'preact';
import { serializeVNode } from '../../src/debug';

/** @jsx createElement */

describe('serializeVNode', () => {
	it("should prefer a function component's displayName", () => {
		function Foo() {
			return <div />;
		}
		Foo.displayName = 'Bar';

		expect(serializeVNode(<Foo />)).to.equal('<Bar />');
	});

	it("should prefer a class component's displayName", () => {
		class Bar extends Component {
			render() {
				return <div />;
			}
		}
		Bar.displayName = 'Foo';

		expect(serializeVNode(<Bar />)).to.equal('<Foo />');
	});

	it('should serialize vnodes without children', () => {
		expect(serializeVNode(<br />)).to.equal('<br />');
	});

	it('should serialize vnodes with children', () => {
		expect(serializeVNode(<div>Hello World</div>)).to.equal('<div>..</div>');
	});

	it('should serialize components', () => {
		function Foo() {
			return <div />;
		}
		expect(serializeVNode(<Foo />)).to.equal('<Foo />');
	});

	it('should serialize props', () => {
		expect(serializeVNode(<div class="foo" />)).to.equal('<div class="foo" />');

		// Ensure that we have a predictable function name. Our test runner
		// creates an all inclusive bundle per file and the identifier
		// "noop" may have already been used.
		// eslint-disable-next-line func-style
		let noop = function noopFn() {};
		expect(serializeVNode(<div onClick={noop} />)).to.equal(
			'<div onClick="function noopFn() {}" />'
		);

		function Foo(props) {
			return props.foo;
		}

		expect(serializeVNode(<Foo foo={[1, 2, 3]} />)).to.equal(
			'<Foo foo="1,2,3" />'
		);

		expect(serializeVNode(<div prop={Object.create(null)} />)).to.equal(
			'<div prop="[object Object]" />'
		);
	});
});