Skip to content

Commit

Permalink
Tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriyyakym committed Oct 17, 2018
1 parent 05df37e commit 04f956f
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"lint:fix": "eslint . --ext .js --fix",
"test": "jest --no-cache --coverage",
"test:debug": "node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand --no-cache --watch",
"test:watch": "jest --no-cache --watch --coverage"
"test:watch": "jest --no-cache --watch"
},
"betterScripts": {
"analyze-bundle": {
Expand Down
19 changes: 19 additions & 0 deletions src/__tests__/data/connect/input/component-and-class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';

class NotAClass {
constructor(a, b) {
this.a = a;
this.b = b;
this.sum = this.sum.bind(this);
}

sum() {
return this.a + this.b;
}
}

const ButtonComponent = (props) => (
<div>Button</div>
);

export default ButtonComponent;
28 changes: 28 additions & 0 deletions src/__tests__/data/connect/output/component-and-class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';
import { connect } from 'react-redux';

class NotAClass {
constructor(a, b) {
this.a = a;
this.b = b;
this.sum = this.sum.bind(this);
}

sum() {
return this.a + this.b;
}
}

const ButtonComponent = (props) => (
<div>Button</div>
);

const mapStateToProps = (state) => ({

});

const mapDispatchToProps = {

};

export default connect(mapStateToProps, mapDispatchToProps)(ButtonComponent);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
import PropTypes from 'prop-types';
import { withSomething } from 'react-nothing';

const ButtonComponent = ({ name }) => (
<div>
{name}
</div>
);

ButtonComponent.propTypes = {
name: PropTypes.string
};

const someGetter = (store) => store.path.value;

export const Button = withSomething(someGetter, ButtonComponent);
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';
import PropTypes from 'prop-types';
import { withSomething } from 'react-nothing';
import { withNothing } from 'with-nothing';

const ButtonComponent = ({ name }) => (
<div>
{name}
</div>
);

ButtonComponent.propTypes = {
name: PropTypes.string
};

const someGetter = (store) => store.path.value;

export const Button = withSomething(someGetter, withNothing(ButtonComponent));
30 changes: 30 additions & 0 deletions src/__tests__/transformations/wrap-component.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const parser = require('../../utils/parser');
const { readTransformationsFile } = require('../test-utils');
const wrapComponent = require('../../transformations/wrap-component');
const settings = require('../../settings');
Expand Down Expand Up @@ -209,4 +210,33 @@ describe('transformation:wrap-component', () => {
});
expect(result).toBe(output);
});

it('should wrap in hoc and leave rest arguments untouched', () => {
const input = readInputFile('button15');
const output = readOutputFile('button15');
const result = wrapComponent(input, undefined, {
name: 'withNothing',
import: {
module: 'with-nothing',
subImports: [
{ name: 'withNothing' }
]
}
});
expect(result).toBe(output);
});

it('should wrap in hoc and invoke with function argument', () => {
const input = 'export default () => (<div>123</div>);';
const output = [
'const Component = () => (<div>123</div>);',
'export default hoc(test, (a, b) => a + b)(Component);'
].join('\n\n');
const arrowAst = parser.parse('(a, b) => a + b;').program.body[0].expression;
const result = wrapComponent(input, undefined, {
name: 'hoc',
invoke: [ 'test', arrowAst ]
})
expect(result).toEqual(output);
});
});
3 changes: 1 addition & 2 deletions src/refactorings/toggle-with-router-hoc/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { isIdentifier } = require('@babel/types');
const FunctionComponentCompatibleRefactoring = require('../../model/function-component-compatible-refactoring');
const parser = require('../../utils/parser');
const ComponentExportDetails = require('../../utils/component-export-details');
const wrapComponent = require('../../transformations/wrap-component');
const unwrapComponent = require('../../transformations/unwrap-component');
Expand All @@ -17,7 +16,7 @@ class ToggleWithRouterHoc extends FunctionComponentCompatibleRefactoring {
return true;
}

toggleWithRouterHoc(code, ast = parser.parse(code)) {
toggleWithRouterHoc(code, ast) {
const details = new ComponentExportDetails(ast).getDetails();
const { outermostHocPath } = details;
let isWrapped = false;
Expand Down
19 changes: 9 additions & 10 deletions src/transformations/wrap-component/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const parser = require('../../utils/parser');
const settings = require('../../settings');
const { getOutermostCallExpressionPath } = require('../../utils/ast');

const findComponentScopePath = ({ arrowComponentDeclaration, classComponentPath, componentExportPath }) => {
const componentPath = arrowComponentDeclaration || classComponentPath || componentExportPath;
const findComponentScopePath = ({ arrowComponentDeclaration, classComponentPath }) => {
const componentPath = arrowComponentDeclaration || classComponentPath;
return componentPath.findParent(
({ node }) => isBlockStatement(node) || isProgram(node)
).node;
Expand Down Expand Up @@ -66,12 +66,11 @@ const getNewComponentName = (details, componentScope) => {
return originalComponentName;
}

const preferableComponentName = originalComponentName || defaultComponentName;
return findNameFromPotential([
componentNameCollisionPattern.replace('${name}', preferableComponentName),
componentNameCollisionPattern.replace('${name}', originalComponentName),
defaultComponentName,
componentNameCollisionPattern.replace('${name}', defaultComponentName),
componentScope.generateUidIdentifier(originalComponentName || defaultComponentName)
componentScope.generateUidIdentifier(originalComponentName)
]);
};

Expand Down Expand Up @@ -155,12 +154,12 @@ const createComponentWrappersAst = (ast, details, nodeToWrap, { invoke, name, ou
};

module.exports = {
findComponentScopePath,
appendNode,
getNewComponentName,
createBodylessArrowFunctionBlock,
createComponentWrappersAst,
createExportAst,
findComponentScopePath,
getNewComponentName,
removeExport,
removeExportAndSetComponentName,
createBodylessArrowFunctionBlock,
createComponentWrappersAst
removeExportAndSetComponentName
};

0 comments on commit 04f956f

Please sign in to comment.