Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions docs/recipes/react.md
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!
Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JavaScript => js

import React, { PropTypes } from 'react';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer import {fn} from 'module': without spaces.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

import { find } from 'lodash';

const FieldError = ({ errors, attribute, code, children }) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, without spaces is nicer imo.

Copy link
Member

Choose a reason for hiding this comment

The 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 = {
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JavaScript => js

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 => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test('title', t => { instead of assert

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t.is() instead of same()

});
```