Skip to content

Commit 91af417

Browse files
author
Thomas Bertet
committed
use Enzyme to use React lifecycles & really test sortObject fix
1 parent ab1b6a4 commit 91af417

File tree

5 files changed

+283
-70
lines changed

5 files changed

+283
-70
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
"babel-register": "6.26.0",
4848
"conventional-changelog-cli": "2.0.5",
4949
"doctoc": "1.3.1",
50+
"enzyme": "^3.7.0",
51+
"enzyme-adapter-react-16": "^1.6.0",
5052
"eslint": "5.6.1",
5153
"eslint-config-algolia": "13.2.3",
5254
"eslint-config-prettier": "3.1.0",
@@ -82,5 +84,8 @@
8284
"dependencies": {
8385
"is-plain-object": "2.0.4",
8486
"stringify-object": "3.2.2"
87+
},
88+
"jest": {
89+
"setupTestFrameworkScriptFile": "<rootDir>tests/setupTests.js"
8590
}
8691
}

src/index.spec.js

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,67 +1087,4 @@ describe('reactElementToJSXString(ReactElement)', () => {
10871087
)
10881088
).toEqual(`<div render={<><div /><div /></>} />`);
10891089
});
1090-
1091-
it('should not cause recursive loop when prop object contains an element', () => {
1092-
const FunctionComp = () => {
1093-
return <span>hello func</span>;
1094-
};
1095-
1096-
class ClassComp extends React.Component {
1097-
render() {
1098-
return <span>hello class</span>;
1099-
}
1100-
}
1101-
1102-
class MainComp extends React.Component {
1103-
render() {
1104-
return <div {...this.props} />;
1105-
}
1106-
}
1107-
1108-
// Simple test, one level of nesting.
1109-
const elementToTest = (
1110-
<MainComp
1111-
title={{ comp: <FunctionComp /> }}
1112-
subtitle={{ anotherComp: <ClassComp /> }}
1113-
/>
1114-
);
1115-
1116-
const jsxString = reactElementToJSXString(elementToTest);
1117-
expect(jsxString).toBe(`<MainComp
1118-
subtitle={{
1119-
anotherComp: <ClassComp />
1120-
}}
1121-
title={{
1122-
comp: <FunctionComp />
1123-
}}
1124-
/>`);
1125-
1126-
// Deep nested component inside props.
1127-
const nestedElementToTest = (
1128-
<MainComp
1129-
title={{
1130-
comp: (
1131-
<MainComp
1132-
some={{
1133-
content: (
1134-
<ClassComp other={{ nested: { comp: <FunctionComp /> } }} />
1135-
),
1136-
}}
1137-
/>
1138-
),
1139-
}}
1140-
subtitle={{ anotherComp: <ClassComp /> }}
1141-
/>
1142-
);
1143-
const nestedJsxString = reactElementToJSXString(nestedElementToTest);
1144-
expect(nestedJsxString).toBe(`<MainComp
1145-
subtitle={{
1146-
anotherComp: <ClassComp />
1147-
}}
1148-
title={{
1149-
comp: <MainComp some={{content: <ClassComp other={{nested: {comp: <FunctionComp />}}} />}} />
1150-
}}
1151-
/>`);
1152-
});
11531090
});

tests/owner/owner.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* eslint-disable react/prop-types , no-console */
2+
import React, { Component } from 'react';
3+
import reactElementToJSXString from '../../src';
4+
5+
import { mount } from 'enzyme';
6+
7+
const Test = () => <div>Test</div>;
8+
9+
const Container = ({ title: { component } }) => <div>{component}</div>;
10+
11+
class App extends Component {
12+
render() {
13+
const inside = <Container title={{ component: <Test /> }} />;
14+
15+
const insideString = reactElementToJSXString(inside);
16+
17+
return (
18+
<div>
19+
{insideString}
20+
21+
<div id="hello" />
22+
23+
<p>Start editing to see some magic happen :)</p>
24+
</div>
25+
);
26+
}
27+
}
28+
29+
describe('reactElementToJSXString', () => {
30+
it('should not cause recursive loop when prop object contains an element', () => {
31+
expect(mount(<App />).find('#hello')).toHaveLength(1);
32+
});
33+
});

tests/setupTests.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Enzyme from 'enzyme';
2+
import Adapter from 'enzyme-adapter-react-16';
3+
4+
Enzyme.configure({ adapter: new Adapter() });

0 commit comments

Comments
 (0)