Skip to content

Commit 08a90a2

Browse files
committed
test(textarea): query the shadowRoot in tests
1 parent 59b2423 commit 08a90a2

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

core/src/components/textarea/test/textarea.spec.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ it('should inherit attributes', async () => {
88
html: '<ion-textarea title="my title" tabindex="-1" data-form-type="password"></ion-textarea>',
99
});
1010

11-
const nativeEl = page.body.querySelector('ion-textarea textarea')!;
11+
const textareaEl = page.body.querySelector('ion-textarea')!;
12+
const nativeEl = textareaEl.shadowRoot!.querySelector('textarea')!;
1213
expect(nativeEl.getAttribute('title')).toBe('my title');
1314
expect(nativeEl.getAttribute('tabindex')).toBe('-1');
1415
expect(nativeEl.getAttribute('data-form-type')).toBe('password');
@@ -21,7 +22,7 @@ it('should inherit watched attributes', async () => {
2122
});
2223

2324
const textareaEl = page.body.querySelector('ion-textarea')!;
24-
const nativeEl = textareaEl.querySelector('textarea')!;
25+
const nativeEl = textareaEl.shadowRoot!.querySelector('textarea')!;
2526

2627
expect(nativeEl.getAttribute('dir')).toBe('ltr');
2728

@@ -52,7 +53,7 @@ describe('textarea: label rendering', () => {
5253

5354
const textarea = page.body.querySelector('ion-textarea')!;
5455

55-
const labelText = textarea.querySelector('.label-text-wrapper')!;
56+
const labelText = textarea.shadowRoot!.querySelector('.label-text-wrapper')!;
5657

5758
expect(labelText.textContent).toBe('Label Prop Text');
5859
});
@@ -66,9 +67,16 @@ describe('textarea: label rendering', () => {
6667

6768
const textarea = page.body.querySelector('ion-textarea')!;
6869

69-
const labelText = textarea.querySelector('.label-text-wrapper')!;
70+
// When using a slot, the content is in the light DOM, not directly
71+
// accessible via textContent. Check that the slot element exists and
72+
// the slotted content is in the light DOM.
73+
const slotEl = textarea.shadowRoot!.querySelector('slot[name="label"]');
74+
const propEl = textarea.shadowRoot!.querySelector('.label-text');
75+
const slottedContent = textarea.querySelector('[slot="label"]');
7076

71-
expect(labelText.textContent).toBe('Label Prop Slot');
77+
expect(slotEl).not.toBe(null);
78+
expect(propEl).toBe(null);
79+
expect(slottedContent?.textContent).toBe('Label Prop Slot');
7280
});
7381
it('should render label prop if both prop and slot provided', async () => {
7482
const page = await newSpecPage({
@@ -80,7 +88,7 @@ describe('textarea: label rendering', () => {
8088

8189
const textarea = page.body.querySelector('ion-textarea')!;
8290

83-
const labelText = textarea.querySelector('.label-text-wrapper')!;
91+
const labelText = textarea.shadowRoot!.querySelector('.label-text-wrapper')!;
8492

8593
expect(labelText.textContent).toBe('Label Prop Text');
8694
});

core/src/components/textarea/test/textarea.spec.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ it('should render bottom content when helper text is defined', async () => {
77
html: `<ion-textarea label="Textarea" helper-text="Helper Text"></ion-textarea>`,
88
});
99

10-
const bottomContent = page.body.querySelector('ion-textarea .textarea-bottom');
10+
const textarea = page.body.querySelector('ion-textarea')!;
11+
const bottomContent = textarea.shadowRoot!.querySelector('.textarea-bottom');
1112
expect(bottomContent).not.toBe(null);
1213
});
1314

@@ -17,7 +18,8 @@ it('should render bottom content when helper text is undefined', async () => {
1718
html: `<ion-textarea label="Textarea"></ion-textarea>`,
1819
});
1920

20-
const bottomContent = page.body.querySelector('ion-textarea .textarea-bottom');
21+
const textarea = page.body.querySelector('ion-textarea')!;
22+
const bottomContent = textarea.shadowRoot!.querySelector('.textarea-bottom');
2123
expect(bottomContent).toBe(null);
2224
});
2325

@@ -27,6 +29,7 @@ it('should render bottom content when helper text is empty string', async () =>
2729
html: `<ion-textarea label="Textarea" helper-text=""></ion-textarea>`,
2830
});
2931

30-
const bottomContent = page.body.querySelector('ion-textarea .textarea-bottom');
32+
const textarea = page.body.querySelector('ion-textarea')!;
33+
const bottomContent = textarea.shadowRoot!.querySelector('.textarea-bottom');
3134
expect(bottomContent).toBe(null);
3235
});

core/src/utils/forms/validity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const reportValidityToElementInternals = (
2424
nativeElement: HTMLInputElement | HTMLTextAreaElement | null | undefined,
2525
internals: ElementInternals
2626
): void => {
27-
if (!nativeElement) {
27+
if (!nativeElement?.validity) {
2828
return;
2929
}
3030

0 commit comments

Comments
 (0)