Skip to content

Commit

Permalink
fix: Use checked property of toBeChecked when it is not a boolean…
Browse files Browse the repository at this point in the history
… literal

Fixes #205
  • Loading branch information
mskelton committed Feb 3, 2024
1 parent 3a8f674 commit 3563148
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/rules/prefer-web-first-assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { parseExpectCall } from '../utils/parseExpectCall';
type MethodConfig = {
inverse?: string;
matcher: string;
prop?: string;
type: 'boolean' | 'string';
};

Expand All @@ -16,7 +17,11 @@ const methods: Record<string, MethodConfig> = {
},
innerText: { matcher: 'toHaveText', type: 'string' },
inputValue: { matcher: 'toHaveValue', type: 'string' },
isChecked: { matcher: 'toBeChecked', type: 'boolean' },
isChecked: {
matcher: 'toBeChecked',
prop: 'checked',
type: 'boolean',
},
isDisabled: {
inverse: 'toBeEnabled',
matcher: 'toBeDisabled',
Expand Down Expand Up @@ -149,6 +154,15 @@ export default {
fixes.push(fixer.remove(matcherArg));
}

// Add the prop argument if needed
else if (methodConfig.prop && matcherArg) {
const propArg = methodConfig.prop;
const variable = getStringValue(matcherArg);
const args = `{ ${propArg}: ${variable} }`;

fixes.push(fixer.replaceText(matcherArg, args));
}

// Add the new matcher arguments if needed
const hasOtherArgs = !!methodArgs.filter(
(arg) => !isBooleanLiteral(arg),
Expand Down
32 changes: 32 additions & 0 deletions test/spec/prefer-web-first-assertions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,21 @@ runRuleTester('prefer-web-first-assertions', rule, {
],
output: test('await expect(page.locator("howdy")).toBeChecked()'),
},
{
code: test('expect(await page.locator("howdy").isChecked()).toBe(foo)'),
errors: [
{
column: 28,
data: { matcher: 'toBeChecked', method: 'isChecked' },
endColumn: 75,
line: 1,
messageId: 'useWebFirstAssertion',
},
],
output: test(
'await expect(page.locator("howdy")).toBeChecked({ checked: foo })',
),
},
{
code: test(
'expect(await page.locator("howdy").isChecked()).toBeTruthy()',
Expand Down Expand Up @@ -345,6 +360,23 @@ runRuleTester('prefer-web-first-assertions', rule, {
],
output: test('await expect(page.locator("howdy")).not.toBeChecked()'),
},
{
code: test(
'expect(await page.locator("howdy").isChecked()).not.toBe(bar)',
),
errors: [
{
column: 28,
data: { matcher: 'toBeChecked', method: 'isChecked' },
endColumn: 75,
line: 1,
messageId: 'useWebFirstAssertion',
},
],
output: test(
'await expect(page.locator("howdy")).not.toBeChecked({ checked: bar })',
),
},
{
code: test('expect(await page.locator("howdy").isChecked()).toBe(false)'),
errors: [
Expand Down

0 comments on commit 3563148

Please sign in to comment.