From 2aaeb48498d99b7056b3208edf175940461264d8 Mon Sep 17 00:00:00 2001 From: Benjamim Sonntag Date: Fri, 5 Apr 2019 14:42:08 +0100 Subject: [PATCH] Update react hooks rules tests --- test/fixtures/correct.js | 31 +++++++++++++++++++++++++------ test/fixtures/incorrect.js | 36 +++++++++++++++++++++++++++++------- test/index.js | 14 +++++++++++--- 3 files changed, 65 insertions(+), 16 deletions(-) diff --git a/test/fixtures/correct.js b/test/fixtures/correct.js index 1883ea5..6b10b9e 100644 --- a/test/fixtures/correct.js +++ b/test/fixtures/correct.js @@ -46,6 +46,9 @@ function noop() { // Do nothing } +// Declare `React` and other react-related variables. +const React = null; + // `array-bracket-spacing`, `comma-spacing` and `no-multi-spaces`. noop(['bar', 'foo']); @@ -384,20 +387,36 @@ if (yoda === true) { yoda = false; } -// `react-hooks/rules-of-hooks`. -const useEffect = noop; +// `react-hooks/exhaustive-deps`. +const ExhaustiveDeps = ({ foo }) => { + React.useEffect(() => foo()); + React.useEffect(() => foo(), [foo]); + React.useMemo(() => foo(), [foo]); + React.useCallback(() => foo(), [foo]); + + const [bar, setBar] = React.useState(); + + React.useEffect(() => setBar(!bar), [bar]); + + return null; +}; + +noop(ExhaustiveDeps); +// `react-hooks/rules-of-hooks`. const RulesOfHooks = () => { - useEffect(noop); + React.useState(); return null; }; -noop(RulesOfHooks); +function useHook() { + React.useState(); +} -// `react/jsx-tag-spacing`. -const React = null; +noop(RulesOfHooks, useHook); +// `react/jsx-tag-spacing`. const TagSpacing = () => (
); diff --git a/test/fixtures/incorrect.js b/test/fixtures/incorrect.js index dacaa60..e5db0f8 100644 --- a/test/fixtures/incorrect.js +++ b/test/fixtures/incorrect.js @@ -3,6 +3,9 @@ function noop() { // Do nothing } +// Declare `React` and other react-related variables. +const React = null; + // `array-bracket-spacing`. noop([ 'bar', 'foo']); @@ -381,22 +384,41 @@ if (true === yoda) { yoda = false; } +// `react-hooks/exhaustive-deps`. +const ExhaustiveDeps = ({ foo }) => { + React.useEffect(() => foo(), []); + React.useMemo(() => foo); + React.useMemo(() => foo(), []); + React.useCallback(() => foo); + React.useCallback(() => foo(), []); + + return null; +}; + +noop(ExhaustiveDeps); + // `react-hooks/rules-of-hooks`. -const useEffect = noop; +const RulesOfHooks = ({ foo }) => { + if (foo) { + React.useState(); + } -const RulesOfHooks = () => { - if (Math.random() > 0.5) { - useEffect(noop); + for (let i = 0; i < 10; i++) { + React.useState(); } + setTimeout(() => React.useState(), 10); + return null; }; -noop(RulesOfHooks); +function notHook() { + React.useState(); +} + +noop(RulesOfHooks, notHook); // `react/jsx-tag-spacing`. -const React = null; - const TagSpacingAfterOpening = () => ( < div /> ); diff --git a/test/index.js b/test/index.js index f0ace22..2cc0a73 100644 --- a/test/index.js +++ b/test/index.js @@ -62,12 +62,12 @@ describe('eslint-config-seegno', () => { 'jest/no-disabled-tests', 'jest/no-disabled-tests', 'jest/no-disabled-tests', - 'mocha/no-exclusive-tests', - 'jest/no-focused-tests', 'jest/no-focused-tests', 'mocha/no-exclusive-tests', + 'jest/no-focused-tests', 'mocha/no-exclusive-tests', 'jest/no-focused-tests', + 'mocha/no-exclusive-tests', 'jest/no-focused-tests', 'jest/no-focused-tests', 'jest/no-identical-title', @@ -123,13 +123,21 @@ describe('eslint-config-seegno', () => { 'spaced-comment', 'sql-template/no-unsafe-query', 'switch-case/newline-between-switch-case', - 'no-fallthrough', 'switch-case/newline-between-switch-case', + 'no-fallthrough', 'template-curly-spacing', 'template-curly-spacing', 'wrap-iife', 'sort-destructure-keys/sort-destructure-keys', 'yoda', + 'react-hooks/exhaustive-deps', + 'react-hooks/exhaustive-deps', + 'react-hooks/exhaustive-deps', + 'react-hooks/exhaustive-deps', + 'react-hooks/exhaustive-deps', + 'react-hooks/rules-of-hooks', + 'react-hooks/rules-of-hooks', + 'react-hooks/rules-of-hooks', 'react-hooks/rules-of-hooks', 'react/jsx-tag-spacing', 'react/jsx-tag-spacing',