This repository has been archived by the owner on Sep 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Passing in args as per React API. Added shouldComponentUpdate
- Loading branch information
Showing
5 changed files
with
104 additions
and
19 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
exports[`test Props are passed to wrapped component and can be rendered 1`] = ` | ||
<div> | ||
React Lifecycle Component | ||
is the best | ||
</div> | ||
`; |
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,72 @@ | ||
import React, { Component } from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import renderer from 'react-test-renderer'; | ||
import applyLifecycle from '../src/applyLifecycle'; | ||
|
||
class TestComponent extends Component { | ||
render() { | ||
return <div>React Lifecycle Component</div>; | ||
} | ||
} | ||
|
||
class TestComponentTwo extends Component { | ||
render() { | ||
return <div>React Lifecycle Component {this.props.message}</div>; | ||
} | ||
} | ||
|
||
const TestComponentThree = () => <div />; | ||
|
||
const lifecycleProps = { | ||
componentWillMount: jest.fn() | ||
} | ||
|
||
test('Wrapper component calls lifecycle methods on incoming props', () => { | ||
const Wrapped = applyLifecycle(TestComponent, lifecycleProps); | ||
const component = renderer.create(<Wrapped />); | ||
expect(lifecycleProps.componentWillMount.mock.calls.length).toEqual(1); | ||
}); | ||
|
||
test('Props are passed to wrapped component and can be rendered', () => { | ||
const Wrapped = applyLifecycle(TestComponentTwo, lifecycleProps); | ||
const component = renderer.create(<Wrapped message="is the best" />); | ||
const tree = component.toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
|
||
|
||
|
||
test('Lifecycle hooks receive the correct arguments', () => { | ||
|
||
const lifecycleProps = { | ||
componentWillReceiveProps(nextProps) { | ||
expect(this.props.name).toEqual('Bob') | ||
expect(nextProps.name).toEqual('James'); | ||
} | ||
}; | ||
|
||
const node = document.createElement('div'); | ||
const Wrapped = applyLifecycle(TestComponentThree, lifecycleProps); | ||
|
||
ReactDOM.render(<Wrapped name="Bob" />, node); | ||
|
||
// Re-render to cause componentWillReceiveProps call | ||
ReactDOM.render(<Wrapped name="James" />, node); | ||
}); | ||
|
||
test('Lifecycle hooks called with the correct context', () => { | ||
|
||
const lifecycleProps = { | ||
componentWillReceiveProps(nextProps) { | ||
expect(this.displayName).toEqual('Lifecycle'); | ||
} | ||
}; | ||
|
||
const node = document.createElement('div'); | ||
const Wrapped = applyLifecycle(TestComponentThree, lifecycleProps); | ||
|
||
ReactDOM.render(<Wrapped />, node); | ||
|
||
// Re-render to cause componentWillReceiveProps call | ||
ReactDOM.render(<Wrapped />, node); | ||
}); |