Skip to content

Commit 43e09fe

Browse files
authored
Tweaked tests to use real functions
This more closely simulates how the utility is being used in production, and would catch cases like anonymous functions (with empty string names).
1 parent 2556fac commit 43e09fe

File tree

1 file changed

+16
-34
lines changed

1 file changed

+16
-34
lines changed

packages/react-devtools-shared/src/__tests__/utils-test.js

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,30 @@ import {getDisplayName} from 'react-devtools-shared/src/utils';
1111

1212
describe('utils', () => {
1313
describe('getDisplayName', () => {
14-
const fallbackName = 'TestFallbackName';
15-
16-
it('should return default fallback name for empty object', () => {
17-
const result = getDisplayName({});
18-
expect(result).toEqual('Anonymous');
19-
});
20-
21-
it('should return displayName property from object', () => {
22-
const obj = {
23-
displayName: 'TestDisplayName',
24-
};
25-
const result = getDisplayName(obj);
26-
expect(result).toEqual(obj.displayName);
14+
it('should return a function name', () => {
15+
function FauxComponent() {}
16+
expect(getDisplayName(FauxComponent)).toEqual('FauxComponent');
2717
});
2818

29-
it('should return name property from object', () => {
30-
const obj = {
31-
name: 'TestName',
32-
};
33-
const result = getDisplayName(obj);
34-
expect(result).toEqual(obj.name);
19+
it('should return a displayName name if specified', () => {
20+
function FauxComponent() {}
21+
FauxComponent.displayName = 'OverrideDisplayName';
22+
expect(getDisplayName(FauxComponent)).toEqual('OverrideDisplayName');
3523
});
3624

37-
it('should return provided fallback name for empty object', () => {
38-
const result = getDisplayName({}, fallbackName);
39-
expect(result).toEqual(fallbackName);
25+
it('should return the fallback for anonymous functions', () => {
26+
expect(getDisplayName(() => {}, 'Fallback')).toEqual('Fallback');
4027
});
4128

42-
it('should provide fallback name when displayName prop is not a string', () => {
43-
const obj = {
44-
displayName: {},
45-
};
46-
const result = getDisplayName(obj, fallbackName);
47-
expect(result).toEqual(fallbackName);
29+
it('should return Anonymous for anonymous functions without a fallback', () => {
30+
expect(getDisplayName(() => {})).toEqual('Anonymous');
4831
});
4932

50-
it('should provide fallback name when name prop is not a string', () => {
51-
const obj = {
52-
name: {},
53-
};
54-
const result = getDisplayName(obj, fallbackName);
55-
expect(result).toEqual(fallbackName);
33+
// Simulate a reported bug:
34+
// https://github.com/facebook/react/issues/16685
35+
it('should return a fallback when the name prop is not a string', () => {
36+
const FauxComponent = {name: {}};
37+
expect(getDisplayName(FauxComponent, 'Fallback')).toEqual('Fallback');
5638
});
5739
});
5840
});

0 commit comments

Comments
 (0)