This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Revert "add tests infrastructure and simple test""
This reverts commit 48d3e16.
- Loading branch information
Edgar
committed
Feb 26, 2019
1 parent
48d3e16
commit 2561b9e
Showing
8 changed files
with
232 additions
and
139 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
presets: ['@babel/preset-env', '@babel/preset-react'], | ||
}; |
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,27 @@ | ||
import React from 'react'; | ||
|
||
export default class SimpleComponent extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { isChecked: false }; | ||
|
||
this.onChange = this.onChange.bind(this); | ||
} | ||
|
||
onChange() { | ||
this.setState({ isChecked: !this.state.isChecked }); | ||
} | ||
|
||
render() { | ||
return ( | ||
<label> | ||
<input | ||
type="checkbox" | ||
checked={this.state.isChecked} | ||
onChange={this.onChange} | ||
/> | ||
{this.state.isChecked ? this.props.labelOn : this.props.labelOff} | ||
</label> | ||
); | ||
} | ||
} |
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,24 @@ | ||
import React from 'react'; | ||
import Enzyme, { shallow } from 'enzyme'; | ||
import Adapter from 'enzyme-adapter-react-16'; | ||
|
||
Enzyme.configure({ adapter: new Adapter() }); | ||
|
||
import SimpleComponent from './components/SimpleComponent'; | ||
import patch from '../patch'; | ||
|
||
it('changed state', () => { | ||
const PatchedSimpleComponent = patch(SimpleComponent, 'test-name', console); | ||
|
||
const checkbox = shallow( | ||
<PatchedSimpleComponent labelOn="On" labelOff="Off" /> | ||
); | ||
|
||
expect(checkbox.text()).toEqual('Off'); | ||
|
||
checkbox.find('input').simulate('change'); | ||
|
||
expect(checkbox.text()).toEqual('On'); | ||
|
||
checkbox.setProps({ labelOn: 'Off' }); | ||
}); |
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,116 @@ | ||
class Logger { | ||
constructor(componentName, log) { | ||
this._componentName = componentName; | ||
this._propsChanges = []; | ||
this._stateChanges = []; | ||
this._renderCount = 0; | ||
|
||
this._log = log; | ||
} | ||
|
||
init() { | ||
if (this._isFirstRender) { | ||
this._printInit(); | ||
} | ||
|
||
this._incrementRenderCount(); | ||
} | ||
|
||
processChanges(stateChange, propsChange) { | ||
this._shallowCompareState(...stateChange); | ||
this._shallowCompareProps(...propsChange); | ||
this._printComparisonsResults(); | ||
} | ||
|
||
_printInit() { | ||
this._log.log( | ||
'%c[clear-render] init for', | ||
'color: #848d95;', | ||
this._componentName | ||
); | ||
this._log.log( | ||
'%c[clear-render] render', | ||
'color: #848d95;', | ||
this._componentName | ||
); | ||
} | ||
|
||
_printChange(change) { | ||
if (typeof change === 'object') { | ||
this._log.groupCollapsed( | ||
`${change.key} %c [${change.type}]`, | ||
'color: #848d95; font-style: italic;' | ||
); | ||
this._log.log( | ||
'%c old ', | ||
'background: #ff6347; color: #fff', | ||
change.oldValue | ||
); | ||
this._log.log( | ||
'%c new ', | ||
'background: #5fba7d; color: #fff', | ||
change.nextValue | ||
); | ||
this._log.groupEnd(); | ||
} else { | ||
this._log.log(change); | ||
} | ||
} | ||
|
||
_printComparisonsResults() { | ||
this._log.group( | ||
`%c[clear-render] re-render #${this._renderCount}`, | ||
'color: #848d95;', | ||
this._componentName | ||
); | ||
this._printComparisonResult('props', this._propsChanges); | ||
this._printComparisonResult('state', this._stateChanges); | ||
this._log.groupEnd(); | ||
} | ||
|
||
_printComparisonResult(title, changes = []) { | ||
if (changes.length === 0) { | ||
return; | ||
} | ||
this._log.group(title); | ||
changes.forEach(this._printChange.bind(this)); | ||
this._log.groupEnd(); | ||
} | ||
|
||
_differenceBetweenObjects(oldObj = {}, nextObj = {}) { | ||
if (!nextObj || !oldObj) { | ||
return []; | ||
} | ||
let difference = []; | ||
Object.keys(nextObj).forEach(key => { | ||
if (nextObj[key] !== oldObj[key]) { | ||
const type = typeof nextObj[key]; | ||
difference.push({ | ||
key, | ||
type, | ||
oldValue: oldObj[key], | ||
nextValue: nextObj[key], | ||
}); | ||
} | ||
}); | ||
return difference; | ||
} | ||
|
||
_shallowCompareProps(prevProps, nextProps) { | ||
this._propsChanges = this._differenceBetweenObjects(prevProps, nextProps); | ||
} | ||
|
||
_shallowCompareState(prevState, nextState) { | ||
this._stateChanges = this._differenceBetweenObjects(prevState, nextState); | ||
} | ||
|
||
get _isFirstRender() { | ||
return !this._renderCount; | ||
} | ||
|
||
_incrementRenderCount() { | ||
this._renderCount++; | ||
} | ||
} | ||
|
||
export default Logger; |
Oops, something went wrong.