Skip to content

Commit

Permalink
Only strip outer div, fix and extend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrookes committed Oct 17, 2019
1 parent f2619bc commit bb08010
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/src/modules/components/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ function Demo(props) {

const match = useMediaQuery(theme => theme.breakpoints.up('sm'));

const jsx = getJsxPreview(demoData.raw || '', demoOptions);
const jsx = getJsxPreview(demoData.raw || '', demoOptions.defaultCodeOpen);
const showPreview =
!demoOptions.hideHeader && jsx !== demoData.raw && jsx.split(/\n/).length <= 20;

Expand Down
13 changes: 5 additions & 8 deletions docs/src/modules/utils/getJsxPreview.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
// docs/pages/components/material-icons.js doesn't provide the source
export default function getJsxPreview(code, demoOptions) {
export default function getJsxPreview(code, defaultCodeOpen) {
/* The regex matches the content of the return statement in the default export,
* stripping any wrapper divs:
*
* `export default.*`
* `\n return (\n` or `\n return `
* ` <div.*>\n` (optional)
* ` <div.*>\n` (optional)
* everything until:
* `\n </div>` (optional)
* `\n </div>` (optional)
* ` );\n}` or `;\n}`
*/
let jsx = code.match(
/export default .*(?:\n {2}return \(\n|\n {2}return )(?: {4}<div.*?>\n)?(?: {6}<div.*?>\n)?(.*?)(\n {6}<\/div>)?(\n {4}<\/div>)?(\n {2}\);\n}|;\n})/s,
/export default .*(?:\n {2}return \(\n|\n {2}return )(?: {4}<div.*?>\n)?(.*?)(\n {4}<\/div>)?(\n {2}\);\n}|;\n})/s,
);
/* Just the match, otherwise the full source if no match or disabled,
so as not to break the Collapse transition. */
jsx = jsx && demoOptions.defaultCodeOpen !== false ? jsx[1] : code;
// Just the match, otherwise the full source if either no match or preview disabled,
// so as not to break the Collapse transition.
jsx = jsx && defaultCodeOpen !== false ? jsx[1] : code;

// Remove leading spaces from each line
return jsx.split(/\n/).reduce(
Expand Down
65 changes: 63 additions & 2 deletions docs/src/modules/utils/getJsxPreview.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from 'chai';
import getJsxPreview from './getJsxPreview';

describe('getJsxPreview', () => {
it('should extract a preview', () => {
it('should extract one-line a preview', () => {
expect(
getJsxPreview(`
import React from 'react';
Expand All @@ -11,8 +11,69 @@ import Rating from '@material-ui/lab/Rating';
export default function HalfRating() {
return <Rating name="half-rating" value={2.5} precision={0.5} />;
}
`),
`, true),
).to.equal(`<Rating name="half-rating" value={2.5} precision={0.5} />
`);
});

it('should extract a multi-line preview', () => {
expect(
getJsxPreview(`
export default function UseWidth() {
return (
<ThemeProvider theme={theme}>
<MyComponent />
</ThemeProvider>
);
}
`, true),
).to.equal(`<ThemeProvider theme={theme}>
<MyComponent />
</ThemeProvider>
`);
});

it('should exclude an outer div', () => {
expect(
getJsxPreview(`
export default function UseWidth() {
return (
<div className={classes.root}>
<MyComponent />
</div>
);
}
`, true),
).to.equal(`<MyComponent />
`);
});

it('should return all if no match', () => {
expect(
getJsxPreview(`
export function UseWidth() {
return ( <MyComponent />;
}
`, true),
).to.equal(`
export function UseWidth() {
return ( <MyComponent />;
}
`);
});
it('should return all if defaultCodeOpen is false', () => {
expect(
getJsxPreview(`
export default function UseWidth() {
return <MyComponent />;
}
`, false),
).to.equal(`
export default function UseWidth() {
return <MyComponent />;
}
`);
});
});

0 comments on commit bb08010

Please sign in to comment.