Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
Revert "add tests infrastructure and simple test"
Browse files Browse the repository at this point in the history
This reverts commit 9870ea3.
  • Loading branch information
Edgar committed Feb 26, 2019
1 parent 9870ea3 commit 48d3e16
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 232 deletions.
14 changes: 14 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"presets": [
"es2015"
],
"plugins": [
["transform-es2015-modules-umd", {
"globals": {
"src/index": "clearRender"
},
"exactGlobals": true
}]
],
"moduleId": "src/index"
}
3 changes: 0 additions & 3 deletions .babelrc.js

This file was deleted.

21 changes: 4 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,14 @@
"render"
],
"devDependencies": {
"@babel/core": "*",
"@babel/preset-env": "*",
"@babel/preset-react": "*",
"babel-jest": "*",
"enzyme": "*",
"enzyme-adapter-react-16": "*",
"jest": "*",
"react": "^16.4.0",
"react-dom": "^16.4.0"
"babel-cli": "^6.26.0",
"babel-plugin-transform-es2015-modules-umd": "6.24.1",
"babel-preset-es2015": "^6.24.1"
},
"main": "build/index.js",
"scripts": {
"build": "babel src/index.js --no-babelrc --presets es2015 --out-file build/index.js",
"build-umd": "babel src/index.js --out-file build/umd/index.js",
"test": "jest",
"format": "prettier --single-quote --trailing-comma es5 --write \"src/**/*.js\""
},
"jest": {
"testPathIgnorePatterns": [
"<rootDir>/src/__tests__/components/"
]
"build-umd": "babel src/index.js --out-file build/umd/index.js"
},
"repository": {
"type": "git",
Expand Down
27 changes: 0 additions & 27 deletions src/__tests__/components/SimpleComponent.js

This file was deleted.

24 changes: 0 additions & 24 deletions src/__tests__/patch.spec.js

This file was deleted.

134 changes: 121 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,118 @@
import patch from './patch';
class Logger {
constructor(componentName) {
this._componentName = componentName;
this._propsChanges = [];
this._stateChanges = [];
this._renderCount = 0;
}

init() {
console.log('%c[clear-render] init for', 'color: #848d95;', this._componentName);
console.log('%c[clear-render] render', 'color: #848d95;', this._componentName);
}

_printChange(change) {
if (typeof change === 'object') {
console.groupCollapsed(`${change.key} %c [${change.type}]`, 'color: #848d95; font-style: italic;');
console.log('%c old ', 'background: #ff6347; color: #fff', change.oldValue);
console.log('%c new ', 'background: #5fba7d; color: #fff', change.nextValue);
console.groupEnd();
} else {
console.log(change);
}
}

printComparisonsResults() {
console.group(`%c[clear-render] re-render #${this._renderCount}`, 'color: #848d95;', this._componentName);
this._printComparisonResult('props', this._propsChanges);
this._printComparisonResult('state', this._stateChanges);
console.groupEnd();
}

_printComparisonResult(title, changes = []) {
if (changes.length === 0) {
return;
}
console.group(title);
changes.forEach(this._printChange);
console.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;
}

export default (...args) => {
incrementRenderCount() {
this._renderCount++;
}
};

const wrap = (ComponentClass, forcedDisplayedName) => {
const componentClassName = ComponentClass.name;

const logger = new Logger(forcedDisplayedName || componentClassName);

const originalRender = ComponentClass.prototype.render;
ComponentClass.prototype.render = function () {
if (logger.isFirstRender) {
logger.init();
}

logger.incrementRenderCount();

if (originalRender) {
return originalRender.call(this);
} else {
return null;
}
}

const originalСomponentDidUpdate = ComponentClass.prototype.componentDidUpdate;
ComponentClass.prototype.componentDidUpdate = function (...args) {
const [prevProps, prevState] = args;
const nextProps = this.props;
const nextState = this.state;

logger.shallowCompareState(prevState, nextState);
logger.shallowCompareProps(prevProps, nextProps);
logger.printComparisonsResults();

if (originalСomponentDidUpdate) {
return originalСomponentDidUpdate.call(this, ...args);
}
}

return ComponentClass;
};

module.exports = function (...args) {
if (args.length > 2) {
console.error('[clear-render] Error: Too many arguments');
return null;
Expand All @@ -19,15 +131,13 @@ export default (...args) => {

switch (type) {
case 'symbol':
case 'string':
{
forcedDisplayedName = arg;
}
case 'string': {
forcedDisplayedName = arg;
}
break;
default:
{
ComponentClass = arg;
}
default: {
ComponentClass = arg;
}
break;
}
});
Expand All @@ -37,7 +147,5 @@ export default (...args) => {
return null;
}

const name = forcedDisplayedName || ComponentClass.name;

return patch(ComponentClass, name, console);
return wrap(ComponentClass, forcedDisplayedName);
};
116 changes: 0 additions & 116 deletions src/logger.js

This file was deleted.

Loading

0 comments on commit 48d3e16

Please sign in to comment.