diff --git a/packages/react-devtools-shared/src/__tests__/extractHOCNames-test.js b/packages/react-devtools-shared/src/__tests__/extractHOCNames-test.js new file mode 100644 index 00000000000..b5f8ac29c6b --- /dev/null +++ b/packages/react-devtools-shared/src/__tests__/extractHOCNames-test.js @@ -0,0 +1,33 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + */ + +import {extractHOCNames} from 'react-devtools-shared/src/backend/views/utils'; + +describe('extractHOCNames', () => { + it('should extract nested HOC names in order', () => { + expect(extractHOCNames('Memo(Forget(Foo))')).toEqual({ + baseComponentName: 'Foo', + hocNames: ['Memo', 'Forget'], + }); + }); + + it('should handle a single HOC', () => { + expect(extractHOCNames('Memo(Foo)')).toEqual({ + baseComponentName: 'Foo', + hocNames: ['Memo'], + }); + }); + + it('should return the original name when there are no HOCs', () => { + expect(extractHOCNames('Foo')).toEqual({ + baseComponentName: 'Foo', + hocNames: [], + }); + }); +}); diff --git a/packages/react-devtools-shared/src/backend/views/utils.js b/packages/react-devtools-shared/src/backend/views/utils.js index a73c8094edb..397cab5137b 100644 --- a/packages/react-devtools-shared/src/backend/views/utils.js +++ b/packages/react-devtools-shared/src/backend/views/utils.js @@ -148,7 +148,7 @@ export function extractHOCNames(displayName: string): { } { if (!displayName) return {baseComponentName: '', hocNames: []}; - const hocRegex = /([A-Z][a-zA-Z0-9]*?)\((.*)\)/g; + const hocRegex = /^([A-Z][a-zA-Z0-9]*?)\((.*)\)$/; const hocNames: string[] = []; let baseComponentName = displayName; let match;