This repository has been archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use display name of the component (#92)
* Fixing #91 * Adding tests for React.memo * Adding tests for React.forwardRef * Adding basic hook tests * Fixing some issues * Fixing bad .type property in get where the selector is a function * Ensuring all specs pass * Adding stub for hooks spec for the time being...
- Loading branch information
1 parent
aca9d68
commit 721f55c
Showing
9 changed files
with
144 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// <reference types="cypress" /> | ||
/// <reference types="../../lib" /> | ||
|
||
import React from 'react' | ||
import CounterWithHooks from '../../src/counter-with-hooks.jsx' | ||
|
||
/* eslint-env mocha */ | ||
describe('CounterWithHooks component', function () { | ||
it.skip('works', function () { | ||
cy.mount(<CounterWithHooks initialCount={3} />) | ||
cy.contains('3') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/// <reference types="cypress" /> | ||
/// <reference types="../../lib" /> | ||
|
||
import React from 'react' | ||
import Button from '../../src/forward-ref.jsx' | ||
|
||
/* eslint-env mocha */ | ||
describe('Button component', function () { | ||
it('works', function () { | ||
cy.mount(<Button>Hello, World</Button>) | ||
cy.contains('Hello, World') | ||
}) | ||
|
||
it('forwards refs as expected', function () { | ||
const ref = React.createRef(); | ||
|
||
cy.mount(<Button className="testing" ref={ref}>Hello, World</Button>); | ||
expect(ref).to.have.property('current'); | ||
// expect(ref.current).not.be.null; | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// <reference types="cypress" /> | ||
/// <reference types="../../lib" /> | ||
|
||
import React from 'react' | ||
import Button from '../../src/pure-component.jsx' | ||
|
||
/* eslint-env mocha */ | ||
describe('Button pure component', function () { | ||
it('works', function () { | ||
cy.mount(<Button>Hello</Button>) | ||
cy.contains('Hello') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/// <reference path="./index.d.ts" /> | ||
|
||
const cachedDisplayNames: WeakMap<JSX, string> = new WeakMap(); | ||
|
||
/** | ||
* Gets the display name of the component when possible. | ||
* @param type {JSX} The type object returned from creating the react element. | ||
* @param fallbackName {string} The alias, or fallback name to use when the name cannot be derived. | ||
* @link https://github.com/facebook/react-devtools/blob/master/backend/getDisplayName.js | ||
*/ | ||
export default function getDisplayName(type: JSX, fallbackName: string = 'Unknown'): string { | ||
const nameFromCache = cachedDisplayNames.get(type) | ||
|
||
if (nameFromCache != null) { | ||
return nameFromCache | ||
} | ||
|
||
let displayName: string | ||
|
||
// The displayName property is not guaranteed to be a string. | ||
// It's only safe to use for our purposes if it's a string. | ||
// github.com/facebook/react-devtools/issues/803 | ||
if (typeof type.displayName === 'string') { | ||
displayName = type.displayName | ||
} | ||
|
||
if (!displayName) { | ||
displayName = type.name || fallbackName | ||
} | ||
|
||
// Facebook-specific hack to turn "Image [from Image.react]" into just "Image". | ||
// We need displayName with module name for error reports but it clutters the DevTools. | ||
const match = displayName.match(/^(.*) \[from (.*)\]$/) | ||
|
||
if (match) { | ||
const componentName = match[1] | ||
const moduleName = match[2] | ||
|
||
if (componentName && moduleName) { | ||
if ( | ||
moduleName === componentName || | ||
moduleName.startsWith(componentName + '.') | ||
) { | ||
displayName = componentName | ||
} | ||
} | ||
} | ||
|
||
cachedDisplayNames.set(type, displayName) | ||
|
||
return displayName | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React from 'react'; | ||
|
||
export default function CounterWithHooks({ initialCount = 0 }) { | ||
const [count, setCount] = React.useState(initialCount); | ||
|
||
const handleCountIncrement = React.useCallback(() => { | ||
setCount(count + 1); | ||
}, [count]); | ||
|
||
return ( | ||
<> | ||
<div className="counter"> | ||
{count} | ||
</div> | ||
<button onClick={handleCountIncrement}>+</button> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import React from 'react'; | ||
|
||
const Button = React.forwardRef(({ children, ...rest }, ref) => <button {...rest} ref={ref}>{children}</button>); | ||
|
||
export default Button; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import React from 'react'; | ||
|
||
const Button = ({ children, ...rest }) => { | ||
return <button {...rest}>{children}</button>; | ||
}; | ||
|
||
export default React.memo(Button); |