-
Notifications
You must be signed in to change notification settings - Fork 47.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completion callbacks resolve synchronously if tree is already complete
More unit tests. These completion callbacks (as I'm calling them) have some interesting properties.
- Loading branch information
Showing
4 changed files
with
180 additions
and
26 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
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
101 changes: 101 additions & 0 deletions
101
src/renderers/shared/fiber/__tests__/ReactIncrementalRoot-test.js
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,101 @@ | ||
/** | ||
* Copyright 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var React; | ||
var ReactNoop; | ||
|
||
describe('ReactIncrementalRoot', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
React = require('react'); | ||
ReactNoop = require('react-noop-renderer'); | ||
}); | ||
|
||
function span(prop) { | ||
return {type: 'span', children: [], prop}; | ||
} | ||
|
||
it('prerenders roots', () => { | ||
const root = ReactNoop.create(); | ||
const work = root.prerender(<span prop="A" />); | ||
expect(root.getChildren()).toEqual([]); | ||
work.commit(); | ||
expect(root.getChildren()).toEqual([span('A')]); | ||
}); | ||
|
||
it('resolves `then` callback synchronously if tree is already completed', () => { | ||
const root = ReactNoop.create(); | ||
const work = root.prerender(<span prop="A" />); | ||
ReactNoop.flush(); | ||
let wasCalled = false; | ||
work.then(() => { | ||
wasCalled = true; | ||
}); | ||
expect(wasCalled).toBe(true); | ||
}); | ||
|
||
it('does not restart a completed tree if there were no additional updates', () => { | ||
let ops = []; | ||
function Foo(props) { | ||
ops.push('Foo'); | ||
return <span prop={props.children} />; | ||
} | ||
const root = ReactNoop.create(); | ||
const work = root.prerender(<Foo>Hi</Foo>); | ||
|
||
ReactNoop.flush(); | ||
expect(ops).toEqual(['Foo']); | ||
expect(root.getChildren([])); | ||
|
||
work.then(() => { | ||
ops.push('Root completed'); | ||
work.commit(); | ||
ops.push('Root committed'); | ||
}); | ||
|
||
expect(ops).toEqual([ | ||
'Foo', | ||
'Root completed', | ||
// Should not re-render Foo | ||
'Root committed', | ||
]); | ||
expect(root.getChildren([span('Hi')])); | ||
}); | ||
|
||
it('works on a blocked tree if the expiration time is less than or equal to the blocked update', () => { | ||
let ops = []; | ||
function Foo(props) { | ||
ops.push('Foo: ' + props.children); | ||
return <span prop={props.children} />; | ||
} | ||
const root = ReactNoop.create(); | ||
root.prerender(<Foo>A</Foo>); | ||
ReactNoop.flush(); | ||
|
||
expect(ops).toEqual(['Foo: A']); | ||
expect(root.getChildren([])); | ||
|
||
// workA and workB have the same expiration time | ||
root.prerender(<Foo>B</Foo>); | ||
ReactNoop.flush(); | ||
|
||
// Should have re-rendered the root, even though it's blocked | ||
// from committing. | ||
expect(ops).toEqual(['Foo: A', 'Foo: B']); | ||
expect(root.getChildren([])); | ||
}); | ||
|
||
it( | ||
'does not work on on a blocked tree if the expiration time is greater than the blocked update', | ||
); | ||
}); |