-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e4f012d
commit 0479ae0
Showing
7 changed files
with
121 additions
and
81 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
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,31 +1,31 @@ | ||
import React from 'react' | ||
|
||
const isStateless = component => !(component.prototype && component.prototype.isReactComponent) | ||
const isStateless = Component => !(Component.prototype && Component.prototype.isReactComponent) | ||
|
||
const createClass = component => React.createClass({ | ||
render() { | ||
return component(this.props) | ||
}, | ||
}) | ||
|
||
const wrap = (Target, Handler) => { | ||
const originalRender = Target.prototype.render | ||
const wrap = (Component, Handler) => { | ||
const originalRender = Component.prototype.render | ||
|
||
Target.prototype.render = function render(...args) { | ||
Component.prototype.render = function render(...args) { | ||
try { | ||
return originalRender.apply(this, args) | ||
} catch (error) { | ||
return <Handler error={error} /> | ||
} | ||
} | ||
|
||
return Target | ||
return Component | ||
} | ||
|
||
export default handler => component => { | ||
const Component = isStateless(component) ? createClass(component) : component | ||
const Poop = error => <div title={error}>💩</div> | ||
const Handler = handler || Poop | ||
const Poop = error => <div title={error}>💩</div> | ||
|
||
return wrap(Component, Handler) | ||
export default Handler => Component => { | ||
const Target = isStateless(Component) ? createClass(Component) : Component | ||
|
||
return wrap(Target, Handler || Poop) | ||
} |
This file was deleted.
Oops, something went wrong.
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,81 @@ | ||
import assert from 'assert' | ||
import { shallow, mount } from 'enzyme' | ||
import React from 'react' | ||
|
||
import poop from '../src' | ||
|
||
describe('poop', () => { | ||
const Handler = () => <div>Handler</div> | ||
|
||
describe('class', () => { | ||
let NiceClass | ||
let EvilClass | ||
|
||
beforeEach(() => { | ||
NiceClass = React.createClass({ | ||
render() { | ||
return <div>Dummy</div> | ||
}, | ||
}) | ||
|
||
EvilClass = React.createClass({ | ||
render() { | ||
return <div>{this.does.not.exist}</div> | ||
}, | ||
}) | ||
}) | ||
|
||
it('renders the component when everything is fine', () => { | ||
const Dummy = poop()(NiceClass) | ||
const wrapper = shallow(<Dummy />) | ||
|
||
assert(wrapper.contains('Dummy')) | ||
}) | ||
|
||
it('renders the handler when something goes wrong', () => { | ||
const Dummy = poop(Handler)(EvilClass) | ||
const wrapper = mount(<Dummy />) | ||
|
||
assert.equal(wrapper.find('Handler').length, 1) | ||
}) | ||
|
||
it('renders the poop when something goes wrong', () => { | ||
const Dummy = poop()(EvilClass) | ||
const wrapper = mount(<Dummy />) | ||
|
||
assert.equal(wrapper.find('Poop').length, 1) | ||
}) | ||
}) | ||
|
||
describe('stateless', () => { | ||
let NiceStateless | ||
let EvilStateless | ||
|
||
beforeEach(() => { | ||
NiceStateless = () => <div>Dummy</div> | ||
|
||
EvilStateless = () => <div>{this.does.not.exist}</div> | ||
}) | ||
|
||
it('renders the component when everything is fine', () => { | ||
const Dummy = poop()(NiceStateless) | ||
const wrapper = shallow(<Dummy />) | ||
|
||
assert(wrapper.contains('Dummy')) | ||
}) | ||
|
||
it('renders the handler when something goes wrong', () => { | ||
const Dummy = poop(Handler)(EvilStateless) | ||
const wrapper = mount(<Dummy />) | ||
|
||
assert.equal(wrapper.find('Handler').length, 1) | ||
}) | ||
|
||
it('renders the poop when something goes wrong', () => { | ||
const Dummy = poop()(EvilStateless) | ||
const wrapper = mount(<Dummy />) | ||
|
||
assert.equal(wrapper.find('Poop').length, 1) | ||
}) | ||
}) | ||
}) |
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,13 @@ | ||
import { jsdom } from 'jsdom' | ||
|
||
global.document = jsdom('') | ||
global.window = document.defaultView | ||
Object.keys(document.defaultView).forEach((property) => { | ||
if (typeof global[property] === 'undefined') { | ||
global[property] = document.defaultView[property] | ||
} | ||
}) | ||
|
||
global.navigator = { | ||
userAgent: 'node.js', | ||
} |