Skip to content

Commit 3132216

Browse files
committed
Add allTextContets to the array of locators
1 parent 12b0832 commit 3132216

File tree

2 files changed

+252
-0
lines changed

2 files changed

+252
-0
lines changed

src/rules/prefer-web-first-assertions.test.ts

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,257 @@ runRuleTester('prefer-web-first-assertions', rule, {
529529
`),
530530
},
531531

532+
// allTextContents
533+
{
534+
code: test('expect(await foo.allTextContents()).toBe("bar")'),
535+
errors: [
536+
{
537+
column: 28,
538+
data: { matcher: 'toHaveText', method: 'allTextContents' },
539+
endColumn: 63,
540+
line: 1,
541+
messageId: 'useWebFirstAssertion',
542+
},
543+
],
544+
output: test('await expect(foo).toHaveText("bar")'),
545+
},
546+
{
547+
code: test('expect(await foo.allTextContents()).not.toBe("bar")'),
548+
errors: [
549+
{
550+
column: 28,
551+
data: { matcher: 'toHaveText', method: 'allTextContents' },
552+
endColumn: 63,
553+
line: 1,
554+
messageId: 'useWebFirstAssertion',
555+
},
556+
],
557+
output: test('await expect(foo).not.toHaveText("bar")'),
558+
},
559+
{
560+
code: test('expect(await foo.allTextContents()).toEqual("bar")'),
561+
errors: [
562+
{
563+
column: 28,
564+
data: { matcher: 'toHaveText', method: 'allTextContents' },
565+
endColumn: 63,
566+
line: 1,
567+
messageId: 'useWebFirstAssertion',
568+
},
569+
],
570+
output: test('await expect(foo).toHaveText("bar")'),
571+
},
572+
{
573+
code: test('expect.soft(await foo.allTextContents()).toBe("bar")'),
574+
errors: [
575+
{
576+
column: 28,
577+
data: { matcher: 'toHaveText', method: 'allTextContents' },
578+
endColumn: 68,
579+
line: 1,
580+
messageId: 'useWebFirstAssertion',
581+
},
582+
],
583+
output: test('await expect.soft(foo).toHaveText("bar")'),
584+
},
585+
{
586+
code: test('expect["soft"](await foo.allTextContents()).not.toEqual("bar")'),
587+
errors: [
588+
{
589+
column: 28,
590+
data: { matcher: 'toHaveText', method: 'allTextContents' },
591+
endColumn: 71,
592+
line: 1,
593+
messageId: 'useWebFirstAssertion',
594+
},
595+
],
596+
output: test('await expect["soft"](foo).not.toHaveText("bar")'),
597+
},
598+
{
599+
code: test(`
600+
const fooLocator = page.locator('.fooClass');
601+
const fooLocatorText = await fooLocator.allTextContents();
602+
expect(fooLocatorText).toEqual('foo');
603+
`),
604+
errors: [
605+
{
606+
column: 9,
607+
data: { matcher: 'toHaveText', method: 'allTextContents' },
608+
endColumn: 31,
609+
line: 4,
610+
messageId: 'useWebFirstAssertion',
611+
},
612+
],
613+
output: test(`
614+
const fooLocator = page.locator('.fooClass');
615+
const fooLocatorText = fooLocator;
616+
await expect(fooLocatorText).toHaveText('foo');
617+
`),
618+
},
619+
{
620+
code: test(`
621+
const fooLocator = page.locator('.fooClass');
622+
let fooLocatorText = await fooLocator.allTextContents();
623+
expect(fooLocatorText).toEqual('foo');
624+
fooLocatorText = 'foo';
625+
expect(fooLocatorText).toEqual('foo');
626+
`),
627+
errors: [
628+
{
629+
column: 9,
630+
data: { matcher: 'toHaveText', method: 'allTextContents' },
631+
endColumn: 31,
632+
line: 4,
633+
messageId: 'useWebFirstAssertion',
634+
},
635+
],
636+
output: test(`
637+
const fooLocator = page.locator('.fooClass');
638+
let fooLocatorText = fooLocator;
639+
await expect(fooLocatorText).toHaveText('foo');
640+
fooLocatorText = 'foo';
641+
expect(fooLocatorText).toEqual('foo');
642+
`),
643+
},
644+
{
645+
code: test(`
646+
let fooLocatorText;
647+
const fooLocator = page.locator('.fooClass');
648+
fooLocatorText = 'Unrelated';
649+
fooLocatorText = await fooLocator.allTextContents();
650+
expect(fooLocatorText).toEqual('foo');
651+
`),
652+
errors: [
653+
{
654+
column: 9,
655+
data: { matcher: 'toHaveText', method: 'allTextContents' },
656+
endColumn: 31,
657+
line: 6,
658+
messageId: 'useWebFirstAssertion',
659+
},
660+
],
661+
output: test(`
662+
let fooLocatorText;
663+
const fooLocator = page.locator('.fooClass');
664+
fooLocatorText = 'Unrelated';
665+
fooLocatorText = fooLocator;
666+
await expect(fooLocatorText).toHaveText('foo');
667+
`),
668+
},
669+
{
670+
code: test(`
671+
let fooLocatorText;
672+
let fooLocatorText2;
673+
const fooLocator = page.locator('.fooClass');
674+
fooLocatorText = await fooLocator.allTextContents();
675+
fooLocatorText2 = await fooLocator.allTextContents();
676+
expect(fooLocatorText).toEqual('foo');
677+
`),
678+
errors: [
679+
{
680+
column: 9,
681+
data: { matcher: 'toHaveText', method: 'allTextContents' },
682+
endColumn: 31,
683+
line: 7,
684+
messageId: 'useWebFirstAssertion',
685+
},
686+
],
687+
output: test(`
688+
let fooLocatorText;
689+
let fooLocatorText2;
690+
const fooLocator = page.locator('.fooClass');
691+
fooLocatorText = fooLocator;
692+
fooLocatorText2 = await fooLocator.allTextContents();
693+
await expect(fooLocatorText).toHaveText('foo');
694+
`),
695+
},
696+
{
697+
code: test(`
698+
let fooLocatorText;
699+
fooLocatorText = 'foo';
700+
expect(fooLocatorText).toEqual('foo');
701+
fooLocatorText = await page.locator('.fooClass').allTextContents();
702+
expect(fooLocatorText).toEqual('foo');
703+
`),
704+
errors: [
705+
{
706+
column: 9,
707+
data: { matcher: 'toHaveText', method: 'allTextContents' },
708+
endColumn: 31,
709+
line: 6,
710+
messageId: 'useWebFirstAssertion',
711+
},
712+
],
713+
output: test(`
714+
let fooLocatorText;
715+
fooLocatorText = 'foo';
716+
expect(fooLocatorText).toEqual('foo');
717+
fooLocatorText = page.locator('.fooClass');
718+
await expect(fooLocatorText).toHaveText('foo');
719+
`),
720+
},
721+
{
722+
code: test(`
723+
const unrelatedAssignment = "unrelated";
724+
const fooLocatorText = await page.locator('.foo').allTextContents();
725+
expect(fooLocatorText).toEqual('foo');
726+
`),
727+
errors: [
728+
{
729+
column: 9,
730+
data: { matcher: 'toHaveText', method: 'allTextContents' },
731+
endColumn: 31,
732+
line: 4,
733+
messageId: 'useWebFirstAssertion',
734+
},
735+
],
736+
output: test(`
737+
const unrelatedAssignment = "unrelated";
738+
const fooLocatorText = page.locator('.foo');
739+
await expect(fooLocatorText).toHaveText('foo');
740+
`),
741+
},
742+
{
743+
code: test(`
744+
const locatorFoo = page.locator(".foo")
745+
const isBarText = await locatorFoo.locator(".bar").allTextContents()
746+
expect(isBarText).toBe("bar")
747+
`),
748+
errors: [
749+
{
750+
column: 9,
751+
data: { matcher: 'toHaveText', method: 'allTextContents' },
752+
endColumn: 26,
753+
line: 4,
754+
messageId: 'useWebFirstAssertion',
755+
},
756+
],
757+
output: test(`
758+
const locatorFoo = page.locator(".foo")
759+
const isBarText = locatorFoo.locator(".bar")
760+
await expect(isBarText).toHaveText("bar")
761+
`),
762+
},
763+
{
764+
code: test(`
765+
const content = await foo.allTextContents();
766+
expect(content).toBe("bar")
767+
`),
768+
errors: [
769+
{
770+
column: 9,
771+
data: { matcher: 'toHaveText', method: 'allTextContents' },
772+
endColumn: 24,
773+
line: 3,
774+
messageId: 'useWebFirstAssertion',
775+
},
776+
],
777+
output: test(`
778+
const content = foo;
779+
await expect(content).toHaveText("bar")
780+
`),
781+
},
782+
532783
// isChecked
533784
{
534785
code: test('expect(await page.locator("howdy").isChecked()).toBe(true)'),

src/rules/prefer-web-first-assertions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type MethodConfig = {
1616
}
1717

1818
const methods: Record<string, MethodConfig> = {
19+
allTextContents: { matcher: 'toHaveText', type: 'string' },
1920
getAttribute: {
2021
matcher: 'toHaveAttribute',
2122
type: 'string',

0 commit comments

Comments
 (0)