Skip to content

Commit

Permalink
chore: more test
Browse files Browse the repository at this point in the history
  • Loading branch information
Foveluy committed May 21, 2018
1 parent 9f084d9 commit 08055d0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 190 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@
"style-loader": "^0.18.2"
}
}

35 changes: 31 additions & 4 deletions packages/__tests__/ReactRender.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const ReactDOM = require('../src/vdom')
const { Component } = require('../src/component')
const React = require('../src/Luy/index')
const { check } = require('../testUtils')

Expand All @@ -11,7 +12,7 @@ describe('ReactRenderDOM', () => {
// ReactDOM.unmountComponentAtNode(container)
})
it('should normal render', done => {
class App extends React.Component {
class App extends Component {
render() {
return (
<div>
Expand All @@ -29,7 +30,7 @@ describe('ReactRenderDOM', () => {
})

it('should render number or text in React.Component', done => {
class Numberic extends React.Component {
class Numberic extends Component {
render() {
return 1
}
Expand All @@ -43,7 +44,7 @@ describe('ReactRenderDOM', () => {
})

it('should render number in inside nested element', done => {
class App extends React.Component {
class App extends Component {
render() {
return <div>123</div>
}
Expand All @@ -57,7 +58,7 @@ describe('ReactRenderDOM', () => {
})

it('should render list child component', done => {
class App extends React.Component {
class App extends Component {
render() {
return (
<div>
Expand Down Expand Up @@ -88,4 +89,30 @@ describe('ReactRenderDOM', () => {
done()
})
})

it('should render array children', done => {
const App = () => {
return [<span />, <span />, <span />, <span />]
}
ReactDOM.render(<App />, container)

check(() => {
expect(container.innerHTML).toEqual('<span></span><span></span><span></span><span></span>')
done()
})
})

it('should render children', done => {
class App extends Component {
render() {
return <div>{this.props.children}</div>
}
}
ReactDOM.render(<App>hello children</App>, container)

check(() => {
expect(container.innerHTML).toEqual('<div>hello children</div>')
done()
})
})
})
36 changes: 19 additions & 17 deletions packages/src/vdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,15 @@ function performUnitOfWork(workInProgress) {
//收集当前节点的effect,然后向上传递
completeWork(current)
if (current.sibling) return current.sibling
if (!current.return) {
// 到达最顶端了
pendingCommit = current
}
//没有 sibling,回到这个节点的父亲,看看有没有sibling
current = current.return
}
}

function beginWork(currentFiber) {
switch (currentFiber.tag) {
case tag.CLASS_COMPONENT: {
return updateClassComponent(currentFiber)
}
case tag.FunctionalComponent: {
return updateFunctionalComponent(currentFiber)
}
default: {
return updateHostComponent(currentFiber)
}
}
}

//收集有 effecttag 的 fiber
function completeWork(currentFiber) {
if (currentFiber.tag === tag.CLASS_COMPONENT) {
Expand All @@ -139,9 +129,20 @@ function completeWork(currentFiber) {
const currentEffectTag = currentFiber.effectTag ? [currentFiber] : []
const parentEffects = currentFiber.return.effects || []
currentFiber.return.effects = parentEffects.concat(currentEffect, currentEffectTag)
} else {
// 到达最顶端了
pendingCommit = currentFiber
}
}

function beginWork(currentFiber) {
switch (currentFiber.tag) {
case tag.CLASS_COMPONENT: {
return updateClassComponent(currentFiber)
}
case tag.FunctionalComponent: {
return updateFunctionalComponent(currentFiber)
}
default: {
return updateHostComponent(currentFiber)
}
}
}

Expand Down Expand Up @@ -254,6 +255,7 @@ function placeChild(currentFiber, newChild) {
}

if (typeof type === 'function') {
// 可能有两种
const _tag = type.prototype.isReactComponent ? tag.CLASS_COMPONENT : tag.FunctionalComponent

return {
Expand Down
52 changes: 0 additions & 52 deletions study/createElement.js

This file was deleted.

117 changes: 0 additions & 117 deletions study/study.js

This file was deleted.

0 comments on commit 08055d0

Please sign in to comment.