-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Fix warning of findDOMNode (#419)
* fix: findDOMNode should only work on validate element * fix: find logic
- Loading branch information
Showing
2 changed files
with
66 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
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,58 @@ | ||
import * as React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import findDOMNode from '../src/Dom/findDOMNode'; | ||
|
||
describe('findDOMNode', () => { | ||
it('base work', () => { | ||
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
|
||
const divRef = React.createRef<HTMLDivElement>(); | ||
const { container } = render( | ||
<React.StrictMode> | ||
<div ref={divRef} /> | ||
</React.StrictMode>, | ||
); | ||
|
||
const ele = findDOMNode(divRef.current); | ||
expect(container.firstChild).toBe(ele); | ||
|
||
expect(errSpy).not.toHaveBeenCalled(); | ||
errSpy.mockRestore(); | ||
}); | ||
|
||
it('not throw if is not a React obj', () => { | ||
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
|
||
const empty = findDOMNode({} as any); | ||
expect(empty).toBeNull(); | ||
|
||
expect(errSpy).not.toHaveBeenCalled(); | ||
errSpy.mockRestore(); | ||
}); | ||
|
||
it('class component', () => { | ||
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
|
||
class DOMWrapper extends React.Component { | ||
getDOM = () => { | ||
return findDOMNode(this); | ||
}; | ||
|
||
render() { | ||
return <div />; | ||
} | ||
} | ||
|
||
const wrapperRef = React.createRef<DOMWrapper>(); | ||
const { container } = render( | ||
<React.StrictMode> | ||
<DOMWrapper ref={wrapperRef} /> | ||
</React.StrictMode>, | ||
); | ||
|
||
expect(wrapperRef.current!.getDOM()).toBe(container.firstChild); | ||
|
||
expect(errSpy).toHaveBeenCalled(); | ||
errSpy.mockRestore(); | ||
}); | ||
}); |
83a3ffd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
util – ./
util.vercel.app
util-git-master-react-component.vercel.app
util-react-component.vercel.app