Skip to content

Commit

Permalink
fix: findDOMNode should return null while input is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Mar 5, 2018
1 parent 0d8729d commit 76bbec5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
45 changes: 27 additions & 18 deletions packages/nerv/__tests__/dom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,25 +257,34 @@ describe('dom', () => {
expect(scratch.textContent).toBe('')
})

it('should findDomNode works', () => {
const B = <b>hellooo</b>
render(B, scratch)
expect(findDOMNode(B).firstChild.textContent).toBe('hellooo')
// expect(findDOMNode(B)).toBe(B)
let app
class Comp extends Component {
constructor () {
super()
app = this
}
render () {
return <span>test</span>
describe('findDOMNode', () => {
it('should findDomNode works', () => {
const B = <b>hellooo</b>
render(B, scratch)
expect(findDOMNode(B).firstChild.textContent).toBe('hellooo')
// expect(findDOMNode(B)).toBe(B)
let app
class Comp extends Component {
constructor () {
super()
app = this
}
render () {
return <span>test</span>
}
}
}
const C = <Comp />
render(C, scratch)
expect(findDOMNode(app).textContent).toBe('test')
// expect(findDOMNode(Comp).firstChild.textContent).toBe('test')
const C = <Comp />
render(C, scratch)
expect(findDOMNode(app).textContent).toBe('test')
// expect(findDOMNode(Comp).firstChild.textContent).toBe('test')
})

it('should return null while input is invalid', () => {
expect(findDOMNode(false)).toBe(null)
expect(findDOMNode(undefined)).toBe(null)
expect(findDOMNode(null)).toBe(null)
expect(findDOMNode(true)).toBe(null)
})
})
})
})
10 changes: 8 additions & 2 deletions packages/nerv/src/dom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isValidElement as isValidNervElement, VType, isComponent } from 'nerv-shared'
import { isValidElement as isValidNervElement, VType, isComponent, isInvalid } from 'nerv-shared'
import { nextTick } from 'nerv-utils'
import { render } from './render'
import { unmount } from './vdom/unmount'
Expand All @@ -16,7 +16,13 @@ export function unmountComponentAtNode (dom) {
}

export function findDOMNode (component) {
return (isComponent(component) ? component.vnode.dom : component.dom) || component
if (isInvalid(component)) {
return null
}
return isComponent(component)
? component.vnode.dom
: isValidNervElement(component)
? component.dom : component
}

export function createFactory (type) {
Expand Down

0 comments on commit 76bbec5

Please sign in to comment.