UNPKG

2.13 kBJavaScriptView Raw
1import test from 'tape';
2import { CLIEngine } from 'eslint';
3import eslintrc from '../';
4import reactRules from '../rules/react';
5
6const cli = new CLIEngine({
7 useEslintrc: false,
8 baseConfig: eslintrc,
9
10 // This rule fails when executing on text.
11 rules: { indent: 0 },
12});
13
14function lint(text) {
15 // @see http://eslint.org/docs/developer-guide/nodejs-api.html#executeonfiles
16 // @see http://eslint.org/docs/developer-guide/nodejs-api.html#executeontext
17 const linter = cli.executeOnText(text);
18 return linter.results[0];
19}
20
21function wrapComponent(body) {
22 return `
23import React from 'react';
24export default class MyComponent extends React.Component {
25${body}
26}
27`;
28}
29
30test('validate react prop order', t => {
31 t.test('make sure our eslintrc has React and JSX linting dependencies', t => {
32 t.plan(1);
33 t.deepEqual(reactRules.plugins, ['jsx-a11y', 'react']);
34 });
35
36 t.test('passes a good component', t => {
37 t.plan(3);
38 const result = lint(wrapComponent(`
39 componentWillMount() {}
40 componentDidMount() {}
41 setFoo() {}
42 getFoo() {}
43 setBar() {}
44 someMethod() {}
45 renderDogs() {}
46 render() { return <div />; }
47`));
48
49 t.notOk(result.warningCount, 'no warnings');
50 t.notOk(result.errorCount, 'no errors');
51 t.deepEquals(result.messages, [], 'no messages in results');
52 });
53
54 t.test('order: when random method is first', t => {
55 t.plan(2);
56 const result = lint(wrapComponent(`
57 someMethod() {}
58 componentWillMount() {}
59 componentDidMount() {}
60 setFoo() {}
61 getFoo() {}
62 setBar() {}
63 renderDogs() {}
64 render() { return <div />; }
65`));
66
67 t.ok(result.errorCount, 'fails');
68 t.equal(result.messages[0].ruleId, 'react/sort-comp', 'fails due to sort');
69 });
70
71 t.test('order: when random method after lifecycle methods', t => {
72 t.plan(2);
73 const result = lint(wrapComponent(`
74 componentWillMount() {}
75 componentDidMount() {}
76 someMethod() {}
77 setFoo() {}
78 getFoo() {}
79 setBar() {}
80 renderDogs() {}
81 render() { return <div />; }
82`));
83
84 t.ok(result.errorCount, 'fails');
85 t.equal(result.messages[0].ruleId, 'react/sort-comp', 'fails due to sort');
86 });
87});