Skip to content

Commit

Permalink
Merge pull request #209 from lightingbeetle/fix/preview-html
Browse files Browse the repository at this point in the history
Fix/preview html
  • Loading branch information
jurajk authored Mar 4, 2020
2 parents 1f19f89 + d177dde commit bd632ae
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
5 changes: 4 additions & 1 deletion packages/styleguide/src/components/MDX/MDX.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export const MDXComponents = {
a: props => <Link {...props} />,
inlineCode: props => <Code {...props} />,
code: props => (
<CodeBlock language={props.className.replace(/language-/, '')} {...props} />
<CodeBlock
language={props.className && props.className.replace(/language-/, '')}
{...props}
/>
),
};

Expand Down
8 changes: 3 additions & 5 deletions packages/styleguide/src/components/Preview/CodeExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,15 @@ export default class CodeExample extends React.Component {
}

render() {
const { children, codeJSXOptions, codeTypes, theme, ...other } = this.props;
const { children, codeJSXOptions, codeTypes, ...other } = this.props;

let codeToShow;
switch (this.state.codePreviewType) {
case 'html':
codeToShow = pretty(
typeof children === 'string'
? unescape(children)
: renderToStaticMarkup({
...children,
props: { ...children.props, theme },
}),
: renderToStaticMarkup(children),
{
ocd: true,
}
Expand Down Expand Up @@ -214,6 +211,7 @@ export default class CodeExample extends React.Component {
<Code
inline={false}
ref={this.codeBlockRef}
data-testid={this.state.codePreviewType}
language={
this.state.codePreviewType === 'html'
? 'markup'
Expand Down
17 changes: 16 additions & 1 deletion packages/styleguide/src/components/Preview/Preview.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render } from '@testing-library/react';
import { render, fireEvent } from '@testing-library/react';

import Preview from '.';

Expand All @@ -8,4 +8,19 @@ describe('rendering', () => {
const { getByText } = render(<Preview>Preview</Preview>);
expect(getByText('Preview')).toBeInTheDocument();
});

it('HTML preview renders more than 1 child', () => {
const { getByText, getByTestId } = render(
<Preview>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</Preview>
);

fireEvent.click(getByText('SHOW CODE'));

fireEvent.click(getByText('HTML'));

expect(getByTestId('html')).toBeInTheDocument();
});
});

0 comments on commit bd632ae

Please sign in to comment.