test-react-order.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import test from 'tape';
  2. import { CLIEngine, ESLint } from 'eslint';
  3. import eslintrc from '..';
  4. import reactRules from '../rules/react';
  5. import reactA11yRules from '../rules/react-a11y';
  6. const rules = {
  7. // It is okay to import devDependencies in tests.
  8. 'import/no-extraneous-dependencies': [2, { devDependencies: true }],
  9. // this doesn't matter for tests
  10. 'lines-between-class-members': 0,
  11. // otherwise we need some junk in our fixture code
  12. 'react/no-unused-class-component-methods': 0,
  13. };
  14. const cli = new (CLIEngine || ESLint)({
  15. useEslintrc: false,
  16. baseConfig: eslintrc,
  17. ...(CLIEngine ? { rules } : { overrideConfig: { rules } }),
  18. });
  19. async function lint(text) {
  20. // @see https://eslint.org/docs/developer-guide/nodejs-api.html#executeonfiles
  21. // @see https://eslint.org/docs/developer-guide/nodejs-api.html#executeontext
  22. const linter = CLIEngine ? cli.executeOnText(text) : await cli.lintText(text);
  23. return (CLIEngine ? linter.results : linter)[0];
  24. }
  25. function wrapComponent(body) {
  26. return `\
  27. import React from 'react';
  28. export default class MyComponent extends React.Component {
  29. /* eslint no-empty-function: 0, class-methods-use-this: 0 */
  30. ${body}}
  31. `;
  32. }
  33. test('validate react methods order', (t) => {
  34. t.test('make sure our eslintrc has React and JSX linting dependencies', (t) => {
  35. t.plan(2);
  36. t.deepEqual(reactRules.plugins, ['react']);
  37. t.deepEqual(reactA11yRules.plugins, ['jsx-a11y', 'react']);
  38. });
  39. t.test('passes a good component', async (t) => {
  40. const result = await lint(wrapComponent(`
  41. componentDidMount() {}
  42. handleSubmit() {}
  43. onButtonAClick() {}
  44. setFoo() {}
  45. getFoo() {}
  46. setBar() {}
  47. someMethod() {}
  48. renderDogs() {}
  49. render() { return <div />; }
  50. `));
  51. t.notOk(result.warningCount, 'no warnings');
  52. t.deepEquals(result.messages, [], 'no messages in results');
  53. t.notOk(result.errorCount, 'no errors');
  54. });
  55. t.test('order: when random method is first', async (t) => {
  56. const result = await lint(wrapComponent(`
  57. someMethod() {}
  58. componentDidMount() {}
  59. setFoo() {}
  60. getFoo() {}
  61. setBar() {}
  62. renderDogs() {}
  63. render() { return <div />; }
  64. `));
  65. t.ok(result.errorCount, 'fails');
  66. t.deepEqual(result.messages.map((msg) => msg.ruleId), ['react/sort-comp'], 'fails due to sort');
  67. });
  68. t.test('order: when random method after lifecycle methods', async (t) => {
  69. const result = await lint(wrapComponent(`
  70. componentDidMount() {}
  71. someMethod() {}
  72. setFoo() {}
  73. getFoo() {}
  74. setBar() {}
  75. renderDogs() {}
  76. render() { return <div />; }
  77. `));
  78. t.ok(result.errorCount, 'fails');
  79. t.deepEqual(result.messages.map((msg) => msg.ruleId), ['react/sort-comp'], 'fails due to sort');
  80. });
  81. t.test('order: when handler method with `handle` prefix after method with `on` prefix', async (t) => {
  82. const result = await lint(wrapComponent(`
  83. componentDidMount() {}
  84. onButtonAClick() {}
  85. handleSubmit() {}
  86. setFoo() {}
  87. getFoo() {}
  88. render() { return <div />; }
  89. `));
  90. t.ok(result.errorCount, 'fails');
  91. t.deepEqual(result.messages.map((msg) => msg.ruleId), ['react/sort-comp'], 'fails due to sort');
  92. });
  93. t.test('order: when lifecycle methods after event handler methods', async (t) => {
  94. const result = await lint(wrapComponent(`
  95. handleSubmit() {}
  96. componentDidMount() {}
  97. setFoo() {}
  98. getFoo() {}
  99. render() { return <div />; }
  100. `));
  101. t.ok(result.errorCount, 'fails');
  102. t.deepEqual(result.messages.map((msg) => msg.ruleId), ['react/sort-comp'], 'fails due to sort');
  103. });
  104. t.test('order: when event handler methods after getters and setters', async (t) => {
  105. const result = await lint(wrapComponent(`
  106. componentDidMount() {}
  107. setFoo() {}
  108. getFoo() {}
  109. handleSubmit() {}
  110. render() { return <div />; }
  111. `));
  112. t.ok(result.errorCount, 'fails');
  113. t.deepEqual(result.messages.map((msg) => msg.ruleId), ['react/sort-comp'], 'fails due to sort');
  114. });
  115. });