From 43e09fe52be4f6902f57dfd4e678a7dd719f1079 Mon Sep 17 00:00:00 2001 From: Brian Vaughn Date: Tue, 17 Sep 2019 13:11:25 -0700 Subject: [PATCH] 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). --- .../src/__tests__/utils-test.js | 50 ++++++------------- 1 file changed, 16 insertions(+), 34 deletions(-) diff --git a/packages/react-devtools-shared/src/__tests__/utils-test.js b/packages/react-devtools-shared/src/__tests__/utils-test.js index ae4d3a0e42982..37e445974a28c 100644 --- a/packages/react-devtools-shared/src/__tests__/utils-test.js +++ b/packages/react-devtools-shared/src/__tests__/utils-test.js @@ -11,48 +11,30 @@ import {getDisplayName} from 'react-devtools-shared/src/utils'; describe('utils', () => { describe('getDisplayName', () => { - const fallbackName = 'TestFallbackName'; - - it('should return default fallback name for empty object', () => { - const result = getDisplayName({}); - expect(result).toEqual('Anonymous'); - }); - - it('should return displayName property from object', () => { - const obj = { - displayName: 'TestDisplayName', - }; - const result = getDisplayName(obj); - expect(result).toEqual(obj.displayName); + it('should return a function name', () => { + function FauxComponent() {} + expect(getDisplayName(FauxComponent)).toEqual('FauxComponent'); }); - it('should return name property from object', () => { - const obj = { - name: 'TestName', - }; - const result = getDisplayName(obj); - expect(result).toEqual(obj.name); + it('should return a displayName name if specified', () => { + function FauxComponent() {} + FauxComponent.displayName = 'OverrideDisplayName'; + expect(getDisplayName(FauxComponent)).toEqual('OverrideDisplayName'); }); - it('should return provided fallback name for empty object', () => { - const result = getDisplayName({}, fallbackName); - expect(result).toEqual(fallbackName); + it('should return the fallback for anonymous functions', () => { + expect(getDisplayName(() => {}, 'Fallback')).toEqual('Fallback'); }); - it('should provide fallback name when displayName prop is not a string', () => { - const obj = { - displayName: {}, - }; - const result = getDisplayName(obj, fallbackName); - expect(result).toEqual(fallbackName); + it('should return Anonymous for anonymous functions without a fallback', () => { + expect(getDisplayName(() => {})).toEqual('Anonymous'); }); - it('should provide fallback name when name prop is not a string', () => { - const obj = { - name: {}, - }; - const result = getDisplayName(obj, fallbackName); - expect(result).toEqual(fallbackName); + // Simulate a reported bug: + // https://github.com/facebook/react/issues/16685 + it('should return a fallback when the name prop is not a string', () => { + const FauxComponent = {name: {}}; + expect(getDisplayName(FauxComponent, 'Fallback')).toEqual('Fallback'); }); }); });