-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
getDisplayName.test.js
69 lines (57 loc) · 2.38 KB
/
getDisplayName.test.js
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
/* eslint-disable react/prefer-stateless-function */
import React from 'react';
import { assert } from 'chai';
import getDisplayName, { getFunctionName } from './getDisplayName';
describe('utils/getDisplayName.js', () => {
describe('getDisplayName', () => {
it('gets the display name of a React component', () => {
class SomeComponent extends React.Component {
render() {
return <div />;
}
}
class SomeOtherComponent extends React.Component {
render() {
return <div />;
}
}
SomeOtherComponent.displayName = 'CustomDisplayName';
function YetAnotherComponent() {
return <div />;
}
const AndAnotherComponent = () => <div />;
const AnonymousForwardRefComponent = React.forwardRef((props, ref) => (
<div {...props} ref={ref} />
));
const ForwardRefComponent = React.forwardRef(function Div(props, ref) {
return <div {...props} ref={ref} />;
});
const NamedForwardRefComponent = React.forwardRef((props, ref) => (
<div {...props} ref={ref} />
));
NamedForwardRefComponent.displayName = 'Div';
assert.strictEqual(getDisplayName(SomeComponent), 'SomeComponent');
assert.strictEqual(getDisplayName(SomeOtherComponent), 'CustomDisplayName');
assert.strictEqual(getDisplayName(YetAnotherComponent), 'YetAnotherComponent');
assert.strictEqual(getDisplayName(AndAnotherComponent), 'AndAnotherComponent');
assert.strictEqual(getDisplayName(() => <div />), 'Component');
assert.strictEqual(getDisplayName('div'), 'div');
assert.strictEqual(getDisplayName(AnonymousForwardRefComponent), 'ForwardRef');
assert.strictEqual(getDisplayName(ForwardRefComponent), 'ForwardRef(Div)');
assert.strictEqual(getDisplayName(NamedForwardRefComponent), 'Div');
assert.strictEqual(getDisplayName(), undefined);
assert.strictEqual(getDisplayName({}), undefined);
assert.strictEqual(getDisplayName(false), undefined);
});
});
describe('getFunctionName', () => {
it('gets the name of a function', () => {
function SomeFunction() {
return <div />;
}
const SomeOtherFunction = () => <div />;
assert.strictEqual(getFunctionName(SomeFunction), 'SomeFunction');
assert.strictEqual(getFunctionName(SomeOtherFunction), 'SomeOtherFunction');
});
});
});