1 | import test from 'tape';
|
2 | import { CLIEngine } from 'eslint';
|
3 | import eslintrc from '../';
|
4 | import reactRules from '../rules/react';
|
5 |
|
6 | const cli = new CLIEngine({
|
7 | useEslintrc: false,
|
8 | baseConfig: eslintrc,
|
9 |
|
10 |
|
11 | rules: { indent: 0 },
|
12 | });
|
13 |
|
14 | function lint(text) {
|
15 |
|
16 |
|
17 | const linter = cli.executeOnText(text);
|
18 | return linter.results[0];
|
19 | }
|
20 |
|
21 | function wrapComponent(body) {
|
22 | return `
|
23 | import React from 'react';
|
24 | export default class MyComponent extends React.Component {
|
25 | ${body}
|
26 | }
|
27 | `;
|
28 | }
|
29 |
|
30 | test('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 | });
|