-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Component-test.tsx
183 lines (157 loc) · 3.5 KB
/
Component-test.tsx
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
import 'mocha';
import { expect } from 'chai';
import { createElement, Component, RenderableProps, Fragment } from '../../';
// Test `this` binding on event handlers
function onHandler(this: HTMLInputElement, event: any) {
return this.value;
}
const foo = <input onChange={onHandler} />;
export class ContextComponent extends Component<{ foo: string }> {
getChildContext() {
return { something: 2 };
}
render() {
return null;
}
}
export interface SimpleComponentProps {
initialName: string | null;
}
export interface SimpleState {
name: string | null;
}
export class SimpleComponent extends Component<
SimpleComponentProps,
SimpleState
> {
constructor(props: SimpleComponentProps) {
super(props);
this.state = {
name: props.initialName
};
}
render() {
if (!this.state.name) {
return null;
}
const { initialName, children } = this.props;
return (
<div>
<span>
{initialName} / {this.state.name}
</span>
{children}
</div>
);
}
}
class DestructuringRenderPropsComponent extends Component<
SimpleComponentProps,
SimpleState
> {
constructor(props: SimpleComponentProps) {
super(props);
this.state = {
name: props.initialName
};
}
render({ initialName, children }: RenderableProps<SimpleComponentProps>) {
if (!this.state.name) {
return null;
}
return (
<span>
{this.props.initialName} / {this.state.name}
</span>
);
}
}
interface RandomChildrenComponentProps {
num?: number;
val?: string;
span?: boolean;
}
class RandomChildrenComponent extends Component<RandomChildrenComponentProps> {
render() {
const { num, val, span } = this.props;
if (num) {
return num;
}
if (val) {
return val;
}
if (span) {
return <span>hi</span>;
}
return null;
}
}
class StaticComponent extends Component<SimpleComponentProps, SimpleState> {
static getDerivedStateFromProps(
props: SimpleComponentProps,
state: SimpleState
): Partial<SimpleState> {
return {
...props,
...state
};
}
static getDerivedStateFromError(err: Error) {
return {
name: err.message
};
}
render() {
return null;
}
}
function MapperItem(props: { foo: number }) {
return <div />;
}
function Mapper() {
return [1, 2, 3].map(x => <MapperItem foo={x} key={x} />);
}
describe('Component', () => {
const component = new SimpleComponent({ initialName: 'da name' });
it('has state', () => {
expect(component.state.name).to.eq('da name');
});
it('has props', () => {
expect(component.props.initialName).to.eq('da name');
});
it('has no base when not mounted', () => {
expect(component.base).to.not.exist;
});
describe('setState', () => {
// No need to execute these tests. because we only need to check if
// the types are working. Executing them would require the DOM.
// TODO: Run TS tests in our standard karma setup
it.skip('can be used with an object', () => {
component.setState({ name: 'another name' });
});
it.skip('can be used with a function', () => {
const updater = (state: any, props: any) => ({
name: `${state.name} - ${props.initialName}`
});
component.setState(updater);
});
});
describe('render', () => {
it('can return null', () => {
const comp = new SimpleComponent({ initialName: null });
const actual = comp.render();
expect(actual).to.eq(null);
});
});
describe('Fragment', () => {
it('should render nested Fragments', () => {
var vnode = (
<Fragment>
<Fragment>foo</Fragment>
bar
</Fragment>
);
expect(vnode.type).to.be.equal(Fragment);
});
});
});