-
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
Written a React recipe (with an example) #462
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# React testing | ||
|
||
With 0.10.0, AVA stopped including your `.babelrc` (for reasons outlined in [this PR](https://github.com/sindresorhus/ava/pull/398)), which means you can't use JSX in your test/spec files. Note that this doesn't apply to your source files, they can still be written in JSX! | ||
|
||
The solution is just to use React's regular JS rendering methods, like `React.createElement`. | ||
|
||
So here's an example component we'd like to test: | ||
|
||
```JavaScript | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
import React, { PropTypes } from 'react'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
import { find } from 'lodash'; | ||
|
||
const FieldError = ({ errors, attribute, code, children }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, without spaces is nicer imo. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
const result = find(errors, { attribute, code }); | ||
|
||
if (result) { | ||
return <span key={`error-${code}`} className="form__error">{ children }</span>; | ||
} | ||
|
||
/** | ||
* Stateless components can't return null for now. React 0.15 is supposed to | ||
* fix that, but for the time being use an empty <span> or <noscript> tag. | ||
* https://github.com/facebook/react/issues/5355 | ||
*/ | ||
return <span />; | ||
}; | ||
|
||
FieldError.propTypes = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessary for the demo |
||
errors: PropTypes.array.isRequired, | ||
attribute: PropTypes.string.isRequired, | ||
code: PropTypes.string.isRequired, | ||
children: PropTypes.string.isRequired, | ||
}; | ||
|
||
export default FieldError; | ||
``` | ||
|
||
And here is the test: | ||
|
||
```JavaScript | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
import test from 'ava'; | ||
import React from 'react'; | ||
import ReactDOMServer from 'react-dom/server'; | ||
import FieldError from '../../../js/components/form/FieldError.js'; | ||
|
||
test('FieldError shows when the error is in array', assert => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const errors = [{ attribute: 'email', code: '123' }]; | ||
|
||
/* | ||
The JSX way yould look something like this, but would return a syntax error. | ||
|
||
const field = ( | ||
<FieldError attribute="email" code="123" errors={ errors }> | ||
Error message | ||
</FieldError> | ||
); | ||
*/ | ||
|
||
const field = React.createElement(FieldError, { | ||
errors, | ||
attribute: 'email', | ||
code: '123', | ||
}, 'Error message'); | ||
|
||
const result = ReactDOMServer.renderToStaticMarkup(field); | ||
const expectation = '<span class="form__error">Error message</span>'; | ||
assert.same(result, expectation); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}); | ||
``` |
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.
I was under the impression that there is soon to be a release that allows you to specify a
.babelrc
to use or some way to configure babel yourself. Is this not true?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.
I think we are pretty close to agreement on how it should look. But the implementation is not written yet.
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.
In the interest of future-proofing this documentation, a reference to the relevant issue might be helpful here. Can't seem to find it right now though.
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.
Found it
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.
#448