|
| 1 | +/** |
| 2 | + * @fileoverview prefer toBePartiallyChecked over checking aria-checked for mixed state |
| 3 | + */ |
| 4 | + |
| 5 | +import { RuleTester } from "eslint"; |
| 6 | +import * as rule from "../../../rules/prefer-partially-checked"; |
| 7 | + |
| 8 | +const ruleTester = new RuleTester({ |
| 9 | + parserOptions: { ecmaVersion: 2015, sourceType: "module" }, |
| 10 | +}); |
| 11 | + |
| 12 | +ruleTester.run("prefer-partially-checked", rule, { |
| 13 | + valid: [ |
| 14 | + `expect(element).toBePartiallyChecked()`, |
| 15 | + `expect(element).not.toBePartiallyChecked()`, |
| 16 | + `const el = screen.getByText("foo"); expect(el).toBePartiallyChecked()`, |
| 17 | + `const el = screen.getByText("foo"); expect(el).toHaveAttribute("aria-checked", "true")`, |
| 18 | + `const el = screen.getByText("foo"); expect(el).toHaveAttribute("aria-checked", "false")`, |
| 19 | + `const el = screen.getByText("foo"); expect(el).toHaveAttribute("aria-checked")`, |
| 20 | + `const el = screen.getByText("foo"); expect(el).toHaveProperty("aria-checked", "true")`, |
| 21 | + `const el = screen.getByText("foo"); expect(el).not.toHaveAttribute("aria-checked")`, |
| 22 | + `const el = foo.bar(); expect(el).toHaveAttribute("aria-checked", "mixed")`, |
| 23 | + `const el = foo.bar(); expect(el).toHaveProperty("aria-checked", "mixed")`, |
| 24 | + ], |
| 25 | + invalid: [ |
| 26 | + { |
| 27 | + code: `const el = screen.getByText("foo"); expect(el).toHaveAttribute("aria-checked", "mixed")`, |
| 28 | + errors: [ |
| 29 | + { |
| 30 | + message: `Use toBePartiallyChecked() instead of toHaveAttribute('aria-checked', 'mixed')`, |
| 31 | + }, |
| 32 | + ], |
| 33 | + output: `const el = screen.getByText("foo"); expect(el).toBePartiallyChecked()`, |
| 34 | + }, |
| 35 | + { |
| 36 | + code: `const el = screen.getByText("foo"); expect(el).toHaveProperty("aria-checked", "mixed")`, |
| 37 | + errors: [ |
| 38 | + { |
| 39 | + message: `Use toBePartiallyChecked() instead of toHaveProperty('aria-checked', 'mixed')`, |
| 40 | + }, |
| 41 | + ], |
| 42 | + output: `const el = screen.getByText("foo"); expect(el).toBePartiallyChecked()`, |
| 43 | + }, |
| 44 | + { |
| 45 | + code: `expect(getByText("foo")).toHaveAttribute("aria-checked", "mixed")`, |
| 46 | + errors: [ |
| 47 | + { |
| 48 | + message: `Use toBePartiallyChecked() instead of toHaveAttribute('aria-checked', 'mixed')`, |
| 49 | + }, |
| 50 | + ], |
| 51 | + output: `expect(getByText("foo")).toBePartiallyChecked()`, |
| 52 | + }, |
| 53 | + { |
| 54 | + code: `expect(getByRole("checkbox")).toHaveAttribute("aria-checked", "mixed")`, |
| 55 | + errors: [ |
| 56 | + { |
| 57 | + message: `Use toBePartiallyChecked() instead of toHaveAttribute('aria-checked', 'mixed')`, |
| 58 | + }, |
| 59 | + ], |
| 60 | + output: `expect(getByRole("checkbox")).toBePartiallyChecked()`, |
| 61 | + }, |
| 62 | + { |
| 63 | + code: `const el = screen.getByText("foo"); expect(el).not.toHaveAttribute("aria-checked", "mixed")`, |
| 64 | + errors: [ |
| 65 | + { |
| 66 | + message: `Use not.toBePartiallyChecked() instead of not.toHaveAttribute('aria-checked', 'mixed')`, |
| 67 | + }, |
| 68 | + ], |
| 69 | + output: `const el = screen.getByText("foo"); expect(el).not.toBePartiallyChecked()`, |
| 70 | + }, |
| 71 | + { |
| 72 | + code: `const el = screen.getByText("foo"); expect(el).not.toHaveProperty("aria-checked", "mixed")`, |
| 73 | + errors: [ |
| 74 | + { |
| 75 | + message: `Use not.toBePartiallyChecked() instead of not.toHaveProperty('aria-checked', 'mixed')`, |
| 76 | + }, |
| 77 | + ], |
| 78 | + output: `const el = screen.getByText("foo"); expect(el).not.toBePartiallyChecked()`, |
| 79 | + }, |
| 80 | + { |
| 81 | + code: `expect(getByText("foo")).not.toHaveAttribute("aria-checked", "mixed")`, |
| 82 | + errors: [ |
| 83 | + { |
| 84 | + message: `Use not.toBePartiallyChecked() instead of not.toHaveAttribute('aria-checked', 'mixed')`, |
| 85 | + }, |
| 86 | + ], |
| 87 | + output: `expect(getByText("foo")).not.toBePartiallyChecked()`, |
| 88 | + }, |
| 89 | + ], |
| 90 | +}); |
0 commit comments