summaryrefslogtreecommitdiff
path: root/preact/test/ts/VNode-test.tsx
blob: 722590184ec3e50c002d68d4ba9e87f8760046da (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import 'mocha';
import { expect } from 'chai';
import {
	createElement,
	Component,
	toChildArray,
	FunctionalComponent,
	ComponentConstructor,
	ComponentFactory,
	VNode,
	ComponentChildren,
	cloneElement
} from '../../';

function getDisplayType(vnode: VNode | string | number) {
	if (typeof vnode === 'string' || typeof vnode == 'number') {
		return vnode.toString();
	} else if (typeof vnode.type == 'string') {
		return vnode.type;
	} else {
		return vnode.type.displayName;
	}
}

class SimpleComponent extends Component<{}, {}> {
	render() {
		return <div>{this.props.children}</div>;
	}
}

const SimpleFunctionalComponent = () => <div />;

const a: ComponentFactory = SimpleComponent;
const b: ComponentFactory = SimpleFunctionalComponent;

describe('VNode TS types', () => {
	it('is returned by h', () => {
		const actual = <div className="wow" />;
		expect(actual).to.include.all.keys('type', 'props', 'key');
	});

	it('has a nodeName of string when html element', () => {
		const div = <div>Hi!</div>;
		expect(div.type).to.equal('div');
	});

	it('has a nodeName equal to the construction function when SFC', () => {
		const sfc = <SimpleFunctionalComponent />;
		expect(sfc.type).to.be.instanceOf(Function);
		const constructor = sfc.type as FunctionalComponent<any>;
		expect(constructor.name).to.eq('SimpleFunctionalComponent');
	});

	it('has a nodeName equal to the constructor of a component', () => {
		const sfc = <SimpleComponent />;
		expect(sfc.type).to.be.instanceOf(Function);
		const constructor = sfc.type as ComponentConstructor<any>;
		expect(constructor.name).to.eq('SimpleComponent');
	});

	it('has children which is an array of string or other vnodes', () => {
		const comp = (
			<SimpleComponent>
				<SimpleComponent>child1</SimpleComponent>
				child2
			</SimpleComponent>
		);

		expect(comp.props.children).to.be.instanceOf(Array);
		expect(comp.props.children[1]).to.be.a('string');
	});

	it('children type should work with toChildArray', () => {
		const comp: VNode = <SimpleComponent>child1 {1}</SimpleComponent>;

		const children = toChildArray(comp.props.children);
		expect(children).to.have.lengthOf(2);
	});

	it('toChildArray should filter out some types', () => {
		const compChild = <SimpleComponent />;
		const comp: VNode = (
			<SimpleComponent>
				a{null}
				{true}
				{false}
				{2}
				{undefined}
				{['b', 'c']}
				{compChild}
			</SimpleComponent>
		);

		const children = toChildArray(comp.props.children);
		expect(children).to.deep.equal(['a', 2, 'b', 'c', compChild]);
	});

	it('functions like getDisplayType should work', () => {
		function TestComp(props: { children?: ComponentChildren }) {
			return <div>{props.children}</div>;
		}
		TestComp.displayName = 'TestComp';

		const compChild = <TestComp />;
		const comp: VNode = (
			<SimpleComponent>
				a{null}
				{true}
				{false}
				{2}
				{undefined}
				{['b', 'c']}
				{compChild}
			</SimpleComponent>
		);

		const types = toChildArray(comp.props.children).map(getDisplayType);
		expect(types).to.deep.equal(['a', '2', 'b', 'c', 'TestComp']);
	});

	it('component should work with cloneElement', () => {
		const comp: VNode = (
			<SimpleComponent>
				<div>child 1</div>
			</SimpleComponent>
		);
		const clone: VNode = cloneElement(comp);

		expect(comp.type).to.equal(clone.type);
		expect(comp.props).not.to.equal(clone.props);
		expect(comp.props).to.deep.equal(clone.props);
	});

	it('component should work with cloneElement using generics', () => {
		const comp: VNode<string> = <SimpleComponent></SimpleComponent>;
		const clone: VNode<string> = cloneElement<string>(comp);

		expect(comp.type).to.equal(clone.type);
		expect(comp.props).not.to.equal(clone.props);
		expect(comp.props).to.deep.equal(clone.props);
	});
});

class ComponentWithFunctionChild extends Component<{
	children: (num: number) => string;
}> {
	render() {
		return null;
	}
}

<ComponentWithFunctionChild>
	{num => num.toFixed(2)}
</ComponentWithFunctionChild>;

class ComponentWithStringChild extends Component<{ children: string }> {
	render() {
		return null;
	}
}

<ComponentWithStringChild>child</ComponentWithStringChild>;

class ComponentWithNumberChild extends Component<{ children: number }> {
	render() {
		return null;
	}
}

<ComponentWithNumberChild>{1}</ComponentWithNumberChild>;

class ComponentWithBooleanChild extends Component<{ children: boolean }> {
	render() {
		return null;
	}
}

<ComponentWithBooleanChild>{false}</ComponentWithBooleanChild>;

class ComponentWithNullChild extends Component<{ children: null }> {
	render() {
		return null;
	}
}

<ComponentWithNullChild>{null}</ComponentWithNullChild>;

class ComponentWithNumberChildren extends Component<{ children: number[] }> {
	render() {
		return null;
	}
}

<ComponentWithNumberChildren>
	{1}
	{2}
</ComponentWithNumberChildren>;