-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Split new CodeEvaluator component out from Preview #976
Merged
sapegin
merged 8 commits into
styleguidist:master
from
kidkuro:feature/975-code-evaluator
May 14, 2018
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6220654
Fixes #975 - Split new CodeEvaluator component out from Preview
389c554
Merge branch 'master' into feature/975-code-evaluator
0d7c46d
Increasing code coverage of Preview
26ea351
Renamed CodeEvaluator to ReactExample
b349fb4
Merge branch 'master' into feature/975-code-evaluator
6083342
Renamed InitialStateComponent to StateHolder
358dffb
Merge branch 'master' into feature/975-code-evaluator
f35d1c9
Fixing snapshot
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 13 additions & 7 deletions
20
src/rsg-components/Preview/__snapshots__/Preview.spec.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { transform } from 'buble'; | ||
import Wrapper from 'rsg-components/Wrapper'; | ||
import splitExampleCode from '../../utils/splitExampleCode'; | ||
|
||
/* eslint-disable no-invalid-this, react/no-multi-comp */ | ||
|
||
const FragmentTag = React.Fragment ? 'React.Fragment' : 'div'; | ||
|
||
const compileCode = (code, config) => transform(code, config).code; | ||
const wrapCodeInFragment = code => `<${FragmentTag}>${code}</${FragmentTag}>;`; | ||
|
||
// Wrap everything in a React component to leverage the state management | ||
// of this component | ||
class StateHolder extends Component { | ||
static propTypes = { | ||
component: PropTypes.func.isRequired, | ||
initialState: PropTypes.object.isRequired, | ||
}; | ||
|
||
state = this.props.initialState; | ||
setStateBinded = this.setState.bind(this); | ||
|
||
render() { | ||
return this.props.component(this.state, this.setStateBinded); | ||
} | ||
} | ||
|
||
export default class ReactExample extends Component { | ||
static propTypes = { | ||
code: PropTypes.string.isRequired, | ||
evalInContext: PropTypes.func.isRequired, | ||
onError: PropTypes.func.isRequired, | ||
compilerConfig: PropTypes.object, | ||
}; | ||
static contextTypes = {}; | ||
|
||
shouldComponentUpdate(nextProps) { | ||
return this.props.code !== nextProps.code; | ||
} | ||
|
||
// Eval the code to extract the value of the initial state | ||
getExampleInitialState(compiledCode) { | ||
if (compiledCode.indexOf('initialState') === -1) { | ||
return {}; | ||
} | ||
|
||
return this.props.evalInContext(` | ||
var state = {}, initialState = {}; | ||
try { | ||
${compiledCode}; | ||
} catch (err) {} | ||
return initialState; | ||
`)(); | ||
} | ||
|
||
// Run example code and return the last top-level expression | ||
getExampleComponent(compiledCode) { | ||
return this.props.evalInContext(` | ||
var initialState = {}; | ||
${compiledCode} | ||
`); | ||
} | ||
|
||
compileCode(code) { | ||
try { | ||
const wrappedCode = code.trim().match(/^</) ? wrapCodeInFragment(code) : code; | ||
return compileCode(wrappedCode, this.props.compilerConfig); | ||
} catch (err) { | ||
if (this.props.onError) { | ||
this.props.onError(err); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
render() { | ||
const compiledCode = this.compileCode(this.props.code); | ||
if (!compiledCode) { | ||
return null; | ||
} | ||
|
||
const { head, example } = splitExampleCode(compiledCode); | ||
const initialState = this.getExampleInitialState(head); | ||
const exampleComponent = this.getExampleComponent(example); | ||
const wrappedComponent = ( | ||
<Wrapper onError={this.props.onError}> | ||
<StateHolder component={exampleComponent} initialState={initialState} /> | ||
</Wrapper> | ||
); | ||
return wrappedComponent; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import React from 'react'; | ||
import noop from 'lodash/noop'; | ||
import ReactExample from '../ReactExample'; | ||
|
||
const evalInContext = a => | ||
// eslint-disable-next-line no-new-func | ||
new Function('require', 'state', 'setState', 'const React = require("react");' + a).bind( | ||
null, | ||
require | ||
); | ||
|
||
it('should render code', () => { | ||
const actual = shallow( | ||
<ReactExample code={'<button>OK</button>'} evalInContext={evalInContext} onError={noop} /> | ||
); | ||
|
||
expect(actual).toMatchSnapshot(); | ||
}); | ||
|
||
it('should wrap code in Fragment when it starts with <', () => { | ||
const actual = mount( | ||
<div> | ||
<ReactExample code="<span /><span />" evalInContext={evalInContext} onError={noop} /> | ||
</div> | ||
); | ||
|
||
expect(actual.html()).toMatchSnapshot(); | ||
}); | ||
|
||
it('should handle errors', () => { | ||
const onError = jest.fn(); | ||
|
||
shallow(<ReactExample code={'<invalid code'} evalInContext={evalInContext} onError={onError} />); | ||
|
||
expect(onError).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should set initialState before the first render', () => { | ||
const code = ` | ||
initialState = {count:1}; | ||
<span>{state.count}</span> | ||
`; | ||
const actual = mount(<ReactExample code={code} evalInContext={evalInContext} onError={noop} />); | ||
expect(actual.html()).toMatchSnapshot(); | ||
}); | ||
|
||
it('should update state on setState', done => { | ||
const code = ` | ||
initialState = {count:1}; | ||
setTimeout(() => state.count === 1 && setState({count:2})); | ||
<button>{state.count}</button> | ||
`; | ||
const actual = mount(<ReactExample code={code} evalInContext={evalInContext} onError={noop} />); | ||
|
||
actual.find('button').simulate('click'); | ||
|
||
setTimeout(() => { | ||
try { | ||
expect(actual.html()).toMatchSnapshot(); | ||
done(); | ||
} catch (err) { | ||
done.fail(err); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, in the
handleError
function.