From d7cd02a45df3c5c4e3806deb7abfa098e72fd49d Mon Sep 17 00:00:00 2001 From: Valera Melnikov Date: Mon, 12 Feb 2024 18:59:10 +0300 Subject: [PATCH] fix: add testing library eslint rules (#31028) Added recommended rules for testing library ## Summary by CodeRabbit - **New Features** - Added `data-testid` attributes across various components for improved test identification. - **Tests** - Enhanced test cases with asynchronous handling (`async`/`await`) for more reliable user interaction simulations. - Transitioned to using `getByTestId` instead of `queryByTestId` for better assertion reliability in tests. - Added `await` before the `userEvent.click(el)` statement in the ChartWidget test file. - Updated the destructured variable names from `queryByTestId` to `getByTestId` in the DividerWidget test file for improved clarity. - Added an import for `screen` from "@testing-library/react" and updated element querying in the TabsWidget test file. - **Chores** - Updated ESLint configurations to include testing-library plugins and rules, improving code quality and consistency in test files. - Removed unnecessary `cleanup` function calls after tests, following best practices for test cleanup. --- app/client/.eslintrc.base.json | 9 + app/client/.eslintrc.js | 6 + app/client/package.json | 1 + .../widgets-old/src/Dropdown/index.tsx | 1 + .../components/Button/tests/Button.test.tsx | 8 +- .../Checkbox/tests/Checkbox.test.tsx | 6 +- .../tests/CheckboxGroup.test.tsx | 18 +- .../RadioGroup/tests/RadioGroup.test.tsx | 15 +- .../src/components/Text/tests/Text.test.tsx | 5 +- app/client/packages/dsl/.eslintrc.json | 3 +- app/client/packages/rts/.eslintrc.json | 3 +- .../AutoResizeTextArea.test.tsx | 2 + .../CodeEditor/CodeEditor.test.tsx | 2 + .../CodeEditor/index.test.tsx | 4 +- .../EditorContextProvider.test.tsx | 10 +- .../DynamicInputTextControl.test.tsx | 8 +- .../ColorPickerComponentV2.test.tsx | 112 ++++++------- .../IconSelectControl.test.tsx | 154 +++++++++--------- .../dropTarget/DragLayerComponent.test.tsx | 1 + .../FormGroup/Accordion.test.tsx | 2 +- .../src/pages/AppViewer/PrimaryCTA.test.tsx | 4 +- .../Libraries/__tests__/Installer.test.tsx | 7 +- .../GlobalHotKeys/GlobalHotKeys.test.tsx | 4 +- .../pages/Editor/IDE/EditorTabs/FileTabs.tsx | 2 +- .../Editor/MainContainerWidthToggles.test.tsx | 24 +-- .../gitSync/GitSettingsModal/index.test.tsx | 26 +-- .../QuickGitActions/BranchButton.test.tsx | 2 +- .../Tabs/__tests__/ConnectionSuccess.test.tsx | 10 +- .../gitSync/components/BetaTag.test.tsx | 2 +- .../gitSync/components/ConflictInfo.test.tsx | 6 +- .../gitSync/components/DefaultTag.test.tsx | 2 +- .../components/RemoteBranchList.test.tsx | 4 +- app/client/src/pages/setup/SetupForm.test.tsx | 8 +- .../ChartWidget/component/index.test.tsx | 2 +- .../DividerWidget/widget/index.test.tsx | 8 +- .../widgets/TabsWidget/widget/index.test.tsx | 7 +- app/client/yarn.lock | 14 +- 37 files changed, 273 insertions(+), 229 deletions(-) diff --git a/app/client/.eslintrc.base.json b/app/client/.eslintrc.base.json index b075c619bc..70975b0a86 100644 --- a/app/client/.eslintrc.base.json +++ b/app/client/.eslintrc.base.json @@ -16,12 +16,14 @@ "react-hooks", "sort-destructure-keys", "cypress", + "testing-library", "jest" ], "extends": [ "plugin:react/recommended", // Uses the recommended rules from @eslint-plugin-react "plugin:@typescript-eslint/recommended", "plugin:cypress/recommended", + "plugin:testing-library/react", // Note: Please keep this as the last config to make sure that this (and by extension our .prettierrc file) overrides all configuration above it // https://www.npmjs.com/package/eslint-plugin-prettier#recommended-configuration "plugin:prettier/recommended" @@ -35,6 +37,13 @@ "project": "./tsconfig.json" }, "rules": { + "testing-library/consistent-data-testid": [ + "warn", + { + "testIdPattern": "^t--[a-zA-Z0-9_.-]*$", + "testIdAttribute": ["data-testid"] + } + ], "@typescript-eslint/consistent-type-definitions": "error", "@typescript-eslint/prefer-nullish-coalescing": "error", "@typescript-eslint/promise-function-async": "error", diff --git a/app/client/.eslintrc.js b/app/client/.eslintrc.js index 82f4f8f653..30bd6ceda3 100644 --- a/app/client/.eslintrc.js +++ b/app/client/.eslintrc.js @@ -13,6 +13,12 @@ const baseNoRestrictedImports = const eslintConfig = { extends: ["./.eslintrc.base.json"], rules: { + "testing-library/no-container": "off", + "testing-library/no-node-access": "off", + "testing-library/no-debugging-utils": "off", + "testing-library/prefer-screen-queries": "off", + "testing-library/render-result-naming-convention": "off", + "testing-library/no-unnecessary-act": "off", "@typescript-eslint/prefer-nullish-coalescing": "off", "@typescript-eslint/strict-boolean-expressions": "off", "@typescript-eslint/no-explicit-any": "off", diff --git a/app/client/package.json b/app/client/package.json index e980462b11..9fc3c1faf7 100644 --- a/app/client/package.json +++ b/app/client/package.json @@ -329,6 +329,7 @@ "eslint-plugin-react-hooks": "^4.6.0", "eslint-plugin-sort-destructure-keys": "^1.5.0", "eslint-plugin-storybook": "^0.6.15", + "eslint-plugin-testing-library": "^6.2.0", "esm": "^3.2.25", "factory.ts": "^0.5.1", "husky": "^8.0.0", diff --git a/app/client/packages/design-system/widgets-old/src/Dropdown/index.tsx b/app/client/packages/design-system/widgets-old/src/Dropdown/index.tsx index b05c7c33f2..ee8cc53117 100644 --- a/app/client/packages/design-system/widgets-old/src/Dropdown/index.tsx +++ b/app/client/packages/design-system/widgets-old/src/Dropdown/index.tsx @@ -858,6 +858,7 @@ export function RenderDropdownOptions(props: DropdownOptionsProps) { { render(