feat: add rules for toBePressed, toBePartiallyPressed, toBePartiallyChecked matchers#419
feat: add rules for toBePressed, toBePartiallyPressed, toBePartiallyChecked matchers#419shamrt-v wants to merge 8 commits intotesting-library:mainfrom
Conversation
|
Yeah changing the configs is a breaking change, so please revert that - it would also be good if you can cherry-pick out the bug fix into its own PR since that should be landable on its own. Also just FYI we're currently blocked on releasing new versions until @benmonro gets the pipeline set back up (see #417), so while I'm happy to review PRs, I may hold off landing them depending on the changes to avoid having too many unreleased changes |
Allow attribute rules to skip specific values (e.g. 'mixed') so tri-state aria attributes are not incorrectly flagged. Apply to prefer-checked to stop suggesting not.toBeChecked() for aria-checked='mixed'.
Verify aria-checked='mixed' is not flagged by prefer-checked.
Suggest toBePressed()/not.toBePressed() instead of manual toHaveAttribute/toHaveProperty checks on aria-pressed. Excludes 'mixed' value (handled by prefer-partially-pressed).
Shared rule factory for suggesting partially-state matchers (toBePartiallyChecked, toBePartiallyPressed) when code checks a tri-state aria attribute for the 'mixed' value.
Suggest toBePartiallyPressed() instead of
toHaveAttribute('aria-pressed', 'mixed').
Suggest toBePartiallyChecked() instead of
toHaveAttribute('aria-checked', 'mixed'). Fills a pre-existing
gap—the matcher has been in jest-dom for years without a
corresponding lint rule.
New rules (prefer-pressed, prefer-partially-pressed, prefer-partially-checked) are in the 'all' config only, avoiding a breaking change for existing users.
9ca36e5 to
cafd6c7
Compare
Alrighty. Done: #420 |
|
Thanks, I've landed #420. For the "partially" support though I'm not sure if they need dedicated rules, since the point of them is "use this dedicated matcher, rather than a generic attribute check" - I don't really see a situation where someone would prefer using e.g. I wouldn't be surprised if it's more complicated to implement that due to the way this plugin uses functions to build its rules, but I'd be keen to see more of what it'd actually look like to go that route before deciding if we should add new rules instead. In the meantime, I'd be happy to add Either way fwiw new rules should generally have their own PRs unless they have a lot of overlap in their implementation, to make it easier to review and so that they have their own changelog entry. |
What:
Add three new ESLint rules for recent jest-dom matchers:
prefer-pressed— suggeststoBePressed()/not.toBePressed()instead of manualaria-pressedattribute checksprefer-partially-pressed— suggeststoBePartiallyPressed()instead oftoHaveAttribute('aria-pressed', 'mixed')prefer-partially-checked— suggeststoBePartiallyChecked()instead oftoHaveAttribute('aria-checked', 'mixed')All three rules are included in the
allconfig but excluded fromrecommendedto avoid breaking changes.Why:
jest-dom v6.7.0+ added
toBePressedandtoBePartiallyPressedmatchers, andtoBePartiallyCheckedhas existed for years — but eslint-plugin-jest-dom had no rules to guide users toward them.How:
prefer-pressedreuses the existingcreateBannedAttributeRulefactory (same pattern asprefer-checked)createPartiallyBannedAttributeRulefactory for the two "partially" rules, targeting only the"mixed"attribute value viatoHaveAttribute/toHavePropertyselectorsrecommendedconfig via anewRulesexclusion list insrc/index.jsNote
This PR depends on #420 (
fix/prefer-checked-mixed-value), which addsexcludeValuessupport tocreateBannedAttributeRule. Bothprefer-pressedandprefer-checkeduseexcludeValues: ["mixed"]to avoid incorrectly flaggingaria-pressed="mixed"/aria-checked="mixed". Merge #420 first, then rebase this branch.Checklist:
Testing Instructions
npm install && npm test— all 586 tests should passaria-pressed="mixed"is not flagged byprefer-pressed