main
js 72 lines 2.07 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @flow
8 */
9
10 import {extractHOCNames} from 'react-devtools-shared/src/backend/views/utils';
11
12 describe('extractHOCNames', () => {
13 it('should return an empty result for an empty display name', () => {
14 expect(extractHOCNames('')).toEqual({
15 baseComponentName: '',
16 hocNames: [],
17 });
18 });
19
20 it('should not extract anything from an unwrapped component', () => {
21 expect(extractHOCNames('Button')).toEqual({
22 baseComponentName: 'Button',
23 hocNames: [],
24 });
25 });
26
27 it('should extract a single wrapper', () => {
28 expect(extractHOCNames('Memo(Button)')).toEqual({
29 baseComponentName: 'Button',
30 hocNames: ['Memo'],
31 });
32 });
33
34 it('should extract every wrapper of a nested display name', () => {
35 expect(extractHOCNames('Memo(ForwardRef(Button))')).toEqual({
36 baseComponentName: 'Button',
37 hocNames: ['Memo', 'ForwardRef'],
38 });
39 });
40
41 it('should extract wrappers nested more than two levels deep', () => {
42 expect(extractHOCNames('Memo(Forget(ForwardRef(Button)))')).toEqual({
43 baseComponentName: 'Button',
44 hocNames: ['Memo', 'Forget', 'ForwardRef'],
45 });
46 });
47
48 it('should extract lowercase wrapper names verbatim', () => {
49 expect(extractHOCNames('withRouter(Button)')).toEqual({
50 baseComponentName: 'Button',
51 hocNames: ['withRouter'],
52 });
53 });
54
55 it('should extract a mix of lowercase and uppercase wrappers', () => {
56 expect(extractHOCNames('connect(Memo(Button))')).toEqual({
57 baseComponentName: 'Button',
58 hocNames: ['connect', 'Memo'],
59 });
60 });
61
62 it('should not extract from a display name that is not shaped like a wrapper', () => {
63 expect(extractHOCNames('Foo (bar)')).toEqual({
64 baseComponentName: 'Foo (bar)',
65 hocNames: [],
66 });
67 expect(extractHOCNames('Memo(Button) extra')).toEqual({
68 baseComponentName: 'Memo(Button) extra',
69 hocNames: [],
70 });
71 });
72 });