-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the ability to specify a React Context that will be available to your Components within the bootstrapping process. Deprecates "asyncBootstrap" method on your components in favour of "bootstrap" alternative name. Your "asyncBootstrap" methods will still work, however you will get a deprecation warning printed. Adds a bunch more documentation.
- Loading branch information
Showing
5 changed files
with
747 additions
and
390 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,60 @@ | ||
/* eslint-disable react/prop-types */ | ||
/* eslint-disable react/no-multi-comp */ | ||
|
||
import React, { Component } from 'react' | ||
import asyncBootstrapper from '../' | ||
|
||
describe('asyncBootstrapper()', () => { | ||
it('works', () => { | ||
const values = [] | ||
|
||
class Foo extends Component { | ||
asyncBootstrap() { | ||
values.push(this.props.id) | ||
return true | ||
} | ||
|
||
render() { | ||
return <div>{this.props.children}</div> | ||
} | ||
let values = [] | ||
let actualContext | ||
|
||
class DeprecatedAPI extends Component { | ||
asyncBootstrap() { | ||
values.push(this.props.id) | ||
return true | ||
} | ||
|
||
render() { | ||
return <div>{this.props.children}</div> | ||
} | ||
} | ||
|
||
const app = ( | ||
<Foo id={1}> | ||
<div> | ||
<h1>Test</h1> | ||
</div> | ||
<Foo id={2}> | ||
<Foo id={4} /> | ||
</Foo> | ||
<Foo id={3} /> | ||
class NewAPI extends Component { | ||
bootstrap() { | ||
values.push(this.props.id) | ||
actualContext = this.context.isBootstrapping | ||
return true | ||
} | ||
|
||
render() { | ||
return <div>{this.props.children}</div> | ||
} | ||
} | ||
|
||
const app = Foo => ( | ||
<Foo id={1}> | ||
<div> | ||
<h1>Test</h1> | ||
</div> | ||
<Foo id={2}> | ||
<Foo id={4} /> | ||
</Foo> | ||
) | ||
<Foo id={3} /> | ||
</Foo> | ||
) | ||
|
||
return asyncBootstrapper(app).then(() => | ||
expect(values).toEqual([1, 2, 4, 3]), | ||
) | ||
beforeEach(() => { | ||
values = [] | ||
}) | ||
|
||
it('deprecated API', () => | ||
asyncBootstrapper(app(DeprecatedAPI)).then(() => | ||
expect(values).toEqual([1, 2, 4, 3]), | ||
)) | ||
|
||
it('new API', () => | ||
asyncBootstrapper(app(NewAPI), null, { isBootstrapping: true }).then(() => { | ||
expect(values).toEqual([1, 2, 4, 3]) | ||
expect(actualContext).toBe(true) | ||
})) | ||
}) |
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 |
---|---|---|
@@ -1,11 +1,20 @@ | ||
import reactTreeWalker from 'react-tree-walker' | ||
|
||
export default function asyncBootstrapper(app, options) { | ||
const warnmsg = | ||
'Deprecation notice: you are using the deprecated "asyncBootsrap" for "react-async-bootstrapper", please rename these to "bootstrap"' | ||
|
||
export default function asyncBootstrapper(app, options, context = {}) { | ||
const visitor = (element, instance) => { | ||
if (instance && typeof instance.asyncBootstrap === 'function') { | ||
return instance.asyncBootstrap() | ||
if ( | ||
instance && | ||
(typeof instance.asyncBootstrap === 'function' || | ||
typeof instance.bootstrap === 'function') | ||
) { | ||
return typeof instance.bootstrap === 'function' | ||
? instance.bootstrap() | ||
: console.log(warnmsg) || instance.asyncBootstrap() | ||
} | ||
} | ||
|
||
return reactTreeWalker(app, visitor, {}, options) | ||
return reactTreeWalker(app, visitor, context, options) | ||
} |
Oops, something went wrong.